Skip to content

Commit

Permalink
Removed support of default values in TypedDict
Browse files Browse the repository at this point in the history
It should never have been added in the first place.
  • Loading branch information
agronholm committed Nov 26, 2019
1 parent 6002d7c commit f5517a8
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 7 deletions.
1 change: 1 addition & 0 deletions docs/versionhistory.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ This library adheres to `Semantic Versioning 2.0 <https://semver.org/#semantic-v
**UNRELEASED**

- Fixed the handling of ``total=False`` in ``TypedDict``
- Removed support of default values in ``TypedDict``, as they are not supported in the spec

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

Expand Down
13 changes: 7 additions & 6 deletions tests/test_typeguard_py38.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,16 @@ def foo(a: Literal[1, 6, 8]):

@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),
({'y': 'foo'}, True, 'the required key "x" is missing for argument "arg"'),
({'x': 6, 'y': 3}, True,
'type of dict item "y" for argument "arg" must be str; got int instead'),
({'x': 6}, True, 'the required key "y" is missing for argument "arg"'),
({'x': 6}, 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'])
], ids=['correct', 'missing_x', 'wrong_y', 'missing_y_error', 'missing_y_ok', 'wrong_x'])
def test_typed_dict(value, total, error_re):
class DummyDict(TypedDict, total=total):
x: int = 0
x: int
y: str

@typechecked
Expand Down
2 changes: 1 addition & 1 deletion typeguard/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ def check_typed_dict(argname: str, value, expected_type, memo: Optional[_CallMem
argvalue = value.get(key, _missing)
if argvalue is not _missing:
check_type('dict item "{}" for {}'.format(key, argname), argvalue, argtype)
elif expected_type.__total__ and not hasattr(expected_type, key):
elif expected_type.__total__:
raise TypeError('the required key "{}" is missing for {}'
.format(key, argname)) from None

Expand Down

0 comments on commit f5517a8

Please sign in to comment.