Skip to content

[ty] Reject fixed tuples for non-inferable TypeVarTuples - #27943

Open
carljm wants to merge 1 commit into
cjm/ty-typevartuple-unknown-recoveryfrom
cjm/ty-4342-fixed-variadic-tuples
Open

[ty] Reject fixed tuples for non-inferable TypeVarTuples#27943
carljm wants to merge 1 commit into
cjm/ty-typevartuple-unknown-recoveryfrom
cjm/ty-4342-fixed-variadic-tuples

Conversation

@carljm

@carljm carljm commented Aug 20, 2026

Copy link
Copy Markdown
Contributor

On main, a function returning tuple[*Ts] can wrongly return an unrelated fixed-length tuple without an error. For example, returning () from a variadic-identity function caused ty to infer a nonempty tuple at call sites even though the runtime result was an empty tuple.

Only expand a non-inferable TypeVarTuple target when the source tuple is variable-length. Fixed tuples now reach the ordinary type-variable rejection instead of repeating the same tuple comparison and succeeding through the recursion guard. Inference and explicit constraint assumptions keep their existing behavior.

Depends on #27950 to avoid cascading errors after a missing TypeVarTuple unpack, and transitively on #27946 to preserve exhaustiveness for variadic class patterns.

Fixes astral-sh/ty#4342.

Test plan

Added mdtests for empty and nonempty fixed returns, annotated assignments, matching tuple prefixes and suffixes, subtyping, and fixed tuples containing Any or Never. Constraint-implication coverage checks that an applicable assumption permits the relationship while incompatible elements and unconstrained packs remain rejected.

@carljm carljm added the ty Multi-file analysis & type inference label Aug 20, 2026
@astral-sh-bot

astral-sh-bot Bot commented Aug 20, 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 20, 2026

Copy link
Copy Markdown

Memory usage report

Memory usage unchanged ✅

@astral-sh-bot

astral-sh-bot Bot commented Aug 20, 2026

Copy link
Copy Markdown

ecosystem-analyzer results

No diagnostic changes detected ✅

Full report with detailed diff (timing results)

@carljm
carljm changed the base branch from main to cjm/ty-invariant-gradual-tuple-materialization August 21, 2026 01:12
@carljm
carljm force-pushed the cjm/ty-4342-fixed-variadic-tuples branch from 9d31f5c to dac40d1 Compare August 21, 2026 01:12
@carljm
carljm marked this pull request as ready for review August 21, 2026 01:46
@carljm
carljm requested a review from a team as a code owner August 21, 2026 01:46
@astral-sh-bot
astral-sh-bot Bot requested a review from ibraheemdev August 21, 2026 01:46
@carljm
carljm marked this pull request as draft August 21, 2026 02:33
@carljm
carljm changed the base branch from cjm/ty-invariant-gradual-tuple-materialization to cjm/ty-typevartuple-unknown-recovery August 21, 2026 03:47
@carljm
carljm force-pushed the cjm/ty-4342-fixed-variadic-tuples branch from dac40d1 to 26e094c Compare August 21, 2026 03:48
@carljm
carljm requested review from dhruvmanila and removed request for ibraheemdev August 21, 2026 04:00
@carljm
carljm marked this pull request as ready for review August 21, 2026 04:01
Comment on lines +370 to +371
def reject_empty[*Ts](values: tuple[*Ts]) -> tuple[*Ts]:
return () # error: [invalid-return-type]

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This does raise a question on what to do for narrowing like:

def reject_empty[*Ts](values: tuple[*Ts]) -> tuple[*Ts]:
    if len(values) == 0:
        # error on this branch but should it?
        return values
    return ()  # error: [invalid-return-type]

Both Pyright and mypy don't error on the first return statement, pyrefly errors on both return statements.

This can also be fixed in follow-up but it would be useful to have a test case for it.

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.

Varadic generic tuples accept too many tuples

2 participants