|
16 | 16 | Union, |
17 | 17 | ) |
18 | 18 |
|
| 19 | +import attrs |
19 | 20 | import pytest |
20 | 21 | from attrs import Factory, define, field, fields, has, make_class |
21 | 22 | from hypothesis import assume, given |
@@ -339,6 +340,31 @@ class C: |
339 | 340 | assert inst == converter.structure(unstructured, C) |
340 | 341 |
|
341 | 342 |
|
| 343 | +@given(simple_typed_classes(defaults="always", allow_nan=False)) |
| 344 | +def test_omit_default_with_attrs_converter_roundtrip(cl_and_vals): |
| 345 | + """ |
| 346 | + Omit default with attrs' converter on the converter works. |
| 347 | + """ |
| 348 | + converter = Converter(omit_if_default=True) |
| 349 | + cl, vals, kwargs = cl_and_vals |
| 350 | + |
| 351 | + @define |
| 352 | + class C: |
| 353 | + a1: int = field(default="1", converter=int) |
| 354 | + a2: int = field(default="1", converter=attrs.Converter(int)) |
| 355 | + c: cl = Factory(lambda: cl(*vals, **kwargs)) |
| 356 | + |
| 357 | + inst = C() |
| 358 | + unstructured = converter.unstructure(inst) |
| 359 | + assert unstructured == {} |
| 360 | + assert inst == converter.structure(unstructured, C) |
| 361 | + |
| 362 | + inst = C(0, 0) |
| 363 | + unstructured = converter.unstructure(inst) |
| 364 | + assert unstructured == {"a1": 0, "a2": 0} |
| 365 | + assert inst == converter.structure(unstructured, C) |
| 366 | + |
| 367 | + |
342 | 368 | def test_dict_roundtrip_with_alias(): |
343 | 369 | """ |
344 | 370 | A class with an aliased attribute can be unstructured and structured. |
|
0 commit comments