Skip to content

invalid-type-form on TypeAlias with stringified generic annotation #2770

@dr-carlos

Description

@dr-carlos

Summary

When a PEP 613 TypeAlias is created with a generic whose parameter is a stringified type annotation, ty mistakenly emits a invalid-type-form diagnostic and infers Unknown.

The correct behaviour is present for PEP 695 type aliases, or when non-string or fully string annotations are used.

from typing import TYPE_CHECKING, TypeAlias, Optional, reveal_type

class A: ...

type AliasA1 = Optional[A] # Okay
AliasA2: TypeAlias = Optional[A] # Okay

type AliasB1 = Optional["A"] # Okay
AliasB2: TypeAlias = Optional["A"] # Not okay

type AliasC1 = "Optional[A]" # Okay
AliasC2: TypeAlias = "Optional[A]" # Okay

def _(a1: AliasA1, a2: AliasA2, b1: AliasB1, b2: AliasB2, c1: AliasC1, c2: AliasC2) -> None: ...
#                                                ^^^^^^^
# Variable of type `Literal["A"]` is not allowed in a type expression (invalid-type-form) [Ln 14, Col 50]

https://play.ty.dev/97be9fea-9c02-43f1-ab8d-f1ae7235a1fd

Version

ty 0.0.15

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions