@@ -149,16 +149,14 @@ def handler(obj, _):
149149 simple_typed_classes (
150150 defaults = "never" , newtypes = False , allow_nan = False , min_attrs = 1
151151 ),
152- unstructure_strats ,
153152)
154- def test_310_union_field_roundtrip (cl_and_vals_a , cl_and_vals_b , strat ):
153+ def test_310_union_field_roundtrip_dict (cl_and_vals_a , cl_and_vals_b ):
155154 """
156155 Classes with union fields can be unstructured and structured.
157156 """
158- converter = BaseConverter (unstruct_strat = strat )
157+ converter = BaseConverter ()
159158 cl_a , vals_a , kwargs_a = cl_and_vals_a
160159 cl_b , _ , _ = cl_and_vals_b
161- assume (strat is UnstructureStrategy .AS_DICT or not kwargs_a )
162160 a_field_names = {a .name for a in fields (cl_a )}
163161 b_field_names = {a .name for a in fields (cl_b )}
164162
@@ -171,18 +169,47 @@ class C:
171169
172170 inst = C (a = cl_a (* vals_a , ** kwargs_a ))
173171
174- if strat is UnstructureStrategy .AS_DICT :
175- assert inst == converter .structure (converter .unstructure (inst ), C )
176- else :
177- # Our disambiguation functions only support dictionaries for now.
178- with pytest .raises (StructureHandlerNotFoundError ):
179- converter .structure (converter .unstructure (inst ), C )
172+ assert inst == converter .structure (converter .unstructure (inst ), C )
180173
181- def handler (obj , _ ):
182- return converter .structure (obj , cl_a )
183174
184- converter .register_structure_hook (cl_a | cl_b , handler )
185- assert inst == converter .structure (converter .unstructure (inst ), C )
175+ @pytest .mark .skipif (not is_py310_plus , reason = "3.10+ union syntax" )
176+ @settings (suppress_health_check = [HealthCheck .too_slow ])
177+ @given (
178+ simple_typed_classes (
179+ defaults = "never" , newtypes = False , allow_nan = False , min_attrs = 1 , kw_only = "never"
180+ ),
181+ simple_typed_classes (
182+ defaults = "never" , newtypes = False , allow_nan = False , min_attrs = 1 , kw_only = "never"
183+ ),
184+ )
185+ def test_310_union_field_roundtrip_tuple (cl_and_vals_a , cl_and_vals_b ):
186+ """
187+ Classes with union fields can be unstructured and structured.
188+ """
189+ converter = BaseConverter (unstruct_strat = UnstructureStrategy .AS_TUPLE )
190+ cl_a , vals_a , kwargs_a = cl_and_vals_a
191+ cl_b , _ , _ = cl_and_vals_b
192+ a_field_names = {a .name for a in fields (cl_a )}
193+ b_field_names = {a .name for a in fields (cl_b )}
194+
195+ common_names = a_field_names & b_field_names
196+ assume (len (a_field_names ) > len (common_names ))
197+
198+ @define
199+ class C :
200+ a : cl_a | cl_b
201+
202+ inst = C (a = cl_a (* vals_a , ** kwargs_a ))
203+
204+ # Our disambiguation functions only support dictionaries for now.
205+ with pytest .raises (StructureHandlerNotFoundError ):
206+ converter .structure (converter .unstructure (inst ), C )
207+
208+ def handler (obj , _ ):
209+ return converter .structure (obj , cl_a )
210+
211+ converter .register_structure_hook (cl_a | cl_b , handler )
212+ assert inst == converter .structure (converter .unstructure (inst ), C )
186213
187214
188215@given (simple_typed_classes (defaults = "never" , newtypes = False , allow_nan = False ))
0 commit comments