Skip to content

Commit

Permalink
Fixed support for homogenous Tuple declarations
Browse files Browse the repository at this point in the history
  • Loading branch information
agronholm committed Aug 23, 2016
1 parent 0d2d6ad commit a400753
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 6 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.rst
@@ -1,6 +1,12 @@
Version history
===============

1.2.2
-----

- Fixed checking of homogenous Tuple declarations (``Tuple[bool, ...]``)


1.2.1
-----

Expand Down
24 changes: 23 additions & 1 deletion tests/test_typeguard.py
Expand Up @@ -283,6 +283,12 @@ def foo(a: Tuple[int, int]):

foo((1, 2))

def test_tuple_ellipsis(self):
def foo(a: Tuple[int, ...]):
assert check_argument_types()

foo((1, 2, 3, 4))

def test_tuple_bad_type(self):
def foo(a: Tuple[int]):
assert check_argument_types()
Expand All @@ -291,14 +297,22 @@ def foo(a: Tuple[int]):
assert str(exc.value) == (
'type of argument a must be a tuple; got int instead')

def test_tuple_wrong_number_of_elements(self):
def test_tuple_too_many_elements(self):
def foo(a: Tuple[int, str]):
assert check_argument_types()

exc = pytest.raises(TypeError, foo, (1, 'aa', 2))
assert str(exc.value) == ('argument a has wrong number of elements (expected 2, got 3 '
'instead)')

def test_tuple_too_few_elements(self):
def foo(a: Tuple[int, str]):
assert check_argument_types()

exc = pytest.raises(TypeError, foo, (1,))
assert str(exc.value) == ('argument a has wrong number of elements (expected 2, got 1 '
'instead)')

def test_tuple_bad_element(self):
def foo(a: Tuple[int, str]):
assert check_argument_types()
Expand All @@ -307,6 +321,14 @@ def foo(a: Tuple[int, str]):
assert str(exc.value) == (
'type of argument a[1] must be str; got int instead')

def test_tuple_ellipsis_bad_element(self):
def foo(a: Tuple[int, ...]):
assert check_argument_types()

exc = pytest.raises(TypeError, foo, (1, 2, 'blah'))
assert str(exc.value) == (
'type of argument a[2] must be int; got str instead')

@pytest.mark.parametrize('value', [6, 'aa'])
def test_union(self, value):
def foo(a: Union[str, int]):
Expand Down
17 changes: 12 additions & 5 deletions typeguard.py
Expand Up @@ -132,11 +132,18 @@ def check_tuple(argname: str, value, expected_type, typevars_memo: Dict[Any, typ
if not isinstance(value, tuple):
raise TypeError('type of {} must be a tuple; got {} instead'.
format(argname, qualified_name(value)))
if len(value) != len(expected_type.__tuple_params__):
raise TypeError('{} has wrong number of elements (expected {}, got {} instead)'
.format(argname, len(expected_type.__tuple_params__), len(value)))
for i, (element, expected_type) in enumerate(zip(value, expected_type.__tuple_params__)):
check_type('{}[{}]'.format(argname, i), element, expected_type, typevars_memo)

if expected_type.__tuple_use_ellipsis__:
element_type = expected_type.__tuple_params__[0]
for i, element in enumerate(value):
check_type('{}[{}]'.format(argname, i), element, element_type, typevars_memo)
else:
if len(value) != len(expected_type.__tuple_params__):
raise TypeError('{} has wrong number of elements (expected {}, got {} instead)'
.format(argname, len(expected_type.__tuple_params__), len(value)))

for i, (element, element_type) in enumerate(zip(value, expected_type.__tuple_params__)):
check_type('{}[{}]'.format(argname, i), element, element_type, typevars_memo)


def check_union(argname: str, value, expected_type, typevars_memo: Dict[Any, type]) -> None:
Expand Down

0 comments on commit a400753

Please sign in to comment.