-
-
Notifications
You must be signed in to change notification settings - Fork 32.3k
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 our terms of service and privacy 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?
Conversation
@@ -2486,14 +2486,14 @@ types. | |||
|
|||
.. warning:: | |||
|
|||
Runtime-checkable protocols are known to be unsafe in several ways. | |||
Runtime-checkable protocols are known to be unsound 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 this here
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.
@AlexWaygood this has a conflict now, and some suggested wording changes. |
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 thread https://discuss.python.org/t/pep-767-annotating-read-only-attributes/73408
📚 Documentation preview 📚: https://cpython-previews--127985.org.readthedocs.build/