Thomas <thomas.d.mc...@gmail.com> added the comment:

Just to rephrase, because the explanation in my last message can be ambiguous:

At dataclass construction time (when the @dataclass decorator inspects and 
enhances the class):

for field in fields:
    if descriptor := getattr(field, 'descriptor'):
        setattr(cls, field.name, descriptor)
    elif default := getattr(field, 'default'):
        setattr(cls, field.name, default)


Then at __init__ time:

for field in fields:
    if (
        (descriptor := getattr(field, 'descriptor'))
        and (default := getattr(field, 'default'))
    ):
        setattr(self, field.name, default)
    elif default_factory := getattr(field, 'default_factory'):
        setattr(self, field.name, default_factory())

Now, this is just pseudo-code to illustrate the point, I know the dataclass 
implementation generates the __init__ on the fly by building its code as a 
string then exec'ing it. This logic would have to be applied to that generative 
code.

I keep thinking I'm not seeing some obvious problem here, so if something jumps 
out let me know.

----------

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue39247>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to