Skip to content

Fix None slice bounds with strict-optional #3445

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 29, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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