Skip to content

Commit

Permalink
address PR comments
Browse files Browse the repository at this point in the history
- remove unneeded test
- pre-compute allowed keys
  • Loading branch information
bkurtz committed Apr 12, 2021
1 parent 32c00aa commit 5982028
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 19 deletions.
5 changes: 3 additions & 2 deletions src/cattr/gen.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,9 +184,10 @@ def make_dict_structure_fn(
)
lines.append(" }")
if _cattrs_forbid_extra_keys:
post_lines.append(f" allowed_fields = {set(a.name for a in attrs)}")
allowed_fields = {a.name for a in attrs}
globs["__c_a"] = allowed_fields
post_lines += [
" unknown_fields = set(o.keys()) - allowed_fields",
" unknown_fields = set(o.keys()) - __c_a",
" if unknown_fields:",
" raise Exception(",
f" 'Extra fields in constructor for {cl_name}: ' + ', '.join(unknown_fields)"
Expand Down
17 changes: 0 additions & 17 deletions tests/metadata/test_genconverter.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,23 +72,6 @@ def test_simple_roundtrip_with_extra_keys_forbidden(cls_and_vals, strat):
assert inst == converter.structure(unstructured, cl)


@given(simple_typed_attrs(defaults=True), unstructure_strats)
def test_simple_roundtrip_defaults_with_extra_keys_forbidden(
attr_and_vals, strat
):
"""
Simple classes can be unstructured and restructured with forbid_extra_keys=True.
"""
a, _ = attr_and_vals
cl = make_class("HypClass", {"a": a})
converter = Converter(unstruct_strat=strat, forbid_extra_keys=True)
inst = cl()
assert converter.unstructure(
converter.structure({}, cl)
) == converter.unstructure(inst)
assert inst == converter.structure(converter.unstructure(inst), cl)


@given(simple_typed_classes() | simple_typed_dataclasses())
def test_forbid_extra_keys(cls_and_vals):
"""
Expand Down

0 comments on commit 5982028

Please sign in to comment.