-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Bad error message for covariant overriding of a mutable attribute #7109
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
It looks like mypy already prohibits covariant overriding of mutable attributes if one of them is a method, unlike for variables (see #3208), which is quite funny. Note however, this is not a bug, this is actually unsafe code: y = Sub()
x: Base = y
x.formatter2 = None
y.formatter2(42) # Oops, TypeError: NoneType not callable. |
Actually, on the second thought I think we should keep this open for cryptic error message. It should explain that the problem here is mutability in superclass. |
Hmm, indeed this is unsafe in general. The way I use this pattern is to define optional operations, so these fields will never be modified at runtime, but there is no way for mypy to know that. |
It now works as expected (both without and with |
When I check this with mypy 0.711:
It reports:
I would expect all four formatters to be accepted, but for some reason
formatter2
is not.It doesn't make a difference whether I use the old or new semantic analyzer.
The text was updated successfully, but these errors were encountered: