Skip to content

Commit 9ec8a95

Browse files
miedzinskigvanrossum
authored andcommitted
Fix None slice bounds with strict-optional (#3445)
Fixes #3442.
1 parent 8c989bf commit 9ec8a95

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

mypy/checkexpr.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
from mypy.typevars import fill_typevars
4646
from mypy.visitor import ExpressionVisitor
4747
from mypy.funcplugins import get_function_plugin_callbacks, PluginCallback
48+
from mypy.typeanal import make_optional_type
4849

4950
from mypy import experiments
5051

@@ -1928,10 +1929,11 @@ def analyze_super(self, e: SuperExpr, is_lvalue: bool) -> Type:
19281929
return AnyType()
19291930

19301931
def visit_slice_expr(self, e: SliceExpr) -> Type:
1932+
expected = make_optional_type(self.named_type('builtins.int'))
19311933
for index in [e.begin_index, e.end_index, e.stride]:
19321934
if index:
19331935
t = self.accept(index)
1934-
self.chk.check_subtype(t, self.named_type('builtins.int'),
1936+
self.chk.check_subtype(t, expected,
19351937
index, messages.INVALID_SLICE_INDEX)
19361938
return self.named_type('builtins.slice')
19371939

test-data/unit/check-expressions.test

+10
Original file line numberDiff line numberDiff line change
@@ -1006,6 +1006,16 @@ a[None:]
10061006
a[:None]
10071007
[builtins fixtures/slice.pyi]
10081008

1009+
[case testNoneSliceBoundsWithStrictOptional]
1010+
# flags: --strict-optional
1011+
from typing import Any
1012+
a = None # type: Any
1013+
a[None:1]
1014+
a[1:None]
1015+
a[None:]
1016+
a[:None]
1017+
[builtins fixtures/slice.pyi]
1018+
10091019

10101020
-- String interpolation
10111021
-- --------------------

0 commit comments

Comments
 (0)