Skip to content

Lenient handling of *tuple[Any, ...] (part 1) - #22001

Open
ilevkivskyi wants to merge 7 commits into
python:masterfrom
ilevkivskyi:fix-any-tvt
Open

ilevkivskyi wants to merge 7 commits into
python:masterfrom
ilevkivskyi:fix-any-tvt

Conversation

@ilevkivskyi

@ilevkivskyi ilevkivskyi commented Sep 17, 2026

Copy link
Copy Markdown
Member

Fixes #19908
Ref #19109
Ref #18665

From very early days mypy used to handle tuple[X, ...] essentially as tuple[()] | tuple[X] | tuple[X, X] | .... However, there is one notable exception, when X = Any we should use any() semantics instead of all(). That is, if subtyping (or other typeop) works for at least one number of Any items, we should accept it.

Note I special-case both tuple types and variadic instances, so that Foo[*tuple[Any, ...], int] <: Foo[int, int]. Variadic instances with non-Any item stay strict (matching the tuple behaviour). FWIW I didn't see any issues where people complained about strict behavior in non-Any case. We can still change it later, but I don't think we should.

This PR handles the subtyping part only. I will probably make two follow-up PRs handling inference, and special cases (like indexing, slicing, multiassing, etc).

This part was even easier than I thought. The simplification comes from the part that we need to adjust the logic only when variadic Any appears on the left. When it appears on the right, things already work "naturally". (Also looking at architecture of relevant code I am happy how I wrote it :-))

cc @JukkaL @hauntsaninja

@github-actions

Copy link
Copy Markdown
Contributor

Diff from mypy_primer, showing the effect of this PR on open source code:

scipy-stubs (https://github.com/scipy/scipy-stubs)
+ scipy-stubs/sparse/_construct.pyi:96: error: Unused "type: ignore" comment  [unused-ignore]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

tuple[*Ts] subtype with extra type parameter not gradually assignable to itself

1 participant