-
Notifications
You must be signed in to change notification settings - Fork 1.6k
[ty] Fix generic inference for non-dataclass inheriting from generic dataclass #21159
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
[ty] Fix generic inference for non-dataclass inheriting from generic dataclass #21159
Conversation
…dataclass Fixes astral-sh/ty#1427 When a non-dataclass class inherits from a generic dataclass, the generic type parameters were being lost during __init__ synthesis, causing invalid-argument-type errors. This was a regression introduced in alpha.24. The issue occurred because own_synthesized_member() only used the parent class's inherited_generic_context instead of accepting the child class's context that was being passed down the MRO. Changes: - Added inherited_generic_context parameter to own_synthesized_member() - Use child's generic context when synthesizing __new__/__init__ signatures - Updated call sites to pass the inherited context - Added regression test for non-dataclass inheriting from generic dataclass 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
a0beca3 to
3dcafb9
Compare
Diagnostic diff on typing conformance testsNo changes detected when running ty on typing conformance tests ✅ |
|
| def uses_dataclass[T](x: T) -> ChildOfParentDataclass[T]: | ||
| return ChildOfParentDataclass(x) | ||
|
|
||
| # TODO: ParentDataclass.__init__ should show generic types, not Unknown |
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.
Thank you for highlighting this. I opened astral-sh/ty#1461 to track this.
sharkdp
left a comment
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.
Thank you!
* origin/main: [ty] Fix generic inference for non-dataclass inheriting from generic dataclass (#21159) Update etcetera to 0.11.0 (#21160) Fix missing diagnostics for notebooks (#21156) [ty] Fix tests for definition completions (#21153) Bump v0.14.3 (#21152) [ty] Don't provide completions when in class or function definition (#21146) [ty] Use the top materialization of classes for narrowing in class-patterns for `match` statements (#21150)
Summary
Fixes astral-sh/ty#1427
This PR fixes a regression introduced in alpha.24 where non-dataclass children of generic dataclasses lost generic type parameter information during
__init__synthesis.The issue occurred because when looking up inherited members in the MRO, the child class's
inherited_generic_contextwas correctly passed down, butown_synthesized_member()(which synthesizes dataclass__init__methods) didn't accept this parameter. It only usedself.inherited_generic_context(db), which returned the parent's context instead of the child's.The fix threads the child's generic context through to the synthesis logic, allowing proper generic type inference for inherited dataclass constructors.
Test Plan
Verification
Tested against multiple type checkers: