Skip to content

Commit

Permalink
Fixed the handling of total=False in TypedDict
Browse files Browse the repository at this point in the history
Fixes #94.
  • Loading branch information
agronholm committed Nov 26, 2019
1 parent 01e0004 commit f1c87c6
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
4 changes: 4 additions & 0 deletions docs/versionhistory.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ Version history

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

**UNRELEASED**

- Fixed the handling of ``total=False`` in ``TypedDict``

**2.6.1** (2019-11-17)

- Fixed import errors when using the import hook and trying to import a module that has both a
Expand Down
18 changes: 10 additions & 8 deletions tests/test_typeguard_py38.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,16 @@ def foo(a: Literal[1, 6, 8]):
pytest.raises(TypeError, foo, 4).match(r'must be one of \(1, 6, 8\); got 4 instead$')


@pytest.mark.parametrize('value, error_re', [
({'x': 6, 'y': 'foo'}, None),
({'y': 'foo'}, None),
({'y': 3}, 'type of dict item "y" for argument "arg" must be str; got int instead'),
({}, 'the required key "y" is missing for argument "arg"')
], ids=['correct', 'missing_x', 'wrong_type', 'missing_y'])
def test_typed_dict(value, error_re):
class DummyDict(TypedDict):
@pytest.mark.parametrize('value, total, error_re', [
({'x': 6, 'y': 'foo'}, True, None),
({'y': 'foo'}, True, None),
({'y': 3}, True, 'type of dict item "y" for argument "arg" must be str; got int instead'),
({}, True, 'the required key "y" is missing for argument "arg"'),
({}, False, None),
({'x': 'abc'}, False, 'type of dict item "x" for argument "arg" must be int; got str instead')
], ids=['correct', 'missing_x', 'wrong_y', 'missing_y', 'empty_dict', 'wrong_x'])
def test_typed_dict(value, total, error_re):
class DummyDict(TypedDict, total=total):
x: int = 0
y: str

Expand Down

0 comments on commit f1c87c6

Please sign in to comment.