Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork32k
Typing docs: increase prominence of warnings regarding@runtime_checkable
#127985
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
base:main
Are you sure you want to change the base?
Uh oh!
There was an error while loading.Please reload this page.
Conversation
@@ -2486,14 +2486,14 @@ types. | |||
.. warning:: | |||
Runtime-checkable protocols are known to beunsafe in several ways. | |||
Runtime-checkable protocols are known to beunsound in several ways. | |||
You should only use them for simple protocols, and even then only use |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
What about suggesting that they should only be used for method-only protocols? Or just for protocols with dunder methods -- since dunder methods typically have a well-understood signature, so the check is more likely to do the right thing at runtime?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
It isn't safe on even just dunder methods. An example of this is numpy arrays and__bool__
, see other discussion about type checkers making unsafe assumptions related to thishere
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
This looks good but I agree with Jukka that it's probably worth mentioning that runtime-checkable protocols make a bit more sense when only methods are involved.
Uh oh!
There was an error while loading.Please reload this page.
We have some warnings in the docs for
typing.runtime_checkable
already, but:This PR makes the warnings more prominent and expands on the examples. It follows renewed discussion on how runtime-checkable protocols are fundamentally unsafe in many ways on the Discourse threadhttps://discuss.python.org/t/pep-767-annotating-read-only-attributes/73408
📚 Documentation preview 📚:https://cpython-previews--127985.org.readthedocs.build/