Skip to content

[ty] Respect tuple subclass iterator overrides - #28769

Draft
carljm wants to merge 1 commit into
mainfrom
cjm/tuple-iteration-overrides
Draft

carljm wants to merge 1 commit into
mainfrom
cjm/tuple-iteration-overrides

Conversation

@carljm

@carljm carljm commented Sep 21, 2026 •

Copy link
Copy Markdown
Contributor

Iteration over a tuple subclass currently uses its stored element types even when the class overrides __iter__.

It would be reasonable to consider an override of __iter__ on a tuple subclass that changes iteration behavior (and why else would you override __iter__, if not to change iteration?) to be inherently a Liskov violation, if iteration behavior is part of the core of what it means to be a tuple. So we could choose to error on tuple subclasses overriding __iter__.

But it seems to me that even if we also do that, it's still better to have this change than not to have it. The added complexity is minimal, the perf impact is not detectable (there's zero cost for regular tuples, which are the vast majority in real-world code), and the result is that we are less likely to make blatantly wrong conclusions in the rare case of code that does this. Even if we want to error on tuple subclasses implementing __iter__, why would we not also want to be less wrong in our type inference of code using such subclasses?

Use the tuple's stored element types when its method resolution order reaches tuple before finding an __iter__ override. Otherwise, infer iteration through the visible iterator method. Base tuple annotations continue to use builtin tuple behavior; the guard handles overrides visible on the supplied subclass type.

Follow-up to #28676.

Test plan

Mdtests cover subclasses inheriting tuple.__iter__, direct and inherited overrides that yield no elements, and mixins before and after tuple in the method resolution order. They verify tuple conversion, loop element types, and membership after starred unpacking, including an override that yields different values from those stored in the tuple.

@carljm
carljm requested a review from a team as a code owner September 21, 2026 22:03
@carljm carljm added the ty The ty type checker label Sep 21, 2026
@astral-sh-bot

astral-sh-bot Bot commented Sep 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 98.15%. The percentage of expected errors that received a diagnostic held steady at 98.07%. The number of fully passing files held steady at 133/146.

@astral-sh-bot

astral-sh-bot Bot commented Sep 21, 2026

Copy link
Copy Markdown

Memory usage report

Memory usage unchanged ✅

@astral-sh-bot

astral-sh-bot Bot commented Sep 21, 2026

Copy link
Copy Markdown

ecosystem-analyzer results

No diagnostic changes detected ✅

Flaky changes detected. This PR summary excludes flaky changes; see the HTML report for details.

Full report with detailed diff (timing results)

@carljm
carljm requested review from AlexWaygood and removed request for charliermarsh September 21, 2026 22:11
@carljm
carljm marked this pull request as draft September 24, 2026 17:12

This branch has not been deployed

No deployments
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.

1 participant