Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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
bpo-43453: Update and re-add example to typing runtime_checkable #27013
bpo-43453: Update and re-add example to typing runtime_checkable #27013
Changes from 1 commit
f909f79
db0cc5e
cc7d729
6630543
File filter
Filter by extension
Conversations
Jump to
There are no files selected for viewing
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.
Runtime protocol checking is an area which I'm not too familiar with so I may be wrong here, but isn't the requirement for
Callable
to define__call__
, not__init__
?https://docs.python.org/3/library/collections.abc.html#collections.abc.Callable
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.
__call__
makes the instances callable, not the class itself. In my testing, a class meets the Callable check if it doesn't have__call__
defined, and it works if init is either explicitly defined or implicitly inherited fromobject
. However, this class -- SSLObject -- cannot be called because init raises an exception. So this example illustrates that check for callable will tell you SSLObject is callable when in fact it's not callable in practice due to the dunder method raising an exception.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.
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.
Wow, interesting sleuthing. Thanks for investigating!
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.
I wonder if this has anything to play in it, but a class implicitly has
__call__
from the type object:As you can see, the last two are equivalent. This corresponds to what the docs say about callables https://docs.python.org/3/reference/datamodel.html#object.__call__. Also mentioned here for
callable()
https://docs.python.org/3/library/functions.html#callableThe part I'm nitpicky about is saying
ssl.SSLObject
passes theissubclass
check because it defines__init__
. I think that's untrue -- it passes the check because all classes are callable (as mentioned in the docs forcallable()
above). I completely agree with the part about__init__
being used for raising exceptions.I think the phrasing would be better if it became this:
Sorry about the bikshedding!
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.
You're right, good catch! I think your edit is good, but I want to highlight the fact that init as implemented makes this class uncallable, maybe something like this:
WDYT?
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.
Great!