Skip to content

[ty] Promote tuple lengths based on the outer element types - #29147

Open
charliermarsh wants to merge 1 commit into
charlie/codex-generic-base-validationfrom
charlie/codex-tuple-promotion
Open

charliermarsh wants to merge 1 commit into
charlie/codex-generic-base-validationfrom
charlie/codex-tuple-promotion

Conversation

@charliermarsh

@charliermarsh charliermarsh commented Oct 6, 2026 •

Copy link
Copy Markdown
Member

Summary

We normally infer list[tuple[int, ...]] for [(1,), (1, 2)], allowing tuples of other lengths to be added later. This tuple-size promotion applies to tuple literals; tuple types supplied by annotations or other expressions retain their existing lengths.

The check that preserves those lengths currently searches for tuples anywhere inside an element's type. This incorrectly blocks promotion when the tuple appears inside a generic type argument, a callable's return type, or a protocol member:

class Box[T]: ...

def example(box: Box[tuple[int, str]]) -> None:
    values = [(1,), (1, 2), box]
    reveal_type(values)
    # Before: list[tuple[int] | tuple[int, int] | Box[tuple[int, str]]]
    # After:  list[Box[tuple[int, str]] | tuple[int, ...]]

The Box is not a tuple, so its type argument should not prevent us from widening the two tuple literals. We replace this use of any_over_type with a check for whether the element itself may be a tuple. The check still follows aliases, unions, type-variable bounds, and NewType bases that can describe a tuple value, while skipping nested type arguments and members.

@charliermarsh charliermarsh added bug An issue describing something that isn't working, or a PR that fixes a bug ty The ty type checker labels Oct 6, 2026
@charliermarsh
charliermarsh added this pull request to stack #29150 October 6, 2026 20:05
@astral-sh-bot

astral-sh-bot Bot commented Oct 6, 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.24%. The percentage of expected errors that received a diagnostic held steady at 98.24%. The number of fully passing files held steady at 134/146.

@astral-sh-bot

astral-sh-bot Bot commented Oct 6, 2026 •

Copy link
Copy Markdown

Memory usage report

Summary

Project Old New Diff Outcome
flake8 38.28MB 38.28MB - ✅
trio 92.60MB 92.58MB -0.01% (11.88kB) ⬇️
prefect 475.17MB 475.15MB -0.00% (14.27kB) ⬇️
sphinx 172.51MB 172.48MB -0.02% (34.62kB) ⬇️

Significant changes

Click to expand detailed breakdown

trio

Name Old New Diff Outcome
ProtocolInterface 200.94kB 197.91kB -1.51% (3.03kB) ⬇️
Type<'db>::apply_specialization_inner_::interned_arguments 932.27kB 930.55kB -0.18% (1.72kB) ⬇️
CallableType 1.63MB 1.63MB -0.09% (1.59kB) ⬇️
GenericAlias<'db>::try_mro_ 735.45kB 734.12kB -0.18% (1.34kB) ⬇️
Type<'db>::apply_specialization_inner_ 654.41kB 653.21kB -0.18% (1.20kB) ⬇️
cached_protocol_interface 73.48kB 72.53kB -1.30% (976.00B) ⬇️
FunctionType 1.20MB 1.20MB -0.07% (856.00B) ⬇️
function_known_decorators 330.52kB 330.24kB -0.09% (288.00B) ⬇️
Specialization 1.05MB 1.05MB -0.02% (240.00B) ⬇️
may_have_unbounded_specialization_inner 840.00B 656.00B -21.90% (184.00B) ⬇️
infer_expression_types_impl 5.97MB 5.97MB -0.00% (152.00B) ⬇️
GenericAlias 373.22kB 373.08kB -0.04% (144.00B) ⬇️
may_have_unbounded_specialization_inner::interned_arguments 448.00B 320.00B -28.57% (128.00B) ⬇️
infer_scope_types_impl 2.83MB 2.83MB -0.00% (64.00B) ⬇️
infer_definition_types 4.68MB 4.68MB -0.00% (64.00B) ⬇️
... 3 more

prefect

Name Old New Diff Outcome
ProtocolInterface 910.42kB 907.72kB -0.30% (2.70kB) ⬇️
infer_expression_types_impl 24.05MB 24.05MB -0.01% (1.86kB) ⬇️
CallableType 8.94MB 8.94MB -0.02% (1.81kB) ⬇️
Type<'db>::apply_specialization_inner_::interned_arguments 4.10MB 4.10MB -0.03% (1.33kB) ⬇️
GenericAlias<'db>::try_mro_ 4.56MB 4.56MB -0.03% (1.24kB) ⬇️
infer_definition_types 37.83MB 37.82MB -0.00% (1.01kB) ⬇️
infer_scope_types_impl 19.67MB 19.67MB -0.00% (1016.00B) ⬇️
Type<'db>::apply_specialization_inner_ 2.96MB 2.96MB -0.03% (912.00B) ⬇️
cached_protocol_interface 304.23kB 303.41kB -0.27% (840.00B) ⬇️
FunctionType 5.73MB 5.73MB -0.01% (488.00B) ⬇️
may_have_unbounded_specialization_inner 1.57kB 1.14kB -27.36% (440.00B) ⬇️
function_known_decorators 1.82MB 1.82MB -0.02% (304.00B) ⬇️
Specialization 4.87MB 4.87MB -0.00% (128.00B) ⬇️
may_have_unbounded_specialization_inner::interned_arguments 960.00B 832.00B -13.33% (128.00B) ⬇️
infer_statement_types_impl 673.79kB 673.70kB -0.01% (88.00B) ⬇️
... 10 more

sphinx

Name Old New Diff Outcome
CallableType 2.05MB 2.04MB -0.33% (6.87kB) ⬇️
ProtocolInterface 322.68kB 316.54kB -1.90% (6.14kB) ⬇️
FunctionType 1.78MB 1.78MB -0.24% (4.38kB) ⬇️
Type<'db>::apply_specialization_inner_::interned_arguments 1.16MB 1.16MB -0.26% (3.12kB) ⬇️
GenericAlias<'db>::try_mro_ 1.41MB 1.41MB -0.19% (2.72kB) ⬇️
Type<'db>::apply_specialization_inner_ 775.89kB 773.70kB -0.28% (2.19kB) ⬇️
cached_protocol_interface 113.33kB 111.26kB -1.83% (2.07kB) ⬇️
infer_expression_types_impl 11.66MB 11.66MB -0.01% (1.08kB) ⬇️
Specialization 1.59MB 1.59MB -0.06% (960.00B) ⬇️
may_have_unbounded_specialization_inner 4.01kB 3.21kB -19.88% (816.00B) ⬇️
remove_self_inner::interned_arguments 99.70kB 99.00kB -0.71% (720.00B) ⬇️
GenericAlias 605.67kB 605.11kB -0.09% (576.00B) ⬇️
FunctionType<'db>::literal_signature_ 1.08MB 1.08MB -0.04% (464.00B) ⬇️
may_have_unbounded_specialization_inner::interned_arguments 2.94kB 2.50kB -14.89% (448.00B) ⬇️
infer_deferred_types 2.05MB 2.05MB -0.02% (408.00B) ⬇️
... 8 more

@astral-sh-bot

astral-sh-bot Bot commented Oct 6, 2026 •

Copy link
Copy Markdown

ecosystem-analyzer results

No diagnostic changes detected ✅

Full report with detailed diff (timing results)

@charliermarsh
charliermarsh marked this pull request as ready for review October 6, 2026 20:56
@charliermarsh
charliermarsh requested a review from a team as a code owner October 6, 2026 20:56
@charliermarsh
charliermarsh removed this pull request from stack #29150 October 6, 2026 23:50
@charliermarsh
charliermarsh added this pull request to stack #29156 October 6, 2026 23:50
Co-authored-by: Ibraheem Ahmed <ibraheem@ibraheem.ca>
@charliermarsh
charliermarsh force-pushed the charlie/codex-tuple-promotion branch from 7afcfde to 9c77300 Compare October 7, 2026 02:00

This branch was successfully deployed

1 active deployment
automations — 9c77300d Deployed Oct 7, 2026 by charliermarsh via security-review / security review #90922
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug An issue describing something that isn't working, or a PR that fixes a bug ty The ty type checker

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant