Skip to content

Commit b7f0d22

Browse files
improvement: TupleTypeHint is now a subclass of ClassTypeHint; ClassTypeHint.bases no longer returns none
1 parent 9de9328 commit b7f0d22

File tree

5 files changed

+20
-12
lines changed

5 files changed

+20
-12
lines changed

.changelog/_unreleased.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[[entries]]
2+
id = "641cd28b-3814-4b62-8bd1-c6b88fc71d5a"
3+
type = "improvement"
4+
description = "`TupleTypeHint` is now a subclass of `ClassTypeHint`; `ClassTypeHint.bases` no longer returns none"
5+
author = "@NiklasRosenstein"

src/typeapi/typehint.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ def type(self) -> type:
268268
assert False, "ClassTypeHint not initialized from a real type or a generic that points to a real type."
269269

270270
@property
271-
def bases(self) -> "Tuple[Any, ...] | None":
271+
def bases(self) -> "Tuple[Any, ...]":
272272
return get_type_hint_original_bases(self.type)
273273

274274
def get_parameter_map(self) -> Dict[Any, Any]:
@@ -419,7 +419,7 @@ def expr(self) -> str:
419419
return self._forward_ref.__forward_arg__
420420

421421

422-
class TupleTypeHint(TypeHint):
422+
class TupleTypeHint(ClassTypeHint):
423423
def __init__(self, hint: object, source: "Any | None") -> None:
424424
super().__init__(hint, source)
425425
if self._args == ((),):

src/typeapi/typehint_test.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ def test__TypeHint__list_generic() -> None:
5050
assert str(hint.parameters) == "(~T,)"
5151
assert len(hint) == 0
5252
assert hint.type is list
53-
assert hint.bases is None
53+
assert hint.bases == ()
5454

5555

5656
def test__TypeHint__list_templatized() -> None:
@@ -62,7 +62,7 @@ def test__TypeHint__list_templatized() -> None:
6262
assert str(hint.parameters) == "(~T,)"
6363
assert len(hint) == 1
6464
assert hint.type is list
65-
assert hint.bases is None
65+
assert hint.bases == ()
6666

6767

6868
def test__TypeHint__list_specialized() -> None:
@@ -74,12 +74,12 @@ def test__TypeHint__list_specialized() -> None:
7474
assert hint.parameters == ()
7575
assert len(hint) == 1
7676
assert hint.type is list
77-
assert hint.bases is None
77+
assert hint.bases == ()
7878

7979
hint_0 = hint[0]
8080
assert isinstance(hint_0, ClassTypeHint)
8181
assert hint_0.type == int
82-
assert hint_0.bases is None
82+
assert hint_0.bases == ()
8383

8484

8585
@mark.parametrize(
@@ -442,6 +442,7 @@ def test__TypeHint__native_tuple_type() -> None:
442442
assert hint.args == (Any,)
443443
assert hint.parameters == ()
444444
assert hint.repeated
445+
assert hint.bases == ()
445446

446447
hint = TypeHint(Tuple[Any, ...])
447448
assert isinstance(hint, TupleTypeHint), hint
@@ -450,6 +451,8 @@ def test__TypeHint__native_tuple_type() -> None:
450451
assert hint.origin == tuple
451452
assert hint.args == (Any,)
452453
assert hint.parameters == ()
454+
assert hint.repeated
455+
assert hint.bases == ()
453456

454457

455458
def test__TypeHint__empty_tuple() -> None:

src/typeapi/utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ def get_type_hint_origin_or_none(hint: object) -> "Any | None":
9898
return hint_origin
9999

100100

101-
def get_type_hint_original_bases(hint: object) -> "Tuple[Any, ...] | None":
101+
def get_type_hint_original_bases(hint: object) -> "Tuple[Any, ...]":
102102
"""
103103
Returns the original bases of a generic type.
104104
"""
@@ -108,7 +108,7 @@ def get_type_hint_original_bases(hint: object) -> "Tuple[Any, ...] | None":
108108
if orig_bases is not None and IS_PYTHON_AT_LAST_3_6 and getattr(hint, "__args__", None):
109109
orig_bases = None
110110

111-
return orig_bases
111+
return orig_bases or ()
112112

113113

114114
def get_type_hint_args(hint: object) -> Tuple[Any, ...]:

src/typeapi/utils_test.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ def test__typing_List__introspection():
4949
if sys.version_info[:2] <= (3, 6):
5050
assert get_type_hint_original_bases(List) == (list, MutableSequence[_T])
5151
else:
52-
assert get_type_hint_original_bases(List) is None
53-
assert get_type_hint_original_bases(List[int]) is None
52+
assert get_type_hint_original_bases(List) == ()
53+
assert get_type_hint_original_bases(List[int]) == ()
5454

5555
# Generic-form parameters:
5656

@@ -176,7 +176,7 @@ class MyGeneric(Generic[T, U]):
176176
assert get_type_hint_origin_or_none(MyGeneric) is None
177177
assert get_type_hint_origin_or_none(MyGeneric[int, str]) is MyGeneric
178178
assert get_type_hint_original_bases(MyGeneric) == (Generic[T, U],)
179-
assert get_type_hint_original_bases(MyGeneric[int, str]) is None
179+
assert get_type_hint_original_bases(MyGeneric[int, str]) == ()
180180

181181
# Generic-form parameters:
182182

@@ -234,7 +234,7 @@ class SubGeneric(MyGeneric[T], AnotherGeneric[int], int):
234234
assert get_type_hint_origin_or_none(SubGeneric) is None
235235
assert get_type_hint_origin_or_none(SubGeneric[int]) is SubGeneric
236236
assert get_type_hint_original_bases(SubGeneric) == (MyGeneric[T], AnotherGeneric[int], int)
237-
assert get_type_hint_original_bases(SubGeneric[int]) is None
237+
assert get_type_hint_original_bases(SubGeneric[int]) == ()
238238

239239

240240
@pytest.mark.parametrize(

0 commit comments

Comments
 (0)