Skip to content

Commit

Permalink
Fix None slice bounds with strict-optional (#3445)
Browse files Browse the repository at this point in the history
Fixes #3442.
  • Loading branch information
miedzinski authored and gvanrossum committed May 29, 2017
1 parent 8c989bf commit 9ec8a95
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
4 changes: 3 additions & 1 deletion mypy/checkexpr.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
from mypy.typevars import fill_typevars
from mypy.visitor import ExpressionVisitor
from mypy.funcplugins import get_function_plugin_callbacks, PluginCallback
from mypy.typeanal import make_optional_type

from mypy import experiments

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

def visit_slice_expr(self, e: SliceExpr) -> Type:
expected = make_optional_type(self.named_type('builtins.int'))
for index in [e.begin_index, e.end_index, e.stride]:
if index:
t = self.accept(index)
self.chk.check_subtype(t, self.named_type('builtins.int'),
self.chk.check_subtype(t, expected,
index, messages.INVALID_SLICE_INDEX)
return self.named_type('builtins.slice')

Expand Down
10 changes: 10 additions & 0 deletions test-data/unit/check-expressions.test
Original file line number Diff line number Diff line change
Expand Up @@ -1006,6 +1006,16 @@ a[None:]
a[:None]
[builtins fixtures/slice.pyi]

[case testNoneSliceBoundsWithStrictOptional]
# flags: --strict-optional
from typing import Any
a = None # type: Any
a[None:1]
a[1:None]
a[None:]
a[:None]
[builtins fixtures/slice.pyi]


-- String interpolation
-- --------------------
Expand Down

0 comments on commit 9ec8a95

Please sign in to comment.