[ty] Fix inference of t.__mro__ if t is an instance of type[Any]#23632
Merged
AlexWaygood merged 6 commits intomainfrom Feb 28, 2026
Merged
[ty] Fix inference of t.__mro__ if t is an instance of type[Any]#23632AlexWaygood merged 6 commits intomainfrom
t.__mro__ if t is an instance of type[Any]#23632AlexWaygood merged 6 commits intomainfrom
Conversation
When looking up metaclass attributes on `type[Any]` (or `type[Unknown]`), the metaclass is `type[Any]` itself, creating a self-referential cycle where all attribute lookups return `Any`. This prevented data descriptors defined on `type` (like `__mro__` and `__bases__`) from resolving to their correct types. Fix this by checking `type`'s class-level attributes first in `class_member_with_policy` for dynamic `SubclassOf` types. Since all metaclasses inherit from `type`, attributes defined on `type` are always available. For attributes not found on `type`, we fall back to the dynamic result (`Any`/`Unknown`). https://claude.ai/code/session_01KNbz9VVtoQ9CUYtgZ5VRVA
The test is about attribute access on `type[Any]`/`type[Unknown]`, which fits better alongside the existing `__mro__` tests in type.md. https://claude.ai/code/session_01KNbz9VVtoQ9CUYtgZ5VRVA
`type[Any]` and `type[Unknown]` are gradual forms whose metaclass is unknown. When attributes like `__mro__` are resolved via `type`'s descriptors, intersect the result with `Unknown` to express uncertainty about whether the unknown metaclass overrides them. https://claude.ai/code/session_01KNbz9VVtoQ9CUYtgZ5VRVA
Use the original dynamic type from the SubclassOfInner rather than hardcoding Unknown, so `type[Any].__mro__` is `tuple[type, ...] & Any` and `type[Unknown].__mro__` is `tuple[type, ...] & Unknown`. https://claude.ai/code/session_01KNbz9VVtoQ9CUYtgZ5VRVA
Typing conformance results improved 🎉The percentage of diagnostics emitted that were expected errors increased from 85.48% to 85.66%. The percentage of expected errors that received a diagnostic held steady at 76.56%. Summary
False positives removedDetails
|
|
Memory usage reportSummary
Significant changesClick to expand detailed breakdownsphinx
flake8
trio
prefect
|
carljm
approved these changes
Feb 28, 2026
carljm
added a commit
that referenced
this pull request
Mar 2, 2026
* main: [ty] Move binary expression logic out of `builder.rs` (#23649) [ty] Limit recursion depth when displaying self-referential function types (#23647) [ty] Move comparison logic out of `builder.rs` (#23646) Avoid inserting redundant `None` elements in UP045 (#23459) [ty] Add more ParamSpec validation for `P.args` and `P.kwargs` (#23640) [ty] Sync vendored typeshed stubs (#23642) [ty] Fix inference of `t.__mro__` if `t` is an instance of `type[Any]` (#23632) [ty] Detect inconsistent generic base class specializations (#23615) [ty] Validate type variable defaults don't reference later type parameters or type parameters out of scope (#23623) [ty] Ban nested `Required`/`NotRequired`, and ban them both outside of `TypedDict` fields (#23627) [ty] Reject generic metaclasses parameterized by type variables (#23628) Update default Python version examples (#23605) fix binops with NewType of float and Unknown (#23620) [ty] Reject functions with PEP-695 type parameters that shadow type parameters from enclosing scopes (#23619) [ty] Reject ellipsis literals in odd places in type/annotation expressions (#23611) [ty] hash-cons `UseDefMap` fields (#23283)
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 started off this PR trying to fix these conformance-suite assertions:
But, alas... after starting on the PR, I realised that (as with many of the conformance-suite assertions regarding
type[]types), I disagreed with the assertions being made! Iftype[Any]is equivalent totype & Any(and I believe it is!), then the inferred type oft.__mro__wheret: type[Any]should not betuple[type, ...]; it should betuple[type, ...] & Any.Anyway, this PR improves semantics, I think, even if it will sadly not improve our conformance score.
EDIT: oh, huh, it actually improves our conformance score anyway? I guess the error code changes from
type-assertion-failuretoassert-type-unspellable-subtype? Nice.Test Plan
mdtests