-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
mypy warns of redefinition of InitVar
#16816
Comments
I think mypy is correct in generating an error here. You are redeclaring the type of the class-scoped symbol If you would like to make the argument that this should be allowed, this would require a change to the Python typing spec. You could pitch your idea in the Python typing forum. |
Thanks a lot for the help here (and honestly in other typing issues I've been involved in with mypy or pyright)! The following is more of a question than a pushback just so I can understand the spec better.
This is strictly technically correct, but in practice is this true? class Foo:
def __init__(self, x: int) -> None:
self._x = x
@property
def x(self) -> int:
return self._x
@x.setter
def x(self, x: int) -> None:
self._x = x which is acceptable, and the stereotypical pattern for using properties. You are correct in that @dataclasses.dataclass
class Foo:
x: InitVar[int] = 1
... is technically a symbol scoped to the class, but practically it's discarded, and I feel like its use here is acceptable? |
Like as an end user, it seems like mypy is inconsistent here on
|
@paw-lu, I think you have a reasonable argument to make, but it would be better to discuss this in the general typing forum rather than the mypy issue tracker. Such a change would ideally involve clarification in the typing spec and special-casing in type checker implementations. The typing spec is currently silent on the matter. Unless that's changed, I think type checkers are correct to treat |
Bug Report
When using an
InitVar
To Reproduce
mypy playground link
Expected Behavior
Since this is essentially
I think this should be ok in principle. The
InitVar
doesn't get "saved" as an attribute, so it's not a real redefinition?Actual Behavior
mypy warns that the property is a redefinition.
Your Environment
--strict
mypy.ini
(and other config files): NoneTo be transparent, this is a rough semi-continuation of an old issue I wrote up:
InitVar
raiseshas no attribute
on 0.960 #12877The text was updated successfully, but these errors were encountered: