Allow using modules as subtypes of protocols - #13513
Merged
Merged
Conversation
This comment has been minimized.
This comment has been minimized.
Member
Author
|
Hm, those few errors are clearly wrong, and I know where they are coming from. Will try to fix now. |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
sobolevn
reviewed
Aug 26, 2022
sobolevn
reviewed
Aug 26, 2022
Member
Author
|
Yes, I think both should be errors. I will add tests later today (and update implementation if needed). |
Member
Author
|
@sobolevn OK, I added test and fixed code. Btw I discovered that we didn't do the final check for normal instances either, so I fixed that as well (I actually even found an issue about this on the tracker). |
This comment has been minimized.
This comment has been minimized.
Contributor
|
According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉 |
Member
|
You rock! Also thank you to the reviewers. |
Member
Author
|
Got some free time on my paternity leave :-) |
Member
Oh congratulations! |
Member
Author
|
Thank you! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #5018
Fixes #5439
Fixes #10850
The implementation is simple but not the most beautiful one. I simply add a new slot to the
Instanceclass that represents content of the module. This new attribute is short lived (it is not serialized, and not even stored on variables etc., because we erase it incopy_modified()). We don't need to store it, because all the information we need is already available inMypyFilenode. We just need the new attribute to communicate between the checker andsubtypes.py.Other possible alternatives like introducing new dedicated
ModuleType, or passing the symbol tables tosubtypes.pyboth look way to complicated. Another argument in favor of this new slot is it could be useful for other things, likehasattr()support and ad hoc callable attributes (btw I am already working on the former).Note there is one important limitation: since we don't store the module information, we can't support module objects stored in nested positions, like
self.mods = (foo, bar)and thenaccepts_protocol(self.mods[0]). We only support variables (name expressions) and direct instance, class, or module attributes (see tests). I think this will cover 99% of possible use-cases.