Skip to content

bpo-37058: PEP 544: Add Protocol to typing module #13585

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
May 28, 2019

Conversation

ilevkivskyi
Copy link
Member

@ilevkivskyi ilevkivskyi commented May 26, 2019

cc @ambv @JukkaL

I tried to get rid of the _ProtocolMeta, but unfortunately it didn'y work. My idea to return a generic alias from @runtime_checkable made runtime protocols unpickleable. I am not sure what is worse (a custom metaclass or having some classes unpickleable), so I decided to stick with the status quo (since there were no complains so far). So essentially this is a copy of the implementation in typing_extensions with two modifications:

  • Rename @runtime to @runtime_checkable (plus corresponding updates).
  • Allow protocols that extend collections.abc.Iterable etc.

https://bugs.python.org/issue37058

@ilevkivskyi ilevkivskyi changed the title bpo-37058: Add Protocol to typing module bpo-37058: PEP 544: Add Protocol to typing module May 26, 2019
Copy link
Member

@gvanrossum gvanrossum left a comment

Choose a reason for hiding this comment

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

I didn't really review the code, I trust it's just a copy of what's in the external typing.py or typing_extensions.py.


func(C()) # Passes static type check

See :pep:`544` for details. Protocol classes decorated with
Copy link
Member

Choose a reason for hiding this comment

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

This is okay for now, but at some point we may want to make it so that the docs are self-contained without any references to PEPs. (This is so that in the future the implementation can evolve, and the docs should describe the current implementation, while the PEP describes the implementation as it was at the time the PEP was finalized.)

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, we can expand the docs later.

@ilevkivskyi
Copy link
Member Author

ilevkivskyi commented May 27, 2019

I trust it's just a copy of what's in the external typing.py or typing_extensions.py.

To be precise it is not literally a copy. I added some docstrings, few more comments, and factored-out couple pieces of code into private helpers. The tests however are 1:1 copied, so there are no semantic changes.

@ilevkivskyi
Copy link
Member Author

@gvanrossum Thanks for review! (Also thank you for reviewing the other PEP PRs!)

@ilevkivskyi ilevkivskyi merged commit 74d7f76 into python:master May 28, 2019
@ilevkivskyi ilevkivskyi deleted the add-protocols branch May 28, 2019 07:40
DinoV pushed a commit to DinoV/cpython that referenced this pull request Jan 14, 2020
I tried to get rid of the `_ProtocolMeta`, but unfortunately it didn'y work. My idea to return a generic alias from `@runtime_checkable` made runtime protocols unpickleable. I am not sure what is worse (a custom metaclass or having some classes unpickleable), so I decided to stick with the status quo (since there were no complains so far). So essentially this is a copy of the implementation in `typing_extensions` with two modifications:
* Rename `@runtime` to `@runtime_checkable` (plus corresponding updates).
* Allow protocols that extend `collections.abc.Iterable` etc.
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.

4 participants