Skip to content
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

feat: Add is_variadic_tuple to TypeView. #11

Merged
merged 1 commit into from
May 13, 2024
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
14 changes: 14 additions & 0 deletions tests/test_type_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,3 +278,17 @@ def test_parsed_type_equality() -> None:
assert TypeView(list[int]) != TypeView(list[str])
assert TypeView(list[str]) != TypeView(tuple[str])
assert TypeView(Optional[str]) == TypeView(Union[str, None])


def test_tuple():
assert TypeView(list[int]).is_tuple is False
assert TypeView(list[int]).is_variadic_tuple is False

assert TypeView(tuple[int]).is_tuple is True
assert TypeView(tuple[int]).is_variadic_tuple is False

assert TypeView(tuple[int, int]).is_tuple is True
assert TypeView(tuple[int, int]).is_variadic_tuple is False

assert TypeView(tuple[int, ...]).is_tuple is True
assert TypeView(tuple[int, ...]).is_variadic_tuple is True
9 changes: 9 additions & 0 deletions type_lens/type_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,15 @@ def is_tuple(self) -> bool:
"""Whether the annotation is a ``tuple`` or not."""
return self.is_subclass_of(tuple)

@property
def is_variadic_tuple(self) -> bool:
"""Whether the annotation is a ``tuple`` **and** is of unbounded length.

Tuples like `tuple[int, ...]` represent a list-like unbounded sequence
of a single type T.
"""
return self.is_tuple and len(self.args) == 2 and self.args[1] == ...

@property
def is_type_var(self) -> bool:
"""Whether the annotation is a TypeVar or not."""
Expand Down
Loading