Skip to content

typing: add _ProtocolMeta #6394

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 4 commits into from
Nov 27, 2021
Merged

typing: add _ProtocolMeta #6394

merged 4 commits into from
Nov 27, 2021

Conversation

hauntsaninja
Copy link
Collaborator

It looks like a couple people are using this in the wild.

Resolves #6393

hauntsaninja and others added 2 commits November 26, 2021 23:43
It looks like a couple people are using this in the wild.

Resolves python#6393
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.

+1, I use it quite a lot.
The first usecase I can remember and find: https://github.com/antonagestam/phantom-types/blob/main/src/phantom/sized.py#L27-L34

I guess we also need to add it to typing_extensions then.

@github-actions

This comment has been minimized.

@hauntsaninja
Copy link
Collaborator Author

hauntsaninja commented Nov 27, 2021

Okay, some notes:
_ProtocolMeta has been in typing.py since the beginning. However, in Python 3.7, it was class _ProtocolMeta(type): .... Since the use case for this internal thing is so you can have your own metaclasses for Protocols, it thus doesn't make sense to have it at all before Python 3.8.

Btw, for whatever it's worth, it looks like that phantom types use case isn't really affected by the issue linked, since the metaclasses it's using don't have any attributes that contribute to the protocol. If it were to add attributes, it might still run into issues, because in general mypy doesn't understand try: ... except ImportError: ...

@github-actions

This comment has been minimized.

1 similar comment
@github-actions
Copy link
Contributor

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

Copy link
Collaborator

@Akuli Akuli left a comment

Choose a reason for hiding this comment

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

No red flags in code, and the example code from related issue no longer errors.

@Akuli Akuli merged commit e734231 into python:master Nov 27, 2021
@hauntsaninja hauntsaninja deleted the typpro branch November 28, 2021 07:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Mypy *sometimes* doesn't see metaclass-provided properties for protocols
3 participants