[ty] Use try-call-dunder pattern for __class_getitem__#23066
Merged
Conversation
Typing conformance resultsNo changes detected ✅ |
|
c675685 to
d56ef74
Compare
sharkdp
approved these changes
Feb 4, 2026
Contributor
sharkdp
left a comment
There was a problem hiding this comment.
Thank you. I agree that our behavior was correct before (because __class_getitem__ is special), but this looks like a very nice simplification. I'll add some additional test cases before merging this.
| } | ||
| } | ||
|
|
||
| /// Attempt to call a dunder method defined on a class itself (not its metaclass). |
Contributor
There was a problem hiding this comment.
(not its metaclass)
This is a bit misleading because a __class_getitem__ on the metaclass will (and should!) be found by this method as well. I will adapt this docstring accordingly and add some tests to cover the various ways how __class_getitem__ and __getitem__ on the class and the metaclass can interact.
bxff
pushed a commit
to bxff/ruff
that referenced
this pull request
Feb 4, 2026
…3066) ## Summary I think `__class_getitem__` is a bit different than (e.g.) `__contains__` because it's called on the class itself, not instances... So given `Identity[0]` where `Identity` is a class, using `try_call_dunder` would call `member_lookup_with_policy` with `NO_INSTANCE_FALLBACK`, which would look at the metaclass, but not `Identity` itself. I added `try_call_dunder_on_class` for consistency which calls `member` (without `NO_INSTANCE_FALLBACK`), though as far as this PR is concerned, I think our current behavior was ok? Closes astral-sh/ty#190. --------- Co-authored-by: David Peter <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
I think
__class_getitem__is a bit different than (e.g.)__contains__because it's called on the class itself, not instances... So givenIdentity[0]whereIdentityis a class, usingtry_call_dunderwould callmember_lookup_with_policywithNO_INSTANCE_FALLBACK, which would look at the metaclass, but notIdentityitself.I added
try_call_dunder_on_classfor consistency which callsmember(withoutNO_INSTANCE_FALLBACK), though as far as this PR is concerned, I think our current behavior was ok?Closes astral-sh/ty#190.