4747 is_mutable_set ,
4848 is_optional ,
4949 is_protocol ,
50+ is_subclass ,
5051 is_tuple ,
5152 is_typeddict ,
5253 is_union_type ,
7677 UnstructuredValue ,
7778 UnstructureHook ,
7879)
80+ from .enums import enum_structure_factory , enum_unstructure_factory
7981from .errors import (
8082 IterableValidationError ,
8183 IterableValidationNote ,
@@ -246,12 +248,12 @@ def __init__(
246248 lambda t : self .get_unstructure_hook (get_type_alias_base (t )),
247249 True ,
248250 ),
249- (is_literal_containing_enums , self .unstructure ),
250251 (is_mapping , self ._unstructure_mapping ),
251252 (is_sequence , self ._unstructure_seq ),
252253 (is_mutable_set , self ._unstructure_seq ),
253254 (is_frozenset , self ._unstructure_seq ),
254- (lambda t : issubclass (t , Enum ), self ._unstructure_enum ),
255+ (is_literal_containing_enums , self .unstructure ),
256+ (lambda t : is_subclass (t , Enum ), enum_unstructure_factory , "extended" ),
255257 (has , self ._unstructure_attrs ),
256258 (is_union_type , self ._unstructure_union ),
257259 (lambda t : t in ANIES , self .unstructure ),
@@ -298,6 +300,7 @@ def __init__(
298300 self ._union_struct_registry .__getitem__ ,
299301 True ,
300302 ),
303+ (lambda t : is_subclass (t , Enum ), enum_structure_factory , "extended" ),
301304 (has , self ._structure_attrs ),
302305 ]
303306 )
@@ -308,7 +311,6 @@ def __init__(
308311 (bytes , self ._structure_call ),
309312 (int , self ._structure_call ),
310313 (float , self ._structure_call ),
311- (Enum , self ._structure_enum ),
312314 (Path , self ._structure_call ),
313315 ]
314316 )
@@ -630,12 +632,6 @@ def unstructure_attrs_astuple(self, obj: Any) -> tuple[Any, ...]:
630632 res .append (dispatch (a .type or v .__class__ )(v ))
631633 return tuple (res )
632634
633- def _unstructure_enum (self , obj : Enum ) -> Any :
634- """Convert an enum to its unstructured value."""
635- if "_value_" in obj .__class__ .__annotations__ :
636- return self ._unstructure_func .dispatch (obj .value .__class__ )(obj .value )
637- return obj .value
638-
639635 def _unstructure_seq (self , seq : Sequence [T ]) -> Sequence [T ]:
640636 """Convert a sequence to primitive equivalents."""
641637 # We can reuse the sequence class, so tuples stay tuples.
@@ -715,15 +711,6 @@ def _structure_simple_literal(val, type):
715711 raise Exception (f"{ val } not in literal { type } " )
716712 return val
717713
718- def _structure_enum (self , val : Any , cl : type [Enum ]) -> Enum :
719- """Structure ``val`` if possible and return the enum it corresponds to.
720-
721- Uses type hints for the "_value_" attribute if they exist to structure
722- the enum values before returning the result."""
723- if "_value_" in cl .__annotations__ :
724- val = self .structure (val , cl .__annotations__ ["_value_" ])
725- return cl (val )
726-
727714 @staticmethod
728715 def _structure_enum_literal (val , type ):
729716 vals = {(x .value if isinstance (x , Enum ) else x ): x for x in type .__args__ }
0 commit comments