Skip to content

Commit

Permalink
Add a unit test for the expected behaviour when inheriting from an un…
Browse files Browse the repository at this point in the history
…parameterized generic base class (#40)

* Add a unit test for the expected behaviour when inheriting from an unparaemterized generic base class

* update changelog

* update test

* fix mypy
  • Loading branch information
NiklasRosenstein committed May 31, 2024
1 parent 2f0c8a7 commit fb1717e
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
8 changes: 8 additions & 0 deletions .changelog/_unreleased.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,11 @@ id = "0285c33d-85e6-4d72-bf09-7f8812b75e36"
type = "fix"
description = "Fix star import from `typeapi`"
author = "@NiklasRosenstein"
pr = "https://github.com/NiklasRosenstein/python-typeapi/pull/39"

[[entries]]
id = "233e97d3-9e6c-4330-ac8d-fe4a9ad8e3a5"
type = "tests"
description = "Add a unit test for the expected behaviour when inheriting from an unparameterized generic base class"
author = "@NiklasRosenstein"
pr = "https://github.com/NiklasRosenstein/python-typeapi/pull/40"
41 changes: 41 additions & 0 deletions src/typeapi/typehint_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,47 @@ def test__TypeHint__from_newtype() -> None:
assert hint.hint is MyInt


def test__TypeHint__from_generic_with_unbound_typevar() -> None:
"""
Inheriting from a generic base class without parameterizing it is not valid and databind cannot handle the
case correctly. The `TypeHint.bases` will appear as the bases' bases (that is because `__orig_bases__` is not
set on the new subclass and instead it reads the attribute from the parent).
"""

T = TypeVar("T")
U = TypeVar("U")

class Base(Generic[T]):
a: int

class Incorrect(Base): # type: ignore[type-arg]
b: str

class Correct(Base[U]):
b: str

hint = TypeHint(Base)
assert isinstance(hint, ClassTypeHint)
assert hint.type == Base
assert hint.bases == (Generic[T],)
assert hint.origin is None
assert "__orig_bases__" in vars(Base)

hint = TypeHint(Incorrect)
assert isinstance(hint, ClassTypeHint)
assert hint.type == Incorrect
assert hint.bases == (Generic[T],) # Note how this is not (Base,)
assert hint.origin is None
assert "__orig_bases__" not in vars(Incorrect)

hint = TypeHint(Correct)
assert isinstance(hint, ClassTypeHint)
assert hint.type == Correct
assert hint.bases == (Base[U],) # type: ignore[valid-type]
assert hint.origin is None
assert "__orig_bases__" in vars(Correct)


def test__ClassTypeHint__parametrize() -> None:
"""This method tests the infusion of type parameters into other types.
Expand Down

0 comments on commit fb1717e

Please sign in to comment.