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 1 commit
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
3 changes: 2 additions & 1 deletion mypy/checkexpr.py
Original file line number Diff line number Diff line change
Expand Up @@ -1893,10 +1893,11 @@ def analyze_super(self, e: SuperExpr, is_lvalue: bool) -> Type:
return AnyType()

def visit_slice_expr(self, e: SliceExpr) -> Type:
expected = UnionType.make_union([NoneTyp(), self.named_type('builtins.int')])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should probably use typeanal.make_optional_type() instead.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was looking for such function/method, but couldn't find it. Thanks!

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
1 change: 1 addition & 0 deletions test-data/unit/check-expressions.test
Original file line number Diff line number Diff line change
Expand Up @@ -998,6 +998,7 @@ a[:o] # E: Slice index must be an integer or None
[builtins fixtures/slice.pyi]

[case testNoneSliceBounds]
# flags: --strict-optional
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should probably test this both with and without strict-optional.

from typing import Any
a = None # type: Any
a[None:1]
Expand Down