Skip to content

Commit 01fdda5

Browse files
committed
Fix tests
1 parent 65e1e0f commit 01fdda5

File tree

7 files changed

+34
-36
lines changed

7 files changed

+34
-36
lines changed

src/cattr/converters.py

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -93,20 +93,20 @@ class Converter:
9393
"_union_struct_registry",
9494
"_structure_func",
9595
"_prefer_attrib_converters",
96-
"extended_validation",
96+
"detailed_validation",
9797
)
9898

9999
def __init__(
100100
self,
101101
dict_factory: Callable[[], Any] = dict,
102102
unstruct_strat: UnstructureStrategy = UnstructureStrategy.AS_DICT,
103103
prefer_attrib_converters: bool = False,
104-
extended_validation: bool = True,
104+
detailed_validation: bool = True,
105105
) -> None:
106106
unstruct_strat = UnstructureStrategy(unstruct_strat)
107107
self._prefer_attrib_converters = prefer_attrib_converters
108108

109-
self.extended_validation = extended_validation
109+
self.detailed_validation = detailed_validation
110110

111111
# Create a per-instance cache.
112112
if unstruct_strat is UnstructureStrategy.AS_DICT:
@@ -441,7 +441,7 @@ def _structure_list(self, obj, cl):
441441
else:
442442
elem_type = cl.__args__[0]
443443
handler = self._structure_func.dispatch(elem_type)
444-
if self.extended_validation:
444+
if self.detailed_validation:
445445
errors = []
446446
res = []
447447
ix = 0 # Avoid `enumerate` for performance.
@@ -455,7 +455,7 @@ def _structure_list(self, obj, cl):
455455
ix += 1
456456
if errors:
457457
raise IterableValidationError(
458-
f"While structuring {cl.__name__}", errors, cl
458+
f"While structuring {cl!r}", errors, cl
459459
)
460460
else:
461461
res = [handler(e, elem_type) for e in obj]
@@ -467,7 +467,7 @@ def _structure_set(self, obj, cl, structure_to=set):
467467
return structure_to(obj)
468468
elem_type = cl.__args__[0]
469469
handler = self._structure_func.dispatch(elem_type)
470-
if self.extended_validation:
470+
if self.detailed_validation:
471471
errors = []
472472
res = set()
473473
for e in obj:
@@ -479,9 +479,7 @@ def _structure_set(self, obj, cl, structure_to=set):
479479
)
480480
errors.append(exc)
481481
if errors:
482-
raise IterableValidationError(
483-
f"While structuring {cl.__name__}", errors, cl
484-
)
482+
raise IterableValidationError(f"While structuring {cl!r}", errors, cl)
485483
return res if structure_to is set else structure_to(res)
486484
elif structure_to is set:
487485
return {handler(e, elem_type) for e in obj}
@@ -538,7 +536,7 @@ def _structure_tuple(self, obj, tup: Type[T]):
538536
# We're dealing with a homogenous tuple, Tuple[int, ...]
539537
tup_type = tup_params[0]
540538
conv = self._structure_func.dispatch(tup_type)
541-
if self.extended_validation:
539+
if self.detailed_validation:
542540
errors = []
543541
res = []
544542
for ix, e in enumerate(obj):
@@ -549,14 +547,14 @@ def _structure_tuple(self, obj, tup: Type[T]):
549547
errors.append(exc)
550548
if errors:
551549
raise IterableValidationError(
552-
f"While structuring {tup.__name__}", errors, tup
550+
f"While structuring {tup!r}", errors, tup
553551
)
554552
return tuple(res)
555553
else:
556554
return tuple(conv(e, tup_type) for e in obj)
557555
else:
558556
# We're dealing with a heterogenous tuple.
559-
if self.extended_validation:
557+
if self.detailed_validation:
560558
errors = []
561559
res = []
562560
for ix, (t, e) in enumerate(zip(tup_params, obj)):
@@ -568,7 +566,7 @@ def _structure_tuple(self, obj, tup: Type[T]):
568566
errors.append(exc)
569567
if errors:
570568
raise IterableValidationError(
571-
f"While structuring {tup.__name__}", errors, tup
569+
f"While structuring {tup!r}", errors, tup
572570
)
573571
return tuple(res)
574572
else:
@@ -614,13 +612,13 @@ def __init__(
614612
type_overrides: Mapping[Type, AttributeOverride] = {},
615613
unstruct_collection_overrides: Mapping[Type, Callable] = {},
616614
prefer_attrib_converters: bool = False,
617-
extended_validation: bool = True,
615+
detailed_validation: bool = True,
618616
):
619617
super().__init__(
620618
dict_factory=dict_factory,
621619
unstruct_strat=unstruct_strat,
622620
prefer_attrib_converters=prefer_attrib_converters,
623-
extended_validation=extended_validation,
621+
detailed_validation=detailed_validation,
624622
)
625623
self.omit_if_default = omit_if_default
626624
self.forbid_extra_keys = forbid_extra_keys
@@ -746,7 +744,7 @@ def gen_structure_attrs_fromdict(self, cl: Type[T]) -> T:
746744
self,
747745
_cattrs_forbid_extra_keys=self.forbid_extra_keys,
748746
_cattrs_prefer_attrib_converters=self._prefer_attrib_converters,
749-
_cattrs_extended_validation=self.extended_validation,
747+
_cattrs_detailed_validation=self.detailed_validation,
750748
**attrib_overrides,
751749
)
752750
# only direct dispatch so that subclasses get separately generated

src/cattrs/gen.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ def make_dict_structure_fn(
214214
_cattrs_forbid_extra_keys: bool = False,
215215
_cattrs_use_linecache: bool = True,
216216
_cattrs_prefer_attrib_converters: bool = False,
217-
_cattrs_extended_validation: bool = True,
217+
_cattrs_detailed_validation: bool = True,
218218
**kwargs,
219219
) -> Callable[[Mapping[str, Any], Any], T]:
220220
"""Generate a specialized dict structuring function for an attrs class."""
@@ -258,7 +258,7 @@ def make_dict_structure_fn(
258258
resolve_types(cl)
259259

260260
allowed_fields = set()
261-
if _cattrs_extended_validation:
261+
if _cattrs_detailed_validation:
262262
lines.append(" res = {}")
263263
lines.append(" errors = []")
264264
invocation_lines.append("**res,")
@@ -595,7 +595,7 @@ def make_mapping_structure_fn(
595595
structure_to=dict,
596596
key_type=NOTHING,
597597
val_type=NOTHING,
598-
extended_validation: bool = True,
598+
detailed_validation: bool = True,
599599
):
600600
"""Generate a specialized unstructure function for a mapping."""
601601
fn_name = "structure_mapping"
@@ -656,7 +656,7 @@ def make_mapping_structure_fn(
656656
# No args, it's a bare dict.
657657
lines.append(" res = dict(mapping)")
658658
else:
659-
if extended_validation:
659+
if detailed_validation:
660660
globs["enumerate"] = enumerate
661661
globs["IterableValidationError"] = IterableValidationError
662662
lines.append(" res = {}; errors = []")

tests/metadata/test_genconverter.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,11 @@
3434
@given(
3535
simple_typed_classes() | simple_typed_dataclasses(), unstructure_strats, booleans()
3636
)
37-
def test_simple_roundtrip(cls_and_vals, strat, extended_validation):
37+
def test_simple_roundtrip(cls_and_vals, strat, detailed_validation):
3838
"""
3939
Simple classes with metadata can be unstructured and restructured.
4040
"""
41-
converter = Converter(unstruct_strat=strat, extended_validation=extended_validation)
41+
converter = Converter(unstruct_strat=strat, detailed_validation=detailed_validation)
4242
cl, vals = cls_and_vals
4343
inst = cl(*vals)
4444
unstructured = converter.unstructure(inst)

tests/test_gen.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ def test_structure_linecache():
1515
class A:
1616
a: int
1717

18-
c = GenConverter(extended_validation=False)
18+
c = GenConverter(detailed_validation=False)
1919
try:
2020
c.structure({"a": "test"}, A)
2121
except ValueError:

tests/test_generics.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,10 @@ def test_able_to_structure_generics(converter: Converter, t, t2, result):
5959
("t", "result"),
6060
((int, GenericCols(1, [2], {"3": 3})), (str, GenericCols("1", ["2"], {"3": "3"}))),
6161
)
62-
@pytest.mark.parametrize("extended_validation", [True, False])
63-
def test_structure_generics_with_cols(t, result, extended_validation):
62+
@pytest.mark.parametrize("detailed_validation", [True, False])
63+
def test_structure_generics_with_cols(t, result, detailed_validation):
6464
raw = asdict(result)
65-
res = GenConverter(extended_validation=extended_validation).structure(
65+
res = GenConverter(detailed_validation=detailed_validation).structure(
6666
raw, GenericCols[t]
6767
)
6868

tests/test_structure.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -209,9 +209,9 @@ def test_structuring_optional_primitives(primitive_and_type):
209209

210210

211211
@given(lists_of_primitives().filter(lambda lp: not is_bare(lp[1])), booleans())
212-
def test_structuring_lists_of_opt(list_and_type, extended_validation: bool):
212+
def test_structuring_lists_of_opt(list_and_type, detailed_validation: bool):
213213
"""Test structuring lists of Optional primitive types."""
214-
converter = Converter(extended_validation=extended_validation)
214+
converter = Converter(detailed_validation=detailed_validation)
215215
l, t = list_and_type
216216

217217
l.append(None)
@@ -226,7 +226,7 @@ def test_structuring_lists_of_opt(list_and_type, extended_validation: bool):
226226
if not is_bare(t) and (args[0] not in (Any, str) and not is_optional):
227227
with raises(
228228
(TypeError, ValueError)
229-
if not extended_validation
229+
if not detailed_validation
230230
else IterableValidationError
231231
):
232232
converter.structure(l, t)

tests/test_validation.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
def test_class_validation():
1313
"""Proper class validation errors are raised when structuring."""
14-
c = GenConverter(extended_validation=True)
14+
c = GenConverter(detailed_validation=True)
1515

1616
@define
1717
class Test:
@@ -39,7 +39,7 @@ class Test:
3939

4040
def test_list_validation():
4141
"""Proper validation errors are raised structuring lists."""
42-
c = GenConverter(extended_validation=True)
42+
c = GenConverter(detailed_validation=True)
4343

4444
with pytest.raises(IterableValidationError) as exc:
4545
c.structure(["1", 2, "a", 3.0, "c"], List[int])
@@ -57,7 +57,7 @@ def test_list_validation():
5757

5858
def test_mapping_validation():
5959
"""Proper validation errors are raised structuring mappings."""
60-
c = GenConverter(extended_validation=True)
60+
c = GenConverter(detailed_validation=True)
6161

6262
with pytest.raises(IterableValidationError) as exc:
6363
c.structure({"1": 1, "2": "b", "c": 3}, Dict[int, int])
@@ -75,7 +75,7 @@ def test_mapping_validation():
7575

7676
def test_set_validation():
7777
"""Proper validation errors are raised structuring sets."""
78-
c = GenConverter(extended_validation=True)
78+
c = GenConverter(detailed_validation=True)
7979

8080
with pytest.raises(IterableValidationError) as exc:
8181
c.structure({"1", 2, "a"}, Set[int])
@@ -88,7 +88,7 @@ def test_set_validation():
8888

8989
def test_frozenset_validation():
9090
"""Proper validation errors are raised structuring frozensets."""
91-
c = GenConverter(extended_validation=True)
91+
c = GenConverter(detailed_validation=True)
9292

9393
with pytest.raises(IterableValidationError) as exc:
9494
c.structure({"1", 2, "a"}, FrozenSet[int])
@@ -101,7 +101,7 @@ def test_frozenset_validation():
101101

102102
def test_homo_tuple_validation():
103103
"""Proper validation errors are raised structuring homogenous tuples."""
104-
c = GenConverter(extended_validation=True)
104+
c = GenConverter(detailed_validation=True)
105105

106106
with pytest.raises(IterableValidationError) as exc:
107107
c.structure(["1", 2, "a"], Tuple[int, ...])
@@ -117,7 +117,7 @@ def test_homo_tuple_validation():
117117

118118
def test_hetero_tuple_validation():
119119
"""Proper validation errors are raised structuring heterogenous tuples."""
120-
c = GenConverter(extended_validation=True)
120+
c = GenConverter(detailed_validation=True)
121121

122122
with pytest.raises(IterableValidationError) as exc:
123123
c.structure(["1", 2, "a"], Tuple[int, int, int])

0 commit comments

Comments
 (0)