1111 ForwardRef ,
1212 Generic ,
1313 Literal ,
14+ Sequence ,
1415 TypeVar ,
1516 Union ,
1617 _SpecialForm , # pyright: ignore[reportPrivateUsage]
@@ -43,18 +44,19 @@ class TypeView(Generic[T]):
4344 "_wrappers" : "A set of wrapper types that were removed from the annotation." ,
4445 }
4546
46- def __init__ (self , annotation : T ) -> None :
47+ def __init__ (self , annotation : T , * , metadata : Sequence [ Any ] = () ) -> None :
4748 """Initialize ParsedType.
4849
4950 Args:
5051 annotation: The type annotation. This should be extracted from the return of
5152 ``get_type_hints(..., include_extras=True)`` so that forward references are resolved and recursive
5253 ``Annotated`` types are flattened.
54+ metadata: Additional metadata to associate with the annotation.
5355
5456 Returns:
5557 ParsedType
5658 """
57- unwrapped , metadata , wrappers = unwrap_annotation (annotation )
59+ unwrapped , annotation_metadata , wrappers = unwrap_annotation (annotation )
5860 origin = get_origin (unwrapped )
5961
6062 args : tuple [Any , ...] = () if origin is abc .Callable else get_args (unwrapped ) # pyright: ignore
@@ -64,9 +66,9 @@ def __init__(self, annotation: T) -> None:
6466 self .origin : Final [Any ] = origin
6567 self .fallback_origin : Final [Any ] = origin or unwrapped
6668 self .args : Final [tuple [Any , ...]] = args
67- self .metadata : Final = metadata
69+ self .metadata : Final = ( * annotation_metadata , * metadata )
6870 self ._wrappers : Final = wrappers
69- self .inner_types : Final = tuple (TypeView (arg ) for arg in args )
71+ self .inner_types : Final = tuple (TypeView (arg , metadata = self . metadata ) for arg in args )
7072
7173 def __eq__ (self , other : object ) -> bool :
7274 if not isinstance (other , TypeView ):
@@ -285,7 +287,7 @@ def strip_optional(self) -> TypeView[Any]:
285287
286288 args = tuple (a for a in self .args if a is not NoneType )
287289 non_optional = Union [args ] # type: ignore[valid-type]
288- return TypeView (non_optional )
290+ return TypeView (non_optional , metadata = self . metadata )
289291
290292 def strip_type_alias (self ) -> TypeView [Any ]:
291293 """Remove the type alias from a `type Type = T` type alias.
@@ -297,7 +299,7 @@ def strip_type_alias(self) -> TypeView[Any]:
297299 """
298300 if not self .is_type_alias :
299301 return self
300- return TypeView (self .annotation .__value__ )
302+ return TypeView (self .annotation .__value__ , metadata = self . metadata )
301303
302304
303305def _is_typing_extensins_type_alias (type_view : TypeView [Any ]) -> bool :
0 commit comments