@@ -242,36 +242,19 @@ class ClassWithLiteral:
242
242
converter .structure ({"literal_field" : 3 }, ClassWithLiteral )
243
243
244
244
245
- @pytest .mark .parametrize ("converter_type" , [BaseConverter , Converter ])
246
- def test_structure_fallback_to_attrib_converters (converter_type ):
247
- attrib_converter = Mock ()
248
- attrib_converter .side_effect = lambda val : str (val )
245
+ def test_structure_fallback_to_attrib_converters (converter ):
246
+ """`attrs` converters are called after cattrs processing."""
249
247
250
- def called_after_default_converter (val ):
251
- if not isinstance (val , int ):
252
- raise ValueError (
253
- "The 'int' conversion should have happened first by the built-in hooks"
254
- )
255
- return 42
256
-
257
- converter = converter_type ()
258
- cl = make_class (
259
- "HasConverter" ,
260
- {
261
- # non-built-in type with custom converter
262
- "ip" : field (type = Union [IPv4Address , IPv6Address ], converter = ip_address ),
263
- # attribute without type
264
- "x" : field (converter = attrib_converter ),
265
- # built-in types converters
266
- "z" : field (type = int , converter = called_after_default_converter ),
267
- },
268
- )
248
+ @define
249
+ class HasConverter :
250
+ ip : Union [IPv4Address , IPv6Address ] = field (converter = ip_address )
251
+ x = field (converter = lambda v : v + 1 )
252
+ z : int = field (converter = lambda _ : 42 )
269
253
270
- inst = converter .structure ({"ip" : "10.0.0.0" , "x" : 1 , "z" : "3" }, cl )
254
+ inst = converter .structure ({"ip" : "10.0.0.0" , "x" : 1 , "z" : "3" }, HasConverter )
271
255
272
256
assert inst .ip == IPv4Address ("10.0.0.0" )
273
- assert inst .x == "1"
274
- attrib_converter .assert_any_call (1 )
257
+ assert inst .x == 2
275
258
assert inst .z == 42
276
259
277
260
0 commit comments