@@ -356,73 +356,6 @@ def __init__(self, name: str, bases: Tuple[type, ...], attrs: Dict[str, Any]):
356356 value : Any = None
357357 required = None
358358 multi = None
359- if name in annotations :
360- # the field has a type annotation, so next we try to figure out
361- # what field type we can use
362- type_ = annotations [name ]
363- type_metadata = []
364- if isinstance (type_ , _AnnotatedAlias ):
365- type_metadata = type_ .__metadata__
366- type_ = type_ .__origin__
367- skip = False
368- required = True
369- multi = False
370- while hasattr (type_ , "__origin__" ):
371- if type_ .__origin__ == ClassVar :
372- skip = True
373- break
374- elif type_ .__origin__ == Mapped :
375- # M[type] -> extract the wrapped type
376- type_ = type_ .__args__ [0 ]
377- elif type_ .__origin__ == Union :
378- if len (type_ .__args__ ) == 2 and type_ .__args__ [1 ] is type (None ):
379- # Optional[type] -> mark instance as optional
380- required = False
381- type_ = type_ .__args__ [0 ]
382- else :
383- raise TypeError ("Unsupported union" )
384- elif type_ .__origin__ in [list , List ]:
385- # List[type] -> mark instance as multi
386- multi = True
387- required = False
388- type_ = type_ .__args__ [0 ]
389- else :
390- break
391- if skip or type_ == ClassVar :
392- # skip ClassVar attributes
393- continue
394- if type (type_ ) is UnionType :
395- # a union given with the pipe syntax
396- args = get_args (type_ )
397- if len (args ) == 2 and args [1 ] is type (None ):
398- required = False
399- type_ = type_ .__args__ [0 ]
400- else :
401- raise TypeError ("Unsupported union" )
402- field = None
403- field_args : List [Any ] = []
404- field_kwargs : Dict [str , Any ] = {}
405- if isinstance (type_ , type ) and issubclass (type_ , InnerDoc ):
406- # object or nested field
407- field = Nested if multi else Object
408- field_args = [type_ ]
409- elif type_ in self .type_annotation_map :
410- # use best field type for the type hint provided
411- field , field_kwargs = self .type_annotation_map [type_ ] # type: ignore[assignment]
412-
413- # if this field does not have a right-hand value, we look in the metadata
414- # of the annotation to see if we find it there
415- for md in type_metadata :
416- if isinstance (md , (_FieldMetadataDict , Field )):
417- attrs [name ] = md
418-
419- if field :
420- field_kwargs = {
421- "multi" : multi ,
422- "required" : required ,
423- ** field_kwargs ,
424- }
425- value = field (* field_args , ** field_kwargs )
426359
427360 if name in attrs :
428361 # this field has a right-side value, which can be field
@@ -448,6 +381,79 @@ def __init__(self, name: str, bases: Tuple[type, ...], attrs: Dict[str, Any]):
448381 if multi is not None :
449382 value ._multi = multi
450383
384+ if value is None and name in annotations :
385+ # the field has a type annotation, so next we try to figure out
386+ # what field type we can use
387+ type_ = annotations [name ]
388+ type_metadata = []
389+ if isinstance (type_ , _AnnotatedAlias ):
390+ type_metadata = type_ .__metadata__
391+ type_ = type_ .__origin__
392+ skip = False
393+ required = True
394+ multi = False
395+
396+ # if this field does not have a right-hand value, we look in the metadata
397+ # of the annotation to see if we find it there
398+ for md in type_metadata :
399+ if isinstance (md , (_FieldMetadataDict , Field )):
400+ attrs [name ] = md
401+ value = md
402+
403+ if value is None :
404+ while hasattr (type_ , "__origin__" ):
405+ if type_ .__origin__ == ClassVar :
406+ skip = True
407+ break
408+ elif type_ .__origin__ == Mapped :
409+ # M[type] -> extract the wrapped type
410+ type_ = type_ .__args__ [0 ]
411+ elif type_ .__origin__ == Union :
412+ if len (type_ .__args__ ) == 2 and type_ .__args__ [1 ] is type (
413+ None
414+ ):
415+ # Optional[type] -> mark instance as optional
416+ required = False
417+ type_ = type_ .__args__ [0 ]
418+ else :
419+ raise TypeError ("Unsupported union" )
420+ elif type_ .__origin__ in [list , List ]:
421+ # List[type] -> mark instance as multi
422+ multi = True
423+ required = False
424+ type_ = type_ .__args__ [0 ]
425+ else :
426+ break
427+ if skip or type_ == ClassVar :
428+ # skip ClassVar attributes
429+ continue
430+ if type (type_ ) is UnionType :
431+ # a union given with the pipe syntax
432+ args = get_args (type_ )
433+ if len (args ) == 2 and args [1 ] is type (None ):
434+ required = False
435+ type_ = type_ .__args__ [0 ]
436+ else :
437+ raise TypeError ("Unsupported union" )
438+ field = None
439+ field_args : List [Any ] = []
440+ field_kwargs : Dict [str , Any ] = {}
441+ if isinstance (type_ , type ) and issubclass (type_ , InnerDoc ):
442+ # object or nested field
443+ field = Nested if multi else Object
444+ field_args = [type_ ]
445+ elif type_ in self .type_annotation_map :
446+ # use best field type for the type hint provided
447+ field , field_kwargs = self .type_annotation_map [type_ ] # type: ignore[assignment]
448+
449+ if field :
450+ field_kwargs = {
451+ "multi" : multi ,
452+ "required" : required ,
453+ ** field_kwargs ,
454+ }
455+ value = field (* field_args , ** field_kwargs )
456+
451457 if value is None :
452458 raise TypeError (f"Cannot map field { name } " )
453459
0 commit comments