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
Python 2: Forbid type objects as keys. #163
Python 2: Forbid type objects as keys. #163
Changes from 1 commit
412ca73
1b02aab
fb615ef
163f719
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.
_HasDefaultComparison
is defined as a new class above but where isitem
registered for it, so this check can become true?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.
Right. Specifically, it's defined as a new abstract base class, i.e., something that extends
abc.ABCMeta
. (We could do this with a regulartype
subclass, but extendingABCMeta
gets us caching of the results ofisinstance
automatically.)That's the beauty part (as the saying goes). By making
_HasDefaultComparison
implement__subclasshook__
, we algorithmically define whatisinstance(item, _HasDefaultComparison)
means. No need to register anything! The algorithm is the same as what was here before, just moved into__subclasshook__
. (The expressionisinstance(item, Cls)
basically boils down toCls.__subclasscheck__(type(item))
;ABCMeta
defines__subclasscheck__
to do some caching, and if the answer is not in the cache, to callCls.__subclasshook__()
.)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.
Did that help clear things up sufficiently?
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.
TIL, I think I now understood how it works. Maybe adding a link to the Python documentation would help future readers of the code who rarely use abstract base classes.
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.
Done!