Question:
In the relevant section of the PythonEnum
documentation, it is mentioned that mix-in types can be used to ensure items belong to that type and act as objects of that type, like in the example:The code I’m trying to write is
- Defining another class,
EnumFwfField
, and using it as a mix-in, i.e.
but I get the same error as above;class EnumFwfField(FwfField, Enum): pass class CnefeSchema(EnumFwfField, Enum): ...
- Setting the
Enum
fields (in this example,state_code
) toFwfField
, i.e.
but then I getclass CnefeSchema(FwfField, Enum): state_code = FwfField("int", "Código da UF", 2) ...
Traceback (most recent call last): File "src/conf/schemas.py", line 25, in <module> class CnefeSchema(EnumFwfField, Enum): File "/usr/lib/python3.10/enum.py", line 298, in __new__ enum_member.__init__(*args) TypeError: FwfField.__init__() missing 2 required positional arguments: 'name' and 'width'
Answer:
The issue you are having is because in your above code you have aname
attribute in your Field
dataclass, but Enum
will not let you set a name
attribute.Rename that field to something else, for example
'dname'
, and it should work.(The error message in 3.11 is more informative.)
If you have better answer, please add a comment about this, thank you!