Skip to content

Commit

Permalink
Fixed TypeError on check_type(..., None)
Browse files Browse the repository at this point in the history
  • Loading branch information
agronholm committed Mar 18, 2023
1 parent 0a78466 commit 4790b43
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/versionhistory.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ This library adheres to `Semantic Versioning 2.0 <https://semver.org/#semantic-v

- Fixed ``warn_on_error()`` not showing where the type violation actually occurred
- Fixed local assignment to ``*args`` or ``**kwargs`` being type checked incorrectly
- Fixed ``TypeError`` on ``check_type(..., None)``

**3.0.1** (2023-03-16)

Expand Down
8 changes: 8 additions & 0 deletions src/typeguard/_checkers.py
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,13 @@ def check_typeguard(
check_type_internal(value, bool, memo)


def check_none(
value: Any, origin_type: Any, args: tuple[Any, ...], memo: TypeCheckMemo
) -> None:
if value is not None:
raise TypeCheckError("is not None")


def check_number(
value: Any, origin_type: Any, args: tuple[Any, ...], memo: TypeCheckMemo
) -> None:
Expand Down Expand Up @@ -689,6 +696,7 @@ def check_type_internal(value: Any, annotation: Any, memo: TypeCheckMemo) -> Non
LiteralString: check_literal_string,
Mapping: check_mapping,
MutableMapping: check_mapping,
None: check_none,
collections.abc.Mapping: check_mapping,
collections.abc.MutableMapping: check_mapping,
Self: check_self,
Expand Down
4 changes: 4 additions & 0 deletions tests/test_checkers.py
Original file line number Diff line number Diff line change
Expand Up @@ -999,6 +999,10 @@ class Foo(SubclassableAny):
check_type(Foo(), int)


def test_none():
check_type(None, None)


def test_return_checked_value():
value = {"foo": 1}
assert check_type(value, Dict[str, int]) is value

0 comments on commit 4790b43

Please sign in to comment.