-
-
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
Fix type checking decorated method overrides #3918
Conversation
The fix turned out to be pretty complicated, as there were a bunch of untested method overriding scenarios which weren't quite right. I fixed any related issues that I encountered, though I'm not certain whether some of the issues were hidden previously by other bugs. I also added tests for some related, previously untested scenarios. Fixes #1441.
This is ready for review, but don't merge this yet -- I have some outstanding fixes to internal Dropbox code bases that I want to land first before merging this. |
It looks like you didn't update all tests, now all types are wrapped in quotes |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not a real review, just two random comments. Also this reminds me that there is an old related PR #3263, @kirbyfan64 are you still working on it?
mypy/checker.py
Outdated
elif isinstance(original_type, AnyType): | ||
context) | ||
elif is_equivalent(original_type, typ): | ||
# Assume invariance for a non-callable attribute here. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wait, so does this PR also fix #3208?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No -- this only affects decorators that return a non-callable value (but @property
is unaffected). This is a rare edge case. This also means that this will have only minor backward compatibility issues.
mypy/checker.py
Outdated
elif is_equivalent(original_type, typ): | ||
# Assume invariance for a non-callable attribute here. | ||
# | ||
# TODO: Allow covariance for read-only attributes? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FWIW, protocols currently allow covariant overrides of read-only attributes (like @property
).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@property
is special and it can be overridden covariantly even outside protocols. I'll update the comment to mention that this doesn't affect @property
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about writable properties?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks ready to merge to me.
I should also mention that our internal codebase is now clear for this PR.
mypy/checker.py
Outdated
elif is_equivalent(original_type, typ): | ||
# Assume invariance for a non-callable attribute here. | ||
# | ||
# TODO: Allow covariance for read-only attributes? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about writable properties?
These should be invariant, similar to regular attributes. |
The fix turned out to be pretty complicated, as there were a bunch of
untested method overriding scenarios which weren't quite right. I
fixed any related issues that I encountered, though I'm not certain
whether some of the issues were hidden previously by other bugs. I
also added tests for some related, previously untested scenarios.
Fixes #1441.