Skip to content

A TypeAlias of a TypeAliasType loses parameter order #1162

@jorenham

Description

@jorenham

Describe the Bug

from typing import TypeAlias, TypeAliasType, TypeVar

T1 = TypeVar("T1")
T2 = TypeVar("T2", bound=str | bytes)

Spam1 = TypeAliasType("Spam1", T2 | type[T1], type_params=(T1, T2))
Spam2: TypeAlias = Spam1[T1, T2]

x1: Spam1[int, str] = int  # ✔️ 
x2: Spam2[int, str] = int  # ❌ 

The reported errors show that the typar order "gets lost" in the Spam2 alias:

ERROR sandbox.py:10:5-20: Type `int` is not assignable to upper bound `bytes | str` of type variable `T2` [[bad-specialization](https://pyrefly.org/en/docs/error-kinds/#bad-specialization)]
ERROR sandbox.py:10:23-26: `type[int]` is not assignable to `int | type[str]` [[bad-assignment](https://pyrefly.org/en/docs/error-kinds/#bad-assignment)]

For context: This pattern is used in optype, where e.g. optype.numpy.ToArray1D (src) is a TypeAlias to a TypeAliasType called _To1D2 (src). This leads to a bunch of false positives and incorrect inferences when running pyrefly on scipy-stubs, which heavily relies on these optype.numpy types.

Sandbox Link

https://pyrefly.org/sandbox/?project=N4IgZglgNgpgziAXKOBDAdgEwEYHsAeAdAA4CeS4ATrgLYAEALqcROgOZ0Q3G6UN0AVZjACCUCKjgAaQcLES4Q4jBlKYANVSUAOul0CAjHQC8s5ZsoAKbSEM2AlPoBMJsxq3XbTmzLwBXLGM4Bko6AB86bFIGeEc9dABlYlQaI1M1eUk1TySUgx9BFwimZQBtQwBdGRKYAH1kyhS4Y0tDVSd7ONyaJ0Q3TLhXboNyg3aK3V18Az7h0tYGGWDKCtcFqd66bqd59EW6ZdXTdfQQKRA-Bmg4EnJEEABiOgBVK-EmOjAAgGMr3HQ4JMsDAwJ9eDRUAxaug-DRsDArPg%2Bgt7HQALQAPgOIUQujo%2BLolBgDD8lHQnxsADlYfDKH1gPgAL42XQgRnnVC-CAANxgADFoDAKGgsHgiGQ2UA&version=3.13

(Only applicable for extension issues) IDE Information

No response

Metadata

Metadata

Assignees

Type

No fields configured for Bug.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions