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
MyPy could warn if a dataclass definition contains un-annotated attribute declarations. Attributes d and e in the following example would trigger the warning:
By default, all attributes defined in a dataclass definition are class attrtibutes. Those annotated with a type also become instance attributes, unless their annotation is typing.ClassVar. This leads to the potentially surprising behaviour that un-annotated attributes are automatically only class attributes. Doing Foo(d=1) or Foo(e="bar") using the above definition leads to an "unexpected keyword argument" error, which itself does not immediately indicate (to novice Python programmers) that d and e are class attributes.
I propose that if someone is using mypy, they should be at least prompted to clarify that they intend for an attribute to be a class attribute by using typing.ClassVar. Type annotations are frequently described as "optional", however in this case, by leaving them out, the semantics of the attribute declaration changes. It is also arguable whether this behaviour is presented clearly enough in the documentation (we have to infer that "fields" are instance attributes, and they require a type annotation, therefore un-annotated variables are left as regular class attributes).
The text was updated successfully, but these errors were encountered:
Actually, this request overlaps quite a bit with the existing RUF012 rule from ruff, which is already subject to debate: astral-sh/ruff#5243. In that case, I suppose there is not much hope for this one. For posterity, there is also this python.org post where I initially voiced the concern.
Feature
MyPy could warn if a dataclass definition contains un-annotated attribute declarations. Attributes
d
ande
in the following example would trigger the warning:Pitch
By default, all attributes defined in a dataclass definition are class attrtibutes. Those annotated with a type also become instance attributes, unless their annotation is
typing.ClassVar
. This leads to the potentially surprising behaviour that un-annotated attributes are automatically only class attributes. DoingFoo(d=1)
orFoo(e="bar")
using the above definition leads to an "unexpected keyword argument" error, which itself does not immediately indicate (to novice Python programmers) thatd
ande
are class attributes.I propose that if someone is using mypy, they should be at least prompted to clarify that they intend for an attribute to be a class attribute by using
typing.ClassVar
. Type annotations are frequently described as "optional", however in this case, by leaving them out, the semantics of the attribute declaration changes. It is also arguable whether this behaviour is presented clearly enough in the documentation (we have to infer that "fields" are instance attributes, and they require a type annotation, therefore un-annotated variables are left as regular class attributes).The text was updated successfully, but these errors were encountered: