File tree 2 files changed +23
-2
lines changed
2 files changed +23
-2
lines changed Original file line number Diff line number Diff line change 7
7
- Fix TypedDicts with periods in their field names.
8
8
([ #376 ] ( https://github.com/python-attrs/cattrs/issues/376 ) [ #377 ] ( https://github.com/python-attrs/cattrs/pull/377 ) )
9
9
- 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 ) )
11
11
- Fix ` format_exception ` and ` transform_error ` type annotations.
12
12
13
13
## 23.1.2 (2023-06-02)
Original file line number Diff line number Diff line change 1
- from typing import NewType
1
+ from typing import NewType , Optional
2
2
3
+ import pytest
3
4
from attrs import define
4
5
6
+ from cattrs ._compat import is_py310_plus
7
+
5
8
6
9
def test_newtype_optionals (genconverter ):
7
10
"""Newtype optionals should work."""
8
11
Foo = NewType ("Foo" , str )
9
12
10
13
genconverter .register_unstructure_hook (Foo , lambda v : v .replace ("foo" , "bar" ))
11
14
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
+
12
33
@define
13
34
class ModelWithFoo :
14
35
total_foo : Foo
You can’t perform that action at this time.
0 commit comments