Skip to content

Commit cbb91e6

Browse files
committed
Infer generic type arguments for slice expressions
1 parent 8ef2197 commit cbb91e6

File tree

2 files changed

+7
-3
lines changed

2 files changed

+7
-3
lines changed

mypy/checkexpr.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5616,7 +5616,11 @@ def visit_slice_expr(self, e: SliceExpr) -> Type:
56165616
if index:
56175617
t = self.accept(index)
56185618
self.chk.check_subtype(t, expected, index, message_registry.INVALID_SLICE_INDEX)
5619-
return self.named_type("builtins.slice")
5619+
type_args = [
5620+
NoneType() if arg is None else self.accept(arg)
5621+
for arg in [e.begin_index, e.end_index, e.stride]
5622+
]
5623+
return self.chk.named_generic_type("builtins.slice", type_args)
56205624

56215625
def visit_list_comprehension(self, e: ListComprehension) -> Type:
56225626
return self.check_generator_or_comprehension(

test-data/unit/check-expressions.test

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1178,8 +1178,8 @@ class B: pass
11781178
[case testSlicingWithInvalidBase]
11791179

11801180
a: A
1181-
a[1:2] # E: Invalid index type "slice" for "A"; expected type "int"
1182-
a[:] # E: Invalid index type "slice" for "A"; expected type "int"
1181+
a[1:2] # E: Invalid index type "slice[int, int, None]" for "A"; expected type "int"
1182+
a[:] # E: Invalid index type "slice[None, None, None]" for "A"; expected type "int"
11831183
class A:
11841184
def __getitem__(self, n: int) -> 'A': pass
11851185
[builtins fixtures/slice.pyi]

0 commit comments

Comments
 (0)