Skip to content

Allow using modules as subtypes of protocols #13513

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

Merged
merged 12 commits into from
Aug 27, 2022

Conversation

ilevkivskyi
Copy link
Member

@ilevkivskyi ilevkivskyi commented Aug 25, 2022

Fixes #5018
Fixes #5439
Fixes #10850

The implementation is simple but not the most beautiful one. I simply add a new slot to the Instance class 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 in copy_modified()). We don't need to store it, because all the information we need is already available in MypyFile node. We just need the new attribute to communicate between the checker and subtypes.py.

Other possible alternatives like introducing new dedicated ModuleType, or passing the symbol tables to subtypes.py both look way to complicated. Another argument in favor of this new slot is it could be useful for other things, like hasattr() 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 then accepts_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.

@github-actions

This comment has been minimized.

@ilevkivskyi
Copy link
Member Author

Hm, those few errors are clearly wrong, and I know where they are coming from. Will try to fix now.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@ilevkivskyi
Copy link
Member Author

Yes, I think both should be errors. I will add tests later today (and update implementation if needed).

@ilevkivskyi
Copy link
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).

@ilevkivskyi ilevkivskyi requested a review from sobolevn August 26, 2022 15:35
@github-actions

This comment has been minimized.

Copy link
Member

@sobolevn sobolevn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎉

@github-actions
Copy link
Contributor

According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉

@ilevkivskyi ilevkivskyi merged commit 3efbc5c into python:master Aug 27, 2022
@ilevkivskyi ilevkivskyi deleted the more-proto-impls-2 branch August 27, 2022 18:47
@gvanrossum
Copy link
Member

You rock! Also thank you to the reviewers.

@ilevkivskyi
Copy link
Member Author

Got some free time on my paternity leave :-)

@gvanrossum
Copy link
Member

Got some free time on my paternity leave :-)

Oh congratulations!

@ilevkivskyi
Copy link
Member Author

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants