Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix crash when checking slice expression with step 0 in tuple index #18063

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions mypy/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -2502,6 +2502,9 @@ def slice(
if fallback is None:
fallback = self.partial_fallback

if stride == 0:
return None

if any(isinstance(t, UnpackType) for t in self.items):
total = len(self.items)
unpack_index = find_unpack_in_list(self.items)
Expand Down
6 changes: 6 additions & 0 deletions test-data/unit/check-tuples.test
Original file line number Diff line number Diff line change
Expand Up @@ -1438,6 +1438,12 @@ reveal_type(t[x:]) # N: Revealed type is "builtins.tuple[Union[builtins.int, bu
t[y:] # E: Slice index must be an integer, SupportsIndex or None
[builtins fixtures/tuple.pyi]

[case testTupleSliceStepZeroNoCrash]
# This was crashing: https://github.com/python/mypy/issues/18062
# TODO: emit better error when 0 is used for step
()[::0] # E: Ambiguous slice of a variadic tuple
Comment on lines +1443 to +1444
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

If #18065 gets merged, this test will change:

Suggested change
# TODO: emit better error when 0 is used for step
()[::0] # E: Ambiguous slice of a variadic tuple
()[::0] # E: Slice step cannot be 0

[builtins fixtures/tuple.pyi]

[case testInferTupleTypeFallbackAgainstInstance]
from typing import TypeVar, Generic, Tuple
T = TypeVar('T')
Expand Down
Loading