6565 IterableValidationNote ,
6666 StructureHandlerNotFoundError ,
6767)
68- from .fns import identity , raise_error
68+ from .fns import Predicate , identity , raise_error
6969from .gen import (
7070 AttributeOverride ,
7171 DictStructureFn ,
@@ -174,6 +174,7 @@ def __init__(
174174 self ._prefer_attrib_converters = prefer_attrib_converters
175175
176176 self .detailed_validation = detailed_validation
177+ self ._union_struct_registry : dict [Any , Callable [[Any , type [T ]], T ]] = {}
177178
178179 # Create a per-instance cache.
179180 if unstruct_strat is UnstructureStrategy .AS_DICT :
@@ -246,7 +247,8 @@ def __init__(
246247 (is_supported_union , self ._gen_attrs_union_structure , True ),
247248 (
248249 lambda t : is_union_type (t ) and t in self ._union_struct_registry ,
249- self ._structure_union ,
250+ self ._union_struct_registry .__getitem__ ,
251+ True ,
250252 ),
251253 (is_optional , self ._structure_optional ),
252254 (has , self ._structure_attrs ),
@@ -266,9 +268,6 @@ def __init__(
266268
267269 self ._dict_factory = dict_factory
268270
269- # Unions are instances now, not classes. We use different registries.
270- self ._union_struct_registry : dict [Any , Callable [[Any , type [T ]], T ]] = {}
271-
272271 self ._unstruct_copy_skip = self ._unstructure_func .get_num_fns ()
273272 self ._struct_copy_skip = self ._structure_func .get_num_fns ()
274273
@@ -330,7 +329,7 @@ def register_unstructure_hook(
330329 return None
331330
332331 def register_unstructure_hook_func (
333- self , check_func : Callable [[ Any ], bool ] , func : UnstructureHook
332+ self , check_func : Predicate , func : UnstructureHook
334333 ) -> None :
335334 """Register a class-to-primitive converter function for a class, using
336335 a function to check if it's a match.
@@ -339,25 +338,25 @@ def register_unstructure_hook_func(
339338
340339 @overload
341340 def register_unstructure_hook_factory (
342- self , predicate : Callable [[ Any ], bool ]
341+ self , predicate : Predicate
343342 ) -> Callable [[UnstructureHookFactory ], UnstructureHookFactory ]:
344343 ...
345344
346345 @overload
347346 def register_unstructure_hook_factory (
348- self , predicate : Callable [[ Any ], bool ]
347+ self , predicate : Predicate
349348 ) -> Callable [[ExtendedUnstructureHookFactory ], ExtendedUnstructureHookFactory ]:
350349 ...
351350
352351 @overload
353352 def register_unstructure_hook_factory (
354- self , predicate : Callable [[ Any ], bool ] , factory : UnstructureHookFactory
353+ self , predicate : Predicate , factory : UnstructureHookFactory
355354 ) -> UnstructureHookFactory :
356355 ...
357356
358357 @overload
359358 def register_unstructure_hook_factory (
360- self , predicate : Callable [[ Any ], bool ] , factory : ExtendedUnstructureHookFactory
359+ self , predicate : Predicate , factory : ExtendedUnstructureHookFactory
361360 ) -> ExtendedUnstructureHookFactory :
362361 ...
363362
@@ -473,7 +472,7 @@ def register_structure_hook(
473472 self ._structure_func .register_cls_list ([(cl , func )])
474473
475474 def register_structure_hook_func (
476- self , check_func : Callable [[ type [ T ]], bool ] , func : StructureHook
475+ self , check_func : Predicate , func : StructureHook
477476 ) -> None :
478477 """Register a class-to-primitive converter function for a class, using
479478 a function to check if it's a match.
@@ -482,25 +481,25 @@ def register_structure_hook_func(
482481
483482 @overload
484483 def register_structure_hook_factory (
485- self , predicate : Callable [[ Any , bool ]]
484+ self , predicate : Predicate
486485 ) -> Callable [[StructureHookFactory , StructureHookFactory ]]:
487486 ...
488487
489488 @overload
490489 def register_structure_hook_factory (
491- self , predicate : Callable [[ Any , bool ]]
490+ self , predicate : Predicate
492491 ) -> Callable [[ExtendedStructureHookFactory , ExtendedStructureHookFactory ]]:
493492 ...
494493
495494 @overload
496495 def register_structure_hook_factory (
497- self , predicate : Callable [[ Any ], bool ] , factory : StructureHookFactory
496+ self , predicate : Predicate , factory : StructureHookFactory
498497 ) -> StructureHookFactory :
499498 ...
500499
501500 @overload
502501 def register_structure_hook_factory (
503- self , predicate : Callable [[ Any ], bool ] , factory : ExtendedStructureHookFactory
502+ self , predicate : Predicate , factory : ExtendedStructureHookFactory
504503 ) -> ExtendedStructureHookFactory :
505504 ...
506505
@@ -903,11 +902,6 @@ def _structure_optional(self, obj, union):
903902 # We can't actually have a Union of a Union, so this is safe.
904903 return self ._structure_func .dispatch (other )(obj , other )
905904
906- def _structure_union (self , obj , union ):
907- """Deal with structuring a union."""
908- handler = self ._union_struct_registry [union ]
909- return handler (obj , union )
910-
911905 def _structure_tuple (self , obj : Any , tup : type [T ]) -> T :
912906 """Deal with structuring into a tuple."""
913907 tup_params = None if tup in (Tuple , tuple ) else tup .__args__
0 commit comments