Skip to content

Commit 654ae20

Browse files
Fix crash when checking slice expression with step 0 in tuple index (#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.
1 parent c7c4288 commit 654ae20

File tree

2 files changed

+9
-0
lines changed

2 files changed

+9
-0
lines changed

mypy/types.py

+3
Original file line numberDiff line numberDiff line change
@@ -2502,6 +2502,9 @@ def slice(
25022502
if fallback is None:
25032503
fallback = self.partial_fallback
25042504

2505+
if stride == 0:
2506+
return None
2507+
25052508
if any(isinstance(t, UnpackType) for t in self.items):
25062509
total = len(self.items)
25072510
unpack_index = find_unpack_in_list(self.items)

test-data/unit/check-tuples.test

+6
Original file line numberDiff line numberDiff line change
@@ -1438,6 +1438,12 @@ reveal_type(t[x:]) # N: Revealed type is "builtins.tuple[Union[builtins.int, bu
14381438
t[y:] # E: Slice index must be an integer, SupportsIndex or None
14391439
[builtins fixtures/tuple.pyi]
14401440

1441+
[case testTupleSliceStepZeroNoCrash]
1442+
# This was crashing: https://github.com/python/mypy/issues/18062
1443+
# TODO: emit better error when 0 is used for step
1444+
()[::0] # E: Ambiguous slice of a variadic tuple
1445+
[builtins fixtures/tuple.pyi]
1446+
14411447
[case testInferTupleTypeFallbackAgainstInstance]
14421448
from typing import TypeVar, Generic, Tuple
14431449
T = TypeVar('T')

0 commit comments

Comments
 (0)