Skip to content

Commit

Permalink
Fix crash when checking slice expression with step 0 in tuple index (#…
Browse files Browse the repository at this point in the history
…18063)

Fixes #18062

I plan to follow up with another PR that adds a better error messages
for slice expressions with `step=0` when indexing standard containers
that disallow that.
  • Loading branch information
brianschubert authored Oct 29, 2024
1 parent c7c4288 commit 654ae20
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
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
[builtins fixtures/tuple.pyi]

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

0 comments on commit 654ae20

Please sign in to comment.