I have a project where user need to use Annotated[str, MyType()] a lot. In order to simplify it, I try to create a generic type
T = TypeVar('T')CustomUrl = Annotated[str, T]But I get this error when I try to use it this way:
CustomUrl[MyData()]# TypeError: typing.Annotated[str, ~T] is not a generic classI have no idea why this happens, and it works well when I reverse the order to
CustomUrl[T, str]But that's not what I want. Is there any way to make it work?
My use case is to have user declare a dataclass and it will generate a UI schema automatically, for example
# pseudo code@dataclassclass TaskArgs: input_text: Annotated[str, {'multiline': True}] input_file: Annotated[str, {'multifiles': True}]Update
I have try the following answer:
from typing import Annotated, TypeVarT = TypeVar('T')class CustomeUrl(Annotated[str, T]): ...c: CustomeUrl = 's'The problem is the last expression raise the error:
Expression of type "Literal['s']" is incompatible with declared type "CustomeUrl""Literal['s']" is incompatible with "CustomeUrl"The reason I choose Annotated is to ensure the type check is valid so I don't think it solves my issue.