-
-
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
generic mutable class attributes introduce a vent in the type system #11538
Comments
Thanks. I'm guessing part of this is the legacy of pre-PEP 526 days, but it looks like we also don't reject the following:
|
PEP 526 is actually quite clear about this:
Also cc @erictraut since it looks to me like Pyright doesn't handle this either? |
I can work on this if no one else has started yet 🙂 |
Keeping this open because the original issue still stands |
@hauntsaninja thanks for reopening! I've closed it way too soon. Maybe we can mark attributes with values as implicit So, It might break a lot of existing "patterns" where people override class-level values with instance-level ones, but I still think that this is a bad pattern. Another scenario, do not use implicit Which one do you like best? |
I'm not sure of the right thing. I tend to agree that class-level values that get overridden are a bad pattern, but that's likely too aggressive of a change to make by default. We also need to be careful not to break pre-PEP 526 code, e.g. the following are probably pretty widespread:
That is, "disallow type variables in class level attributes with values" seems good to me. We might need to special case the None value here to accommodate pre-PEP 526 code, but that should still be mostly enough to fix this "type system vent". I'd be happy to do some special-casing of type comments as an alternative, but in general mypy doesn't do a good job of keeping track of whether a type comes from a comment. |
I don't see any ways to solve this problem:
Technically it should be reported:
But, in real life - we should not report it. It is a valid case. |
what about:
|
mypy already complains about that ("Access to generic instance variables via class is ambiguous") |
Resolves part of python#11538
In this example the shared attribute
shared_attr
is imposted upon by different instantiations of that class, causing a sus amongus to be appeared.The text was updated successfully, but these errors were encountered: