is there a way to extend Annotated type definitions without having to explicitly dig around in the original type's internals?
In this manner, your code stays declarative as you add new annotations without nesting and maintain the base type.
Script:
from typing import get_args, TypeVar, Tuple, Any
def extend_annotated(base: Any, *annotations: Any) -> Any:
args = get_args(base)
base_type = args[0]
# Combine existing annotations with new ones
combined_annotations = args[1:] + list(annotations)
return Annotated[base_type, *combined_annotations]
You can do like this:
ShortAlpha = extend_annotated(Alpha, Field(max_length=5))