Skip to content

Conversation

@saada
Copy link
Contributor

@saada saada commented Oct 31, 2025

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_context was correctly passed down, but own_synthesized_member() (which synthesizes dataclass __init__ methods) didn't accept this parameter. It only used self.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

  • Added regression test for non-dataclass inheriting from generic dataclass
  • Verified the exact repro case from the issue now works
  • All 277 mdtest tests passing
  • Clippy clean
  • Manually verified with Python runtime, mypy, and pyright - all accept this code pattern

Verification

Tested against multiple type checkers:

  • ✅ Python runtime: Code works correctly
  • ✅ mypy: No issues found
  • ✅ pyright: 0 errors, 0 warnings
  • ✅ ty alpha.23: Worked (before regression)
  • ❌ ty alpha.24: Regression
  • ✅ ty with this fix: Works correctly

…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>
@saada saada force-pushed the fix-dataclass-generic-inheritance-1427 branch from a0beca3 to 3dcafb9 Compare October 31, 2025 03:33
@github-actions
Copy link
Contributor

github-actions bot commented Oct 31, 2025

Diagnostic diff on typing conformance tests

No changes detected when running ty on typing conformance tests ✅

@github-actions
Copy link
Contributor

github-actions bot commented Oct 31, 2025

mypy_primer results

No ecosystem changes detected ✅
No memory usage changes detected ✅

@AlexWaygood AlexWaygood added the ty Multi-file analysis & type inference label Oct 31, 2025
sharkdp

This comment was marked as resolved.

def uses_dataclass[T](x: T) -> ChildOfParentDataclass[T]:
return ChildOfParentDataclass(x)

# TODO: ParentDataclass.__init__ should show generic types, not Unknown
Copy link
Contributor

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.

Copy link
Contributor

@sharkdp sharkdp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

@sharkdp sharkdp merged commit 735ec0c into astral-sh:main Oct 31, 2025
41 checks passed
dcreager added a commit that referenced this pull request Oct 31, 2025
* 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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ty Multi-file analysis & type inference

Projects

None yet

Development

Successfully merging this pull request may close these issues.

alpha.24 regression: Non-dataclass inheriting from a dataclass breaks generic inference

3 participants