Lenient handling of *tuple[Any, ...] (part 1) - #22001
Open
ilevkivskyi wants to merge 7 commits into
Open
ilevkivskyi wants to merge 7 commits into
ilevkivskyi wants to merge 7 commits into
Conversation
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]
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #19908
Ref #19109
Ref #18665
From very early days mypy used to handle
tuple[X, ...]essentially astuple[()] | tuple[X] | tuple[X, X] | .... However, there is one notable exception, whenX = Anywe should useany()semantics instead ofall(). That is, if subtyping (or other typeop) works for at least one number ofAnyitems, 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-Anyitem stay strict (matching the tuple behaviour). FWIW I didn't see any issues where people complained about strict behavior in non-Anycase. 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
Anyappears 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