Skip to content

Commit

Permalink
Fix TypedDict checker for odd types (#322)
Browse files Browse the repository at this point in the history
Co-authored-by: Alex Grönholm <alex.gronholm@nextday.fi>
  • Loading branch information
Tolker-KU and agronholm committed Mar 26, 2023
1 parent e831144 commit 7b9deed
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 0 deletions.
5 changes: 5 additions & 0 deletions docs/versionhistory.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ Version history

This library adheres to `Semantic Versioning 2.0 <https://semver.org/#semantic-versioning-200>`_.

**UNRELEASED**

- Fixed checking non-dictionary objects against a ``TypedDict`` annotation
(PR by Tolker-KU)

**3.0.2** (2023-03-22)

- Improved warnings by ensuring that they target user code and not Typeguard internal
Expand Down
3 changes: 3 additions & 0 deletions src/typeguard/_checkers.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,9 @@ def check_mapping(
def check_typed_dict(
value: Any, origin_type: Any, args: tuple[Any, ...], memo: TypeCheckMemo
) -> None:
if not isinstance(value, dict):
raise TypeCheckError("is not a dict")

declared_keys = frozenset(origin_type.__annotations__)
if hasattr(origin_type, "__required_keys__"):
required_keys = origin_type.__required_keys__
Expand Down
6 changes: 6 additions & 0 deletions tests/test_checkers.py
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,12 @@ class TestTypedDict:
r'dict has unexpected extra key\(s\): "foo"',
id="unknown_key",
),
pytest.param(
None,
True,
"is not a dict",
id="not_dict",
),
],
)
def test_typed_dict(self, value, total: bool, error_re: Optional[str]):
Expand Down

0 comments on commit 7b9deed

Please sign in to comment.