Using class methods strategy for structuring Enums fails without clear info
Maintainers usually reply within 1 day
Nobody has claimed this yet.
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 42/100
Research direction
Start with the reproduction in the issue, then inspect cattrs/converters.py around _structure_call, _single_dispatch, and function_dispatch, along with use_class_methods. Verify why the Enum handler bypasses the class method and compare structuring with unstructuring. Done means the intended dispatch behavior is resolved or the conflict is clearly reported, with the reproduction no longer producing the shown round-trip failure.
Written by the indexing model from the issue text.
Description
- cattrs version: 23.2.3
- Python version: Python 3.9.16
- Operating System: macOS Ventura 13.5.2 (22G91)
Description
I want to ultimately serialize an Enum (anywhere it appears in dataclasses) using its name, rather than its value (as the value is an integer that isn't very human interpretable, whereas the name is).
It looks like for some of my other classes, the use_class_methods strategy will be very convenient, so I thought I would use that here. I know there are other approach I could and probably should take, but I was fairly surprised by the resulting behaviour so thought it was worth an issue.
What I Did
If I run this python script:
from enum import Enum
from cattrs import Converter
from cattrs.strategies import use_class_methods
class MyEnum(Enum):
SomeName = 5
AnotherName = 12
@classmethod
def _structure(cls, data: str) -> "MyEnum":
return cls[data]
def _unstructure(self) -> str:
return self.name
converter = Converter()
use_class_methods(converter, "_structure", "_unstructure")
print(converter.unstructure(MyEnum["SomeName"]))
print(converter.structure(5, MyEnum))
print(converter.structure("SomeName", MyEnum))
The output show that the unstructuring behaviour is what I want (using the name), but structuring still expects a value, and doesn't work with a name:
SomeName
MyEnum.SomeName
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/ktdb353/workplace/kazu/.venv/lib/python3.9/site-packages/cattrs/converters.py", line 332, in structure
return self._structure_func.dispatch(cl)(obj, cl)
File "/Users/ktdb353/workplace/kazu/.venv/lib/python3.9/site-packages/cattrs/converters.py", line 430, in _structure_call
return cl(obj)
File "/Users/ktdb353/.pyenv/versions/3.9.16/lib/python3.9/enum.py", line 384, in __call__
return cls.__new__(cls, value)
File "/Users/ktdb353/.pyenv/versions/3.9.16/lib/python3.9/enum.py", line 702, in __new__
raise ve_exc
ValueError: 'SomeName' is not a valid MyEnum
This now means you can't 'round-trip' with an unstructure and then a structure, or you will get an error, which feels quite unexpected.
It appears to me that this is because _structure_call is registered to handle Enum within the _single_dispatch for structure, which is tried before the function_dispatch that the use_class_methods function is using, so MyEnum._structure is never called.
By comparison, within unstructured, there's nothing for Enums until you get to the function_dispatch, so it does use the method from the class.
Is it worth doing something like when registering the class method functions, checking if there's a single or direct dispatch that will handle the class, and therefore if the method will never be used, and if so to log a warning or raise an error? Equally, the risk is something could be added later to the single or direct dispatch that still causes it to never be called.
- Dominant language
- Python
- Stars
- 1.1k
- Forks
- 165
- Avg merge
- 3d 7m
- Merged PRs (30d)
- 6
Getting set up
- No Dockerfile or Docker Compose file
- No pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from python-attrs/cattrs
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
python-attrs/cattrs#761 · 2 comments ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 64/100
python-attrs/cattrs#513 · 4 comments ·
Maintainers usually reply within 1 day
-
Difficulty 3/5 1-2 days Newbie friendliness 85/100
python-attrs/cattrs#774 ·
Maintainers usually reply within 1 day
-
Difficulty 5/5 Over a week Newbie friendliness 35/100
python-attrs/cattrs#746 · 2 comments ·
Maintainers usually reply within 1 day
-
Difficulty 4/5 3-5 days Newbie friendliness 45/100
python-attrs/cattrs#743 · 2 comments ·
Maintainers usually reply within 1 day
All issues in python-attrs/cattrs
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 86/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 91/100
TencentCloud/Octop#1577 · 1 comment ·
Maintainers usually reply within 1 day
-
bug frontend
Difficulty 2/5 1-3 hours Newbie friendliness 85/100
PedestrianDynamics/pyFDS-Evac#552 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 84/100
resend/resend-skills#144 ·
Maintainers usually reply within 1 day
-
good first issue
Difficulty 1/5 Under an hour Newbie friendliness 68/100