Skip to content

Bug in alias annotations #7184

@hjalmarlucius

Description

@hjalmarlucius

Environment data

  • pyright 1.1.349
  • python version 3.11.6

Code snippet

from typing import Generic
from typing import Literal
from typing import TypeAlias
from typing import TypeVar

A_float: TypeAlias = Literal["float32", "float64"]
A_numeric: TypeAlias = Literal["int32", "int64"] | A_float
Tdtype = TypeVar("Tdtype", bound=A_numeric)


class TypedArray(Generic[Tdtype]):
    pass


Tdtype_float = TypeVar("Tdtype_float", bound=A_float)

# WORKS
OptionalFloatArray: TypeAlias = TypedArray[Tdtype_float] | None

# FAILS but equivalent
OptionalArray: TypeAlias = TypedArray[Tdtype] | None
OptionalFloatArray_: TypeAlias = OptionalArray[Tdtype_float]
# error: Could not specialize type "OptionalArray[Tdtype@OptionalArray]"
#   "Literal['float32']" cannot be assigned to type "Literal['int32']"
#   "Literal['float64']" cannot be assigned to type "Literal['int64']"

Error description

Valid aliases with TypeVars raise error if Literal bounds are narrowed.

  • Seems to only happen when using Literals in both bounds
  • Does not happen if there is only one Literal in A_float
  • Error description indicates that the bug is caused by doing subtype comparison in the wrong order

Metadata

Metadata

Assignees

No one assigned

    Labels

    addressed in next versionIssue is fixed and will appear in next published versionbugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions