-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Type declarations of base class attributes not carried over to subclasses if None
is present
#2022
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
Comments
Yeah, definitely a bug. I think it's related to #1338, the type of class attributes is just not checked against the base classes at all (unlike methods). |
#2380) * For class variables, lookup type in base classes (#1338, #2022, #2211) * Ignore TypeVars from base class when looking up types of class variables * Break even if the base type is a TypeVar This avoids looking into the base class of a base class while the type has been changed * Support TypeVar when lookup up class variables in a base class * An additional test-case to see if TypeVars are resolved with an extra subclass * Also handle TypeVars in, for example, a List * Fix initial test-case * Validate if type is defined with an incompatible value against the type of a base class * Additional test case to ensure methods/variables don't overwrite each other * Also support properties * Add test case to show class variable type can be overwritten with Any
Looking into this again (now that #2510 has been merged) I think there's still something fishy here: I think the first example (in the absence of x = None # type: int
x = None which is silent. (However the second example contains an error, since the subclass uses |
Whoops, I probably messed up when I copied the repro over. Edited the comment to correct this. I sadly no longer have a use case for this, as the affected code is gone. I still think it's worth fixing, though, and I hope you figure it out. Cheers! |
The example code no longer causes a mypy error if you use the class BaseClass:
my_attr = () # type: tuple | None
class Subclass(BaseClass):
my_attr = None And mypy is fine with it. |
Simplest repro:
Output:
However, if
None
isn't the sole value but part of something else, this is likely not the desired behaviour. Consider this:Output:
Changing
None
for anything else, adding a# type: ignore
comment on the("one", set),
line (but not on any other line of the assignment) silences the error. This is somewhat related to #1338 (and, to a lesser extent, #1032).The text was updated successfully, but these errors were encountered: