microsoft / microsoft/pyright

"TypeAliasType" not assignable to "TypeForm[Any]" for dict keys, but same expression works inline

Open
#11,301 0 comments 0 reactions 0 assignees View on GitHub
bug
Dominant language
Python
Stars
15.6k
Forks
1.8k
Avg merge
12h 13m
Merged PRs (30d)
52

Description

This is probably the easiest way to explain it:
```python
from typing import Any
from typing_extensions import TypeForm

mydict: dict[TypeForm[Any], str] = {}

type IntType = type[int]

mydict[IntType] # error: "TypeAliasType" is not assignable to "TypeForm[Any]"
mydict[type[int]] # no error
some_val: TypeForm[Any] = IntType # no error
```
[Example in Pyright Playground](https://pyright-play.net/?pythonVersion=3.14&pythonPlatform=Linux&enableExperimentalFeatures=true&code=GYJw9gtgBALgngBwJYDsDmUkQWEMoCCKcAUKJLIqmgPoCmAHjHSgM5Jhuba74AqiOgDFcEEiQhwAJkgDGMAFxQZ8gNoCEw0aqJwAugBoorGCD1QAvFADeAX3HxNUAJIoYGupcqbVqGHvFJFRhVV3dBcwBiKDoQcBAlACIPAgAbJABDVg9EzFYoFDB8LPY0FAyAI1TPGDAoZMEREAgdYj1EiWk5EMc6Xzc9KIK62PiSVkg6GgA3DNSlDyaW3XMrMI8oaMKYuNwgA)

I know TypeForm is still experimental, so this may be expected … or it may simply not be codified when mixing TypeForm rules, dict key invariance rules and type statement TypeAliasTypes. My read from the docs on type statements was that "the value of the type alias is evaluated in the annotation scope" meant TypeAliasTypes should be valid TypeForm objects (which they are, in the `some_val` case above, but that may be a naive interpretation.

PS: very much appreciate your work on pyright & python typing, thank you

Contributor guide

Open the contributing guide

Research direction

Reproduce the discrepancy in the linked Pyright Playground, comparing the dict-key expression with the direct TypeForm assignment. Trace Pyright’s handling of TypeForm, TypeAliasType, and dict-key assignability; done means the two cases consistently follow the documented TypeForm rules or the behavior is clarified as expected.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.