@@ -267,24 +267,20 @@ def test_nested_roundtrip_tuple(cls_and_vals, omit_if_default: bool):
267267 assert inst == converter .structure (unstructured , cl )
268268
269269
270- @settings (suppress_health_check = [HealthCheck .filter_too_much , HealthCheck . too_slow ])
270+ @settings (suppress_health_check = [HealthCheck .too_slow ])
271271@given (
272- simple_typed_classes (defaults = False , newtypes = False , allow_nan = False ),
273- simple_typed_classes (defaults = False , newtypes = False , allow_nan = False ),
274- unstructure_strats ,
272+ simple_typed_classes (defaults = False , newtypes = False , allow_nan = False , min_attrs = 2 ),
273+ simple_typed_classes (defaults = False , newtypes = False , allow_nan = False , min_attrs = 1 ),
275274)
276- def test_union_field_roundtrip (cl_and_vals_a , cl_and_vals_b , strat ):
275+ def test_union_field_roundtrip_dict (cl_and_vals_a , cl_and_vals_b ):
277276 """
278277 Classes with union fields can be unstructured and structured.
279278 """
280- converter = Converter (unstruct_strat = strat )
279+ converter = Converter ()
281280 cl_a , vals_a , kwargs_a = cl_and_vals_a
282281 cl_b , _ , _ = cl_and_vals_b
283- assume (strat is UnstructureStrategy .AS_DICT or not kwargs_a )
284282 a_field_names = {a .name for a in fields (cl_a )}
285283 b_field_names = {a .name for a in fields (cl_b )}
286- assume (a_field_names )
287- assume (b_field_names )
288284
289285 common_names = a_field_names & b_field_names
290286 assume (len (a_field_names ) > len (common_names ))
@@ -295,20 +291,44 @@ class C:
295291
296292 inst = C (a = cl_a (* vals_a , ** kwargs_a ))
297293
298- if strat is UnstructureStrategy .AS_DICT :
299- unstructured = converter .unstructure (inst )
300- assert inst == converter .structure (converter .unstructure (unstructured ), C )
301- else :
302- # Our disambiguation functions only support dictionaries for now.
303- with pytest .raises (ValueError ):
304- converter .structure (converter .unstructure (inst ), C )
294+ unstructured = converter .unstructure (inst )
295+ assert inst == converter .structure (converter .unstructure (unstructured ), C )
305296
306- def handler (obj , _ ):
307- return converter .structure (obj , cl_a )
308297
309- converter .register_structure_hook (Union [cl_a , cl_b ], handler )
310- unstructured = converter .unstructure (inst )
311- assert inst == converter .structure (unstructured , C )
298+ @settings (suppress_health_check = [HealthCheck .too_slow ])
299+ @given (
300+ simple_typed_classes (
301+ defaults = False , newtypes = False , allow_nan = False , kw_only = False , min_attrs = 2
302+ ),
303+ simple_typed_classes (
304+ defaults = False , newtypes = False , allow_nan = False , kw_only = False , min_attrs = 1
305+ ),
306+ )
307+ def test_union_field_roundtrip_tuple (cl_and_vals_a , cl_and_vals_b ):
308+ """
309+ Classes with union fields can be unstructured and structured.
310+ """
311+ converter = Converter (unstruct_strat = UnstructureStrategy .AS_TUPLE )
312+ cl_a , vals_a , _ = cl_and_vals_a
313+ cl_b , _ , _ = cl_and_vals_b
314+
315+ @define
316+ class C :
317+ a : Union [cl_a , cl_b ]
318+
319+ inst = C (a = cl_a (* vals_a ))
320+
321+ # Our disambiguation functions only support dictionaries for now.
322+ raw = converter .unstructure (inst )
323+ with pytest .raises (StructureHandlerNotFoundError ):
324+ converter .structure (raw , C )
325+
326+ def handler (obj , _ ):
327+ return converter .structure (obj , cl_a )
328+
329+ converter .register_structure_hook (Union [cl_a , cl_b ], handler )
330+ unstructured = converter .unstructure (inst )
331+ assert inst == converter .structure (unstructured , C )
312332
313333
314334@pytest .mark .skipif (not is_py310_plus , reason = "3.10+ union syntax" )
0 commit comments