You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Fields with descriptors as default values should be treated differently, as they do for the "vanilla" dataclasses
Fields with descriptors assigned as field(default=CoolDescriptor(), ...) should not use the descriptor value as the default __init__ argument, instead that argument should be missing.
Possible solution
No response
Alternatives
n/a
Context
No response
The text was updated successfully, but these errors were encountered:
Added a demo-tests for that.
There are two testcase classes, one proving that instantiating normally works, and one doing so by using DataClassJsonMixin.from_json(...). Tests are otherwise exactly the same (with one little exception regarding enums).
Classes used to test the bahaviour are listed below (descriptors do what you expect them to do from their name).
classes.py
importrandomfromdataclassesimportdataclass, fieldfromenumimportEnum, autofromtypingimport*fromdataclasses_jsonimportDataClassJsonMixin, configasdcj_configfromissue_549.descriptorsimport*@dataclassclassUser(DataClassJsonMixin):
""" DataClass with descriptors assigned as default values """user_name: str=ToStrDescriptor()
user_id: str=ToStrDescriptor(default_factory=lambda: random.randint(int(1e+5), int(1e+6)))
@dataclassclassSmartphone(DataClassJsonMixin):
""" DataClass with descriptors assigned as field(default=) without additional metadata """manufacturer: str='Smartsung'battery_charge: float=field(default=PercentDescriptor(default=1.0))
defexplode(self):
self.battery_charge=0delselfclassSemaphoreColor(Enum):
RED=auto()
YELLOW=auto()
GREEN=auto()
defenum_name(e: Enum) ->str: returne.name_scd=EnumDescriptor(SemaphoreColor, default=SemaphoreColor.RED)
@dataclassclassSemaphore(DataClassJsonMixin):
""" DataClass with descriptors assigned as field(default=) with encoder/decoder metadata """program: List[str] =field(default_factory=list)
current_color: SemaphoreColor=field(default=_scd, metadata=dcj_config(encoder=enum_name, decoder=lambdaval: _scd.__value_of__(None, val)))
__all__= \
[
'User',
'Smartphone',
'Semaphore',
'SemaphoreColor',
]
Description
field(default=CoolDescriptor(), ...)
should not use the descriptor value as the default__init__
argument, instead that argument should be missing.Possible solution
No response
Alternatives
n/a
Context
No response
The text was updated successfully, but these errors were encountered: