From 1688e142b45d53157777f98c8742c01a2488376c Mon Sep 17 00:00:00 2001 From: Brian Schubert Date: Mon, 28 Oct 2024 15:16:21 -0400 Subject: [PATCH] Fix crash when checking slice expression with step 0 in tuple index --- mypy/types.py | 3 +++ test-data/unit/check-tuples.test | 6 ++++++ 2 files changed, 9 insertions(+) diff --git a/mypy/types.py b/mypy/types.py index 0b010ca9d593..b9bee535c05e 100644 --- a/mypy/types.py +++ b/mypy/types.py @@ -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) diff --git a/test-data/unit/check-tuples.test b/test-data/unit/check-tuples.test index 972bccf8c24b..d675a35c4aae 100644 --- a/test-data/unit/check-tuples.test +++ b/test-data/unit/check-tuples.test @@ -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')