22
33import os
44import inspect
5- from typing import TYPE_CHECKING , Any , Type , Union , Generic , TypeVar , Callable , cast
5+ from typing import TYPE_CHECKING , Any , Type , Union , Generic , TypeVar , Callable , Optional , cast
66from datetime import date , datetime
77from typing_extensions import (
8+ List ,
89 Unpack ,
910 Literal ,
1011 ClassVar ,
@@ -367,7 +368,7 @@ def _construct_field(value: object, field: FieldInfo, key: str) -> object:
367368 if type_ is None :
368369 raise RuntimeError (f"Unexpected field type is None for { key } " )
369370
370- return construct_type (value = value , type_ = type_ )
371+ return construct_type (value = value , type_ = type_ , metadata = getattr ( field , "metadata" , None ) )
371372
372373
373374def is_basemodel (type_ : type ) -> bool :
@@ -421,7 +422,7 @@ def construct_type_unchecked(*, value: object, type_: type[_T]) -> _T:
421422 return cast (_T , construct_type (value = value , type_ = type_ ))
422423
423424
424- def construct_type (* , value : object , type_ : object ) -> object :
425+ def construct_type (* , value : object , type_ : object , metadata : Optional [ List [ Any ]] = None ) -> object :
425426 """Loose coercion to the expected type with construction of nested values.
426427
427428 If the given value does not match the expected type then it is returned as-is.
@@ -439,8 +440,10 @@ def construct_type(*, value: object, type_: object) -> object:
439440 type_ = type_ .__value__ # type: ignore[unreachable]
440441
441442 # unwrap `Annotated[T, ...]` -> `T`
442- if is_annotated_type (type_ ):
443- meta : tuple [Any , ...] = get_args (type_ )[1 :]
443+ if metadata is not None :
444+ meta : tuple [Any , ...] = tuple (metadata )
445+ elif is_annotated_type (type_ ):
446+ meta = get_args (type_ )[1 :]
444447 type_ = extract_type_arg (type_ , 0 )
445448 else :
446449 meta = tuple ()
0 commit comments