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

Guido van Rossum report at bugs.python.org
Wed Jan 3 17:06:53 EST 2018


Guido van Rossum <guido at python.org> added the comment:

> > I liked the original design better, where things without annotations would just be ignored. What changed?

> With the original proposal the ignored variables without annotations will behave as class variables. This "conflicts" with PEP 526 which requires class variables to be annotated with ClassVar[...]. On the other hand some people may be unhappy that they need to import `typing` to define a class variable in a dataclass. So this is a convenience vs consistence question. I am more in favour of consistence here, but only +0.

There is no real conflict with PEP 526 though. PEP 526 introduces ClassVar so the type checker can be made to understand. PEP 557 allows omitting ClassVar in case you don't care about type checkers. So I think we should stick with the current spec of PEP 557 (which lets you omit ClassVar), except for this special case:

> I still think that flagging this
> 
> @dataclass
> class C:
>     x = field()
> 
> is important, since simply ignoring a ``field()`` will be too confusing (especially for ``attrs`` users).

Agreed. That's a special case and I'm fine with flagging it as an error.

----------

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


More information about the Python-bugs-list mailing list