Skip to content

Commit

Permalink
Refined the checks and their tests
Browse files Browse the repository at this point in the history
  • Loading branch information
agronholm committed Mar 23, 2024
1 parent 1f47446 commit 1ee3625
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 10 deletions.
3 changes: 3 additions & 0 deletions docs/versionhistory.rst
Expand Up @@ -11,6 +11,9 @@ This library adheres to
subscript (`#442 <https://github.com/agronholm/typeguard/issues/442>`_)
- Fixed ``TypedDict`` from ``typing_extensions`` not being recognized as one
(`#443 <https://github.com/agronholm/typeguard/issues/443>`_)
- Fixed parametrized types (``dict[str, int]``, ``List[str]``, etc.) not passing checks
against ``type`` or ``Type``
(`#432 <https://github.com/agronholm/typeguard/issues/432>`_, PR by Yongxin Wang)

**4.1.5** (2023-09-11)

Expand Down
11 changes: 5 additions & 6 deletions src/typeguard/_checkers.py
Expand Up @@ -83,6 +83,9 @@
]

checker_lookup_functions: list[TypeCheckLookupCallback] = []
generic_alias_types: tuple[type, ...] = (type(List), type(List[Any]))
if sys.version_info >= (3, 9):
generic_alias_types += (types.GenericAlias,)


# Sentinel
Expand Down Expand Up @@ -440,11 +443,7 @@ def check_class(
args: tuple[Any, ...],
memo: TypeCheckMemo,
) -> None:
if not isclass(value) and not (
(sys.version_info >= (3, 11) and isinstance(value, types.GenericAlias))
or isinstance(value, type(Type))
or isinstance(value, type(Type[Any]))
):
if not isclass(value) and not isinstance(value, generic_alias_types):
raise TypeCheckError("is not a class")

if not args:
Expand Down Expand Up @@ -479,7 +478,7 @@ def check_class(
raise TypeCheckError(
f"did not match any element in the union:\n{formatted_errors}"
)
elif not issubclass(value, expected_class):
elif not issubclass(value, expected_class): # type: ignore[arg-type]
raise TypeCheckError(f"is not a subclass of {qualified_name(expected_class)}")


Expand Down
10 changes: 6 additions & 4 deletions tests/test_checkers.py
Expand Up @@ -891,11 +891,13 @@ def test_union_typevar(self):
T = TypeVar("T", bound=Parent)
check_type(Child, Type[T])

def test_generic_aliase(self):
@pytest.mark.parametrize("check_against", [type, Type[Any]])
def test_generic_aliase(self, check_against):
if sys.version_info >= (3, 9):
check_type(dict[str, str], type)
check_type(Dict, Type[Any])
check_type(Dict[str, str], Type[Any])
check_type(dict[str, str], check_against)

check_type(Dict, check_against)
check_type(Dict[str, str], check_against)


class TestIO:
Expand Down

0 comments on commit 1ee3625

Please sign in to comment.