Skip to content

(🎁) No warning about TypeAlias to bare TypeVar when using TypeAlias #13376

Open
@KotlinIsland

Description

@KotlinIsland
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

playground

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

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions