[ty] Correct return type for synthesized NamedTuple.__new__ methods#22625
Merged
AlexWaygood merged 2 commits intoastral-sh:mainfrom Jan 16, 2026
Merged
[ty] Correct return type for synthesized NamedTuple.__new__ methods#22625AlexWaygood merged 2 commits intoastral-sh:mainfrom
AlexWaygood merged 2 commits intoastral-sh:mainfrom
Conversation
- Uses a synthetic `Self` type variable for `__new__` in [NamedTuple](cci:2://file:///Users/dexdevlon/Developer/Github/ruff/crates/ty_python_semantic/src/types/class.rs:5307:0-5333:1) synthesis. - Types the `cls` parameter as `type[Self]` and sets the return type to `Self`. - This ensures that `super().__new__` calls in subclasses correctly resolve to the subclass type, fixing astral-sh/ty#2522. - Updated [named_tuple.md](cci:7://file:///Users/dexdevlon/Developer/Github/ruff/crates/ty_python_semantic/resources/mdtest/named_tuple.md:0:0-0:0) assertions to match the new generic display and added a dedicated regression test case.
Typing conformance resultsNo changes detected ✅ |
|
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
Fixed the synthesized
NamedTuple.__new__method to correctly returnSelfinstead of the base class literal. This resolvesinvalid-return-typeerrors in subclasses that override__new__and callsuper().__new__().The change introduces a synthetic
Selftype variable for the__new__signature, making it properly generic. Theclsparameter now usestype[Self]and the return type isSelf, matching Python's runtime behavior and typing conventions.Fixes astral-sh/ty#2522
Test Plan
Added regression test to
crates/ty_python_semantic/resources/mdtest/named_tuple.mdverifying:reveal_type(instance)insideChild.__new__showsSelf@__new__reveal_type(Child(1, 2))correctly showsChildUpdated existing assertions in
named_tuple.mdto reflect the new generic signature: