Code: Select all
T = TypeVar('T')
CustomUrl = Annotated[str, T]
Code: Select all
CustomUrl[MyData()]
# TypeError: typing.Annotated[str, ~T] is not a generic class
Code: Select all
CustomUrl[T, str]
Mein Anwendungsfall besteht darin, dass der Benutzer eine Datenklasse deklariert und automatisch ein UI-Schema generiert wird, zum Beispiel
< pre class="lang-py Prettyprint-override">
Code: Select all
# pseudo code
@dataclass
class TaskArgs:
input_text: Annotated[str, {'multiline': True}]
input_file: Annotated[str, {'multifiles': True}]
Ich habe die folgende Antwort ausprobiert:
Code: Select all
from typing import Annotated, TypeVar
T = TypeVar('T')
class CustomeUrl(Annotated[str, T]):
...
c: CustomeUrl = 's'
Code: Select all
Expression of type "Literal['s']" is incompatible with declared type "CustomeUrl"
"Literal['s']" is incompatible with "CustomeUrl"