[issue32428] dataclasses: make it an error to have initialized non-fields in a dataclass

Eric V. Smith report at bugs.python.org
Tue Dec 26 21:09:29 EST 2017


Eric V. Smith <eric at trueblade.com> added the comment:

I'm not sure I understand the distinction. You have to look through everything in `__dict__`, then exclude those things that are any type of field (so, real fields or pseudo-fields). Those are the things that are in `__annotations__`, anyway.

The trick is what else to exclude.  In this class:

class C:
    x: int = 0
    y = 0

    def func(self): pass

    @staticmethod
    def staticmeth() : pass

    @classmethod
    def classmeth(cls) : pass

    @property
    def prop(self): pass

These are the non-callables:

print([k for k in C.__dict__ if not callable(getattr(C, k))])

['__module__', '__annotations__', 'x', 'y', 'prop', '__dict__', '__weakref__', '__doc__']

How do we only pick out `y` and probably `prop`, and ignore the rest, without being overly fragile to new things being added? I guess ignoring dunders and things in `__annotations__`. Is that close enough?

----------

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


More information about the Python-bugs-list mailing list