Skip to content

Add support for tuple slices. #895

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 2 commits into from
Oct 9, 2015
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
29 changes: 29 additions & 0 deletions mypy/checkexpr.py
Original file line number Diff line number Diff line change
Expand Up @@ -986,6 +986,9 @@ def visit_index_expr_helper(self, e: IndexExpr) -> Type:
# Special case for tuples. They support indexing only by integer
# literals. (Except in weak type checking mode.)
index = e.index
if isinstance(index, SliceExpr):
return self.visit_tuple_slice_helper(left_type, index)

ok = False
if isinstance(index, IntExpr):
n = index.value
Expand All @@ -1010,6 +1013,32 @@ def visit_index_expr_helper(self, e: IndexExpr) -> Type:
e.method_type = method_type
return result

def visit_tuple_slice_helper(self, left_type: TupleType, slic: SliceExpr):
begin = 0
end = len(left_type.items)
stride = 1
if slic.begin_index:
if isinstance(slic.begin_index, IntExpr):
begin = slic.begin_index.value
else:
self.chk.fail(messages.TUPLE_SLICE_MUST_BE_AN_INT_LITERAL, slic.begin_index)
return AnyType()
if slic.end_index:
if isinstance(slic.end_index, IntExpr):
end = slic.end_index.value
else:
self.chk.fail(messages.TUPLE_SLICE_MUST_BE_AN_INT_LITERAL, slic.end_index)
return AnyType()
if slic.stride:
if isinstance(slic.stride, IntExpr):
stride = slic.stride.value
else:
self.chk.fail(messages.TUPLE_SLICE_MUST_BE_AN_INT_LITERAL, slic.stride)
return AnyType()

return TupleType(left_type.items[begin:end:stride], left_type.fallback,
left_type.line, left_type.implicit)

def visit_cast_expr(self, expr: CastExpr) -> Type:
"""Type check a cast expression."""
source_type = self.accept(expr.expr, context=AnyType())
Expand Down
3 changes: 2 additions & 1 deletion mypy/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@
INIT_MUST_HAVE_NONE_RETURN_TYPE = 'The return type of "__init__" must be None'
GETTER_TYPE_INCOMPATIBLE_WITH_SETTER = \
'Type of getter incompatible with setter'
TUPLE_INDEX_MUST_BE_AN_INT_LITERAL = 'Tuple index must an integer literal'
TUPLE_INDEX_MUST_BE_AN_INT_LITERAL = 'Tuple index must be an integer literal'
TUPLE_SLICE_MUST_BE_AN_INT_LITERAL = 'Tuple slice must be an integer literal'
TUPLE_INDEX_OUT_OF_RANGE = 'Tuple index out of range'
TYPE_CONSTANT_EXPECTED = 'Type "Constant" or initializer expected'
INCOMPATIBLE_PAIR_ITEM_TYPE = 'Incompatible Pair item type'
Expand Down
11 changes: 10 additions & 1 deletion mypy/test/data/check-tuples.test
Original file line number Diff line number Diff line change
Expand Up @@ -152,23 +152,32 @@ def f() -> None: pass
from typing import Tuple
t1 = None # type: Tuple[A, B]
t2 = None # type: Tuple[A]
t3 = None # type: Tuple[A, B, C, D, E]
a, b = None, None # type: (A, B)
x = None # type: Tuple[A, B, C]
y = None # type: Tuple[A, C, E]
n = 0

a = t1[1] # E: Incompatible types in assignment (expression has type "B", variable has type "A")
b = t1[0] # E: Incompatible types in assignment (expression has type "A", variable has type "B")
t1[2] # E: Tuple index out of range
t1[3] # E: Tuple index out of range
t2[1] # E: Tuple index out of range
t1[n] # E: Tuple index must an integer literal
t1[n] # E: Tuple index must be an integer literal
t3[n:] # E: Tuple slice must be an integer literal
b = t1[(0)] # E: Incompatible types in assignment (expression has type "A", variable has type "B")

a = t1[0]
b = t1[1]
a = t1[(0)]
x = t3[0:3] # type (A, B, C)
y = t3[0:5:2] # type (A, C, E)

class A: pass
class B: pass
class C: pass
class D: pass
class E: pass
[builtins fixtures/tuple.py]

[case testIndexingTuplesWithNegativeIntegers]
Expand Down