Open
Description
from typing import TypeVar, TypeAlias
T = TypeVar("T")
TAlias: TypeAlias = T
def foo(
t1: TAlias[int], # no error
t2: TAlias, # error: Missing type parameters for generic type "TAlias"
) -> None: ...
Treating it as a generic type and not as TypeVar
is incorrect as at runtime it is not subscriptable:
Traceback (most recent call last):
File "test.py", line 7, in <module>
t1: TAlias[int], # no error
TypeError: 'TypeVar' object is not subscriptable
Strangely, the correct error is reported when the TypeAlias
is omitted.
from typing import TypeVar, TypeAlias
T = TypeVar("T")
TAlias = T # error: Type variable "__main__.T" is invalid as target for type alias [misc]
Metadata
Metadata
Assignees
Projects
Milestone
Relationships
Development
No branches or pull requests
Activity
hauntsaninja commentedon Aug 11, 2022
I think this is "as designed". This is just a very dumb generic type alias
KotlinIsland commentedon Aug 11, 2022
@hauntsaninja I know, the issue is that it's a runtime error:
(OP has now been updated)
[-](🐞) `TypeAlias` to a bare `TypeVar` is treated as a generic type, not as as a `TypeVar`[/-][+]Warn about `TypeAlias` to bare `TypeVar`s[/+][-]Warn about `TypeAlias` to bare `TypeVar`s[/-][+](🎁) Warn about `TypeAlias` to bare `TypeVar`s[/+]sobolevn commentedon Aug 21, 2022
Also related #13449 (comment)
KotlinIsland commentedon Mar 3, 2023
Strangely, it correctly reports the error when the
TypeAlias
is ommitted.Can we have topic-runtime-semanticsmypy doesn't model runtime semantics correctly
?
TypeAlias
type #148242 remaining items