Skip to content

Commit f550ae8

Browse files
fix: ClassTypeHint.bases now falls back to the types actual base classes if it is not a generic type
1 parent 6870ef1 commit f550ae8

File tree

3 files changed

+17
-7
lines changed

3 files changed

+17
-7
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 = "a5578e7f-32ca-442e-b52a-e6a16c573c35"
3+
type = "fix"
4+
description = "`ClassTypeHint.bases` now falls back to the types actual base classes if it is not a generic type"
5+
author = "@NiklasRosenstein"

src/typeapi/typehint.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,12 @@ def type(self) -> type:
269269

270270
@property
271271
def bases(self) -> "Tuple[Any, ...]":
272-
return get_type_hint_original_bases(self.type)
272+
"""
273+
Return the bases of the classes' types. If the type is a generic, the bases of the generic's origin are
274+
returned in their parameterized form (e.g. `Generic[T]` instead of `Generic` is returned).
275+
"""
276+
277+
return get_type_hint_original_bases(self.type) or self.type.__bases__
273278

274279
def get_parameter_map(self) -> Dict[Any, Any]:
275280
if not self.args:

src/typeapi/typehint_test.py

Lines changed: 6 additions & 6 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 == ()
53+
assert hint.bases == (object,)
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 == ()
65+
assert hint.bases == (object,)
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 == ()
77+
assert hint.bases == (object,)
7878

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

8484

8585
@mark.parametrize(
@@ -442,7 +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 == ()
445+
assert hint.bases == (object,)
446446

447447
hint = TypeHint(Tuple[Any, ...])
448448
assert isinstance(hint, TupleTypeHint), hint
@@ -452,7 +452,7 @@ def test__TypeHint__native_tuple_type() -> None:
452452
assert hint.args == (Any,)
453453
assert hint.parameters == ()
454454
assert hint.repeated
455-
assert hint.bases == ()
455+
assert hint.bases == (object,)
456456

457457

458458
def test__TypeHint__empty_tuple() -> None:

0 commit comments

Comments
 (0)