Skip to content

Commit 544c14b

Browse files
committed
Fixes
1 parent e5a51a4 commit 544c14b

File tree

4 files changed

+16
-13
lines changed

4 files changed

+16
-13
lines changed

src/cattrs/gen.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,8 @@ def make_dict_structure_fn(
284284
for a in attrs:
285285
an = a.name
286286
override = kwargs.get(an, _neutral)
287+
if override.omit:
288+
continue
287289
t = a.type
288290
if isinstance(t, TypeVar):
289291
t = mapping.get(t.__name__, t)
@@ -318,9 +320,7 @@ def make_dict_structure_fn(
318320
if a.default is not NOTHING:
319321
lines.append(f"{i}if '{kn}' in o:")
320322
i = f"{i} "
321-
lines.append(f"{i}try:")
322-
else:
323-
lines.append(f"{i}try:")
323+
lines.append(f"{i}try:")
324324
i = f"{i} "
325325
if handler:
326326
if handler == converter._structure_call:
@@ -680,7 +680,7 @@ def make_mapping_structure_fn(
680680

681681
if is_bare_dict:
682682
# No args, it's a bare dict.
683-
lines.append(" res = dict(mapping)")
683+
lines.append(" res = dict(mapping)")
684684
else:
685685
if extended_validation:
686686
globs["enumerate"] = enumerate
@@ -708,11 +708,12 @@ def make_mapping_structure_fn(
708708
f" res = {{{k_s}: {v_s} for k, v in mapping.items()}}"
709709
)
710710
if structure_to is not dict:
711-
lines.append(" res = __cattr_mapping_cl(res)")
711+
lines.append(" res = __cattr_mapping_cl(res)")
712712

713-
total_lines = lines + [" return res"]
713+
total_lines = lines + [" return res"]
714+
script = "\n".join(total_lines)
714715

715-
eval(compile("\n".join(total_lines), "", "exec"), globs)
716+
eval(compile(script, "", "exec"), globs)
716717

717718
fn = globs[fn_name]
718719

tests/metadata/test_genconverter.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,12 @@
3131
unstructure_strats = sampled_from(list(UnstructureStrategy))
3232

3333

34-
@given(simple_typed_classes() | simple_typed_dataclasses(), unstructure_strats)
35-
def test_simple_roundtrip(cls_and_vals, strat):
34+
@given(simple_typed_classes() | simple_typed_dataclasses(), unstructure_strats, booleans())
35+
def test_simple_roundtrip(cls_and_vals, strat, extended_validation):
3636
"""
3737
Simple classes with metadata can be unstructured and restructured.
3838
"""
39-
converter = Converter(unstruct_strat=strat)
39+
converter = Converter(unstruct_strat=strat, extended_validation=extended_validation)
4040
cl, vals = cls_and_vals
4141
inst = cl(*vals)
4242
unstructured = converter.unstructure(inst)

tests/test_gen_dict.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,8 @@ class A:
255255
assert converter.unstructure(A(1)) == {"a": 1}
256256

257257

258-
def test_omitting_structure():
258+
@pytest.mark.parametrize("extended_validation", [True, False])
259+
def test_omitting_structure(extended_validation: bool):
259260
"""Omitting fields works with generated structuring functions."""
260261
converter = Converter()
261262

@@ -268,7 +269,7 @@ class A:
268269
converter.register_structure_hook(
269270
A,
270271
make_dict_structure_fn(
271-
A, converter, b=override(omit=True), c=override(omit=True)
272+
A, converter, b=override(omit=True), c=override(omit=True), _cattrs_extended_validation=extended_validation
272273
),
273274
)
274275

tests/test_generics.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,9 @@ def test_able_to_structure_generics(converter: Converter, t, t2, result):
6565
)
6666
@pytest.mark.parametrize("extended_validation", [True, False])
6767
def test_structure_generics_with_cols(t, result, extended_validation):
68+
raw = asdict(result)
6869
res = GenConverter(extended_validation=extended_validation).structure(
69-
asdict(result), GenericCols[t]
70+
raw, GenericCols[t]
7071
)
7172

7273
assert res == result

0 commit comments

Comments
 (0)