[issue39247] dataclass defaults and property don't work together

Thomas report at bugs.python.org
Thu Oct 21 13:27:36 EDT 2021


Thomas <thomas.d.mckay at gmail.com> added the comment:

Scratch that last one, it leads to problem when mixing descriptors with actual default values:

@dataclass
class Foo:
    bar = field(default=some_descriptor)
    # technically this is a descriptor field without a default value or at the very least, the dataclass constructor can't know because it doesn't know what field, if any, this delegates to. This means this will show up as optional in the __init__ signature but it might not be.

    bar = field(default=some_descriptor, default_factory=lambda:4)
    # this could be a solve for the above problem. The dc constructor would install the constructor at the class level and assign 4 to the instance attribute in the __init__. Still doesn't tell the dc constructor if a field is optional or not when it's default value is a descriptor and no default_factory is passed. And it feels a lot more like hack than anything else.


So ignore my previous message. I'm still 100% behind the "descriptor" arg in the field constructor, though :)

PS: Sorry for the noise, I just stumbled onto this problem for the nth-times and I can't get my brain to shut off.

----------

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue39247>
_______________________________________


More information about the Python-bugs-list mailing list