[ty] support super() in metaclass methods#24483
Merged
Conversation
Typing conformance resultsNo changes detected ✅Current numbersThe percentage of diagnostics emitted that were expected errors held steady at 87.72%. The percentage of expected errors that received a diagnostic held steady at 82.85%. The number of fully passing files held steady at 74/132. |
Memory usage reportMemory usage unchanged ✅ |
|
charliermarsh
approved these changes
Apr 8, 2026
AlexWaygood
reviewed
Apr 8, 2026
AlexWaygood
approved these changes
Apr 9, 2026
Comment on lines
+1382
to
+1387
| pub(crate) fn as_type(self, db: &'db dyn Db) -> Type<'db> { | ||
| match self { | ||
| TypeVarBoundOrConstraints::UpperBound(bound) => bound, | ||
| TypeVarBoundOrConstraints::Constraints(constraints) => constraints.as_type(db), | ||
| } | ||
| } |
Member
There was a problem hiding this comment.
I worry that this method will be a bit of a performance footgun, because it's a very convenient API, but TypeVarConstraints::as_type (which invokes the UnionBuilder) seems unnecessarily expensive for lots of operations where you can get the same result by explicitly mapping over the typevar constraints
Contributor
Author
There was a problem hiding this comment.
I see that, but I'm not sure it justifies not providing the method, when we have valid use cases for it. What would you suggest? I can add a doc comment to it.
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
636988c to
317c37d
Compare
carljm
added a commit
that referenced
this pull request
Apr 10, 2026
* main: [ty] Fix bad diagnostic range for incorrect implicit `__init_subclass__` calls (#24541) [ty] Add a `SupportedPythonVersion` enum (#24412) [ty] Ignore unsupported editor-selected Python versions (#24498) [ty] Add snapshots for `__init_subclass__` diagnostics (#24539) [ty] Minor fix in tests (#24538) [ty] Allow `Final` variable assignments in `__post_init__` (#24529) [ty] Expand test suite for assignment errors (#24537) [ty] Use `map`, not `__map`, as the name of the mapping parameter in `TypedDict` `__init__` methods (#24535) [ty] Rework logic for synthesizing `TypedDict` methods (#24534) [flake8-bandit] Fix S103 false positives and negatives in mask analysis (#24424) [ty] mdtest.py: update dependencies (#24533) Rename patterns and arguments source order iterator method (#24532) [ty] Omit invalid keyword arguments from `TypedDict` signature (#24522) [ty] support super() in metaclass methods (#24483) [ty] Synthesize `__init__` for `TypedDict` (#24476)
carljm
added a commit
that referenced
this pull request
Apr 10, 2026
* main: Bump typing conformance suite commit to latest upstream (#24553) [ty] Reject deleting`Final` attributes (#24508) [ty] Respect property deleters in attribute deletion checks (#24500) [ty] stop unioning Unknown into types of un-annotated attributes (#24531) [ty] Fix bad diagnostic range for incorrect implicit `__init_subclass__` calls (#24541) [ty] Add a `SupportedPythonVersion` enum (#24412) [ty] Ignore unsupported editor-selected Python versions (#24498) [ty] Add snapshots for `__init_subclass__` diagnostics (#24539) [ty] Minor fix in tests (#24538) [ty] Allow `Final` variable assignments in `__post_init__` (#24529) [ty] Expand test suite for assignment errors (#24537) [ty] Use `map`, not `__map`, as the name of the mapping parameter in `TypedDict` `__init__` methods (#24535) [ty] Rework logic for synthesizing `TypedDict` methods (#24534) [flake8-bandit] Fix S103 false positives and negatives in mask analysis (#24424) [ty] mdtest.py: update dependencies (#24533) Rename patterns and arguments source order iterator method (#24532) [ty] Omit invalid keyword arguments from `TypedDict` signature (#24522) [ty] support super() in metaclass methods (#24483) [ty] Synthesize `__init__` for `TypedDict` (#24476)
ibraheemdev
pushed a commit
that referenced
this pull request
Apr 15, 2026
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
This was referenced Apr 27, 2026
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
Fixes astral-sh/ty#3231.
Explicit
super()calls come in two varieties (zero-argumentsuper()is just syntactic sugar for the two explicit forms, with the arguments automatically derived from context):super(ThisClass, instance), whereinstancemust be an instance ofThisClass; this is typically found in instance methods as e.g.super(ThisClass, self), orsuper(ThisClass, SubClass), whereSubClassmust be a subclass ofThisClass; this is typically found in classmethods, as e.g.super(ThisClass, cls).The two cases are handled distinctly at runtime. In the first case we look for
ThisClass(the "pivot class") in the MRO oftype(instance)to decide where to start our MRO walk; in the second case we look for the pivot in the MRO ofSubClassitself (not its type).We previously distinguished these cases by checking whether the second argument (
owner) was a class or not. This fails in the case ofsuper()calls in a metaclass, where the second argument can be a class, but we may still have the "instance" form ofsuper, because the first argument is a metaclass. For example, we havesuper(ThisMetaClass, cls), whereclsis a class, but it is an instance ofThisMetaClass, not a subclass of it.This PR refactors our representation of bound super objects to more clearly represent these two varieties, and more closely match the runtime representation, while eliminating some distinctions we previously tracked as variants of
SuperOwnerKindthat aren't actually necessary to correctly implementing the semantics.Test Plan
Passes all existing tests, plus newly added mdtests for metaclass
super()which fail on main.