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
This would be allowed. It prevents converting some classes that would benefit from being a dataclass, since it requires changing the API, since either the attribute must be renamed or the argument must be renamed.
Actual Behavior
test.py:13:9: error: "Foo" has no attribute "arg" [attr-defined]
Found 1 error in 1 file (checked 1 source file)
For reference, pyright also doesn't like this code, but gives a somewhat different error:
test.py:9:14 - error: Cannot assign member "arg" for type "Foo"
Member "arg" is an init-only field (reportGeneralTypeIssues)
Environment
Mypy version used: 1.0.1
Mypy command-line flags: None
Mypy configuration options from mypy.ini (and other config files): None
Python version used: 3.10.11
The text was updated successfully, but these errors were encountered:
You are declaring the same class-scoped symbol twice with different meanings. In one case it's an InitVar and in another case it's an instance variable. I think it's reasonable for mypy to reject this. As you point out, pyright does the same. Refer to this issue if you're interested in details and workarounds.
arg is an instance variable in this case though, unlike the @property case where it's at the class scope. That means that arg isn't even present in the class, even if dataclass didn't convert it into a Field object and places it in __dataclass_fields__ dunder, removing it from the class's __dict__. In fact, many dataclass instance has a different type for instance attributes than the class attributes, since the class value might be a field anyway.
The bigger problem, is even though renaming the InitVar works in this case since my API is completely private, it requires API change, you couldn't, for example convert this class:
Without making a semantic version major change, since it would requiring either renaming the attribute or the init value, neither of which is backwards compatible
When a dataclass
InitVar
has the same name as a class attribute, mypy complains that the class has no attribute.This is possibly related to: #12046
To Reproduce
by renaming the
InitVar
toarg_: InitVar
it worksExpected Behavior
This would be allowed. It prevents converting some classes that would benefit from being a dataclass, since it requires changing the API, since either the attribute must be renamed or the argument must be renamed.
Actual Behavior
For reference, pyright also doesn't like this code, but gives a somewhat different error:
Environment
mypy.ini
(and other config files): NoneThe text was updated successfully, but these errors were encountered: