Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Allow using modules as subtypes of protocols - #13513

Merged
ilevkivskyi merged 12 commits into
python:masterfrom
ilevkivskyi:more-proto-impls-2
Aug 27, 2022
Merged

Allow using modules as subtypes of protocols#13513
ilevkivskyi merged 12 commits into
python:masterfrom
ilevkivskyi:more-proto-impls-2

Conversation

@ilevkivskyi

@ilevkivskyi ilevkivskyi commented Aug 25, 2022

Copy link
Copy Markdown
Member

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
Copy Markdown
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.

Comment thread test-data/unit/check-protocols.test
Comment thread test-data/unit/check-protocols.test
@ilevkivskyi

Copy link
Copy Markdown
Member Author

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

@ilevkivskyi

Copy link
Copy Markdown
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.

@sobolevn sobolevn left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

🎉

@github-actions

Copy link
Copy Markdown
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
Copy Markdown
Member

You rock! Also thank you to the reviewers.

@ilevkivskyi

Copy link
Copy Markdown
Member Author

Got some free time on my paternity leave :-)

@gvanrossum

Copy link
Copy Markdown
Member

Got some free time on my paternity leave :-)

Oh congratulations!

@ilevkivskyi

Copy link
Copy Markdown
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