Skip to content

Lenient handling of *tuple[Any, ...] (part 2) - #22006

Merged
ilevkivskyi merged 5 commits into
python:masterfrom
ilevkivskyi:fix-any-tvt-2
Sep 22, 2026
Merged

ilevkivskyi merged 5 commits into
python:masterfrom
ilevkivskyi:fix-any-tvt-2

Conversation

@ilevkivskyi

Copy link
Copy Markdown
Member

Ref #19858

This (smaller) part handles various edge cases that should be handled leniently in presence of *tuple[Any, ...]: indexing and unpacking. Everything is straightforward, similar to first part #22001, the tests are focused on situations that previously gave errors.

cc @JukkaL

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@JukkaL JukkaL left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks, looks good -- just a few comments.

x, y, z = t
reveal_type(x) # N: Revealed type is "builtins.int"
reveal_type(y) # N: Revealed type is "Any"
reveal_type(z) # N: Revealed type is "builtins.str"

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Test also the case with two lvalues (tuple expands to empty).

# Slicing support for variadic tuples is currently best effort, the key idea is to not give an error.
reveal_type(t[1:3:2]) # N: Revealed type is "builtins.tuple[builtins.int | Any | builtins.str, ...]"
reveal_type(t1[1:3:2]) # N: Revealed type is "builtins.tuple[builtins.int | Any, ...]"
reveal_type(t2[1:3:2]) # N: Revealed type is "builtins.tuple[Any | builtins.str, ...]"

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Should t1[::0] generate an error, since it doesn't work at runtime (and doesn't make sense)?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yes, we do give an error for 0 stride (but the error message is bad). I will probably change the logic slightly to give a better error message.

@github-actions

Copy link
Copy Markdown
Contributor

According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅

@ilevkivskyi
ilevkivskyi merged commit 5a06937 into python:master Sep 22, 2026
25 checks passed
@ilevkivskyi
ilevkivskyi deleted the fix-any-tvt-2 branch September 22, 2026 00:54
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.

2 participants