Skip to content

Commit 0e3995e

Browse files
committed
Rework test
1 parent 6626c0b commit 0e3995e

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

HISTORY.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
- Fix TypedDicts with periods in their field names.
88
([#376](https://github.com/python-attrs/cattrs/issues/376) [#377](https://github.com/python-attrs/cattrs/pull/377))
99
- Optimize and improve unstructuring of `Optional` (unions of one type and `None`).
10-
([#380](https://github.com/python-attrs/cattrs/issues/380))
10+
([#380](https://github.com/python-attrs/cattrs/issues/380) [#381](https://github.com/python-attrs/cattrs/pull/381))
1111
- Fix `format_exception` and `transform_error` type annotations.
1212

1313
## 23.1.2 (2023-06-02)

tests/test_optionals.py

+22-1
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,35 @@
1-
from typing import NewType
1+
from typing import NewType, Optional
22

3+
import pytest
34
from attrs import define
45

6+
from cattrs._compat import is_py310_plus
7+
58

69
def test_newtype_optionals(genconverter):
710
"""Newtype optionals should work."""
811
Foo = NewType("Foo", str)
912

1013
genconverter.register_unstructure_hook(Foo, lambda v: v.replace("foo", "bar"))
1114

15+
@define
16+
class ModelWithFoo:
17+
total_foo: Foo
18+
maybe_foo: Optional[Foo]
19+
20+
assert genconverter.unstructure(ModelWithFoo(Foo("foo"), Foo("is it a foo?"))) == {
21+
"total_foo": "bar",
22+
"maybe_foo": "is it a bar?",
23+
}
24+
25+
26+
@pytest.mark.skipif(not is_py310_plus, reason="3.10+ union syntax")
27+
def test_newtype_modern_optionals(genconverter):
28+
"""Newtype optionals should work."""
29+
Foo = NewType("Foo", str)
30+
31+
genconverter.register_unstructure_hook(Foo, lambda v: v.replace("foo", "bar"))
32+
1233
@define
1334
class ModelWithFoo:
1435
total_foo: Foo

0 commit comments

Comments
 (0)