Skip to content

[ty] Guard recursive constructor bindings by receiver - #27958

Merged
carljm merged 3 commits into
mainfrom
cjm/ty-4347-binding-cycles
Aug 21, 2026
Merged

carljm merged 3 commits into
mainfrom
cjm/ty-4347-binding-cycles

Conversation

@carljm

@carljm carljm commented Aug 21, 2026

Copy link
Copy Markdown
Contributor

Summary

Fixes astral-sh/ty#4347.

Constructor signature expansion can revisit the same receiver through class-valued __new__, __init__, or metaclass __call__, recursively expanding bindings until ty overflows the stack. A generic decorator that replaces __new__ with type[C] is one way to trigger this.

Track active constructor receivers during binding expansion and return a gradual signature when an exact receiver repeats, letting the enclosing constructor supply the instance return type. Comparing full receiver types preserves finite generic chains and descriptor overloads that distinguish C from type[C].

Test plan

Extend the constructor mdtests to cover:

  • The reported generic decorator, mutual and generic constructor cycles, and cycles through __init__, metaclass __call__, and callable instances.
  • Finite generic chains, aliases, shared constructors in union branches, and descriptor-sensitive receivers that must continue to resolve their return types.
  • Downstream __init__ argument validation and known non-instance return types from __new__ or metaclass __call__.

The full ty_python_semantic test suite, Clippy, and applicable repository hooks pass.

Track active constructor receiver types while resolving call bindings and recover with a gradual signature when a receiver repeats. Preserve descriptor-sensitive and finite generic constructor chains, along with downstream argument checks.

Add regression coverage for decorator-induced, mutual, and generic constructor cycles and the finite cases that must not be truncated.

Fixes astral-sh/ty#4347.
@carljm carljm added the ty The ty type checker label Aug 21, 2026
@astral-sh-bot

astral-sh-bot Bot commented Aug 21, 2026

Copy link
Copy Markdown

Typing conformance results

No changes detected ✅

Current numbers
The percentage of diagnostics emitted that were expected errors held steady at 97.68%. The percentage of expected errors that received a diagnostic held steady at 93.44%. The number of fully passing files held steady at 109/136.

@astral-sh-bot

astral-sh-bot Bot commented Aug 21, 2026

Copy link
Copy Markdown

Memory usage report

Memory usage unchanged ✅

@astral-sh-bot

astral-sh-bot Bot commented Aug 21, 2026 •

Copy link
Copy Markdown

ecosystem-analyzer results

No diagnostic changes detected ✅

Full report with detailed diff (timing results)

@carljm
carljm marked this pull request as ready for review August 21, 2026 18:46
@carljm
carljm requested a review from a team as a code owner August 21, 2026 18:46
@astral-sh-bot
astral-sh-bot Bot requested a review from dcreager August 21, 2026 18:46
@carljm
carljm requested review from charliermarsh and removed request for dcreager August 21, 2026 19:16
@carljm
carljm merged commit d2620d7 into main Aug 21, 2026
67 checks passed
@carljm
carljm deleted the cjm/ty-4347-binding-cycles branch August 21, 2026 19:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ty The ty type checker

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Stack overflow on generic dunder new decorator

2 participants