How to accept Annotated types as well as normal types? #7849
-
Here's a minimal snippet: from typing import Annotated, TypeAlias
class MyClass: ...
def my_function(arg: type[MyClass]) -> None: ...
MyTypealias: TypeAlias = Annotated[MyClass, 1]
# works
my_function(MyClass)
# doesn't work
my_function(MyTypealias) The error:
I'm surprised, I thought that Annotated was supposed to be transparent wrt the type system. The issue is that Is there any way to make this work? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
What are you doing in |
Beta Was this translation helpful? Give feedback.
Annotated
is intended to be used in type expressions (i.e. type annotations). You're using it here in a value expression. See this issue for more details.What are you doing in
my_function
with the value you pass to it? The valueMyTypealias
is not a valid class object, somy_function
is not going to be able to use it as a normaltype
. For example, if there is a class variable defined onMyClass
, you won't be able to access it usingarg.my_class_var
.