Skip to content

Commit

Permalink
fix: support empty sequence
Browse files Browse the repository at this point in the history
  • Loading branch information
PrettyWood committed Feb 4, 2021
1 parent 9b86ec8 commit 95bdc3a
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
9 changes: 9 additions & 0 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ def test_isinstancex_dict(obj, tp, expected):
([3, 4, "1", 2], List[int], False),
([3, 4, 1.1, 2], List[float], False),
((3, 4), List[int], False),
([], List[Any], True),
([], list, True),
],
)
def test_isinstancex_list(obj, tp, expected):
Expand Down Expand Up @@ -134,6 +136,9 @@ def test_isinstancex_set(obj, tp, expected):
((3,), Tuple[str], False),
((3,), Tuple[int, str], False),
([3], Tuple[int], False),
((), Tuple[()], True),
((), Tuple[Any, ...], True),
((), tuple, True),
],
)
def test_isinstancex_tuple(obj, tp, expected):
Expand Down Expand Up @@ -350,6 +355,10 @@ def no_everything(x, y):
(["pika", "chu"], Sequence[str], True),
(("pika", "chu"), Sequence[str], True),
(("pika", "chu"), Sequence[int], False),
([], Sequence, True),
([], Sequence[Any], True),
("", Sequence, True),
((), Sequence[int], True),
],
)
def test_isinstancex_sequence(obj, tp, expected):
Expand Down
3 changes: 3 additions & 0 deletions typingx/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,9 @@ def _is_valid_sequence(obj: Any, tp: TypeLike, *, is_list: bool) -> bool:
Check that a sequence respects a type with args like [str], [str, int], [str, ...]
but also args like [str, int, ...] or even [str, int, ..., bool, ..., float]
"""
if len(obj) == 0:
return True

expected_types = get_args(tp) or (Any, ...)

# We consider expected types of `List[int]` as [int, ...]
Expand Down

0 comments on commit 95bdc3a

Please sign in to comment.