Skip to content

assignability failure with generic PEP 695 alias as type context #2682

@carljm

Description

@carljm

This emits a bogus error:

type MaybeList[T] = list[T] | T

def test[X: int](items: list[X]) -> list[X]:
    a: MaybeList[str | int] = list(items) #
    return items

https://play.ty.dev/1757f2f8-b251-4a94-96d2-51ff3743f6d9

But this version (with a PEP 613 alias, or a legacy type alias) works:

from typing import TypeVar, TypeAlias

T = TypeVar("T")

MaybeList: TypeAlias = list[T] | T

def test[X: int](items: list[X]) -> list[X]:
    a: MaybeList[str | int] = list(items)
    return items

Originally posted by @DetachHead in #2314

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions