microsoft / microsoft/pyright

TypeForm + TypeVar type inference

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

Description

**Describe the bug**
This seems like a bug in the TypeForm experimental feature, I just wanted to check whether I'm trying to do something inherently impossible or whether there's a missing link somewhere in the mechanism.

The bug is in linking the inferred type of an attribute of a generic class with its generic type parameter.

**Code or Screenshots**

Here's a comparison of behaviour with three APIs: just a type annotation, type annotation and setter function, and setter function. Although the type is correctly inferred to be T0 for the variable, when the class is specialised Pyright doesn't seem to pick it up.

```python
from typing import Generic

from typing_extensions import TypeForm, TypeVar, reveal_type

T0 = TypeVar("T0")
T1 = TypeVar("T1")

def func(t: TypeForm[T1]) -> T1: ...

class Class0(Generic[T0]):
attr: T0

def __init__(self, attr: T0) -> None:
super().__init__()

class Class1(Generic[T0]):
attr: T0 = func(T0)

def __init__(self, attr: T0) -> None:
super().__init__()

class Class2(Generic[T0]):
attr = func(T0)

def __init__(self, attr: T0) -> None:
super().__init__()

x0 = Class0(1)
x1 = Class1(1)
x2 = Class2(1)

reveal_type(x0.attr) # int
reveal_type(x1.attr) # int
reveal_type(x2.attr) # T0@Class2
```

**VS Code extension or command-line**
This behaviour is consistent for my local Pylance and Pyright 1.1.401.

Thank you!

Contributor guide

Open the contributing guide

Research direction

No repository files or tests are named. First reproduce the reported behavior with the Class0, Class1, and Class2 example using Pyright 1.1.401, then trace the TypeForm and generic type-inference handling. Done means Class2(1).attr is inferred as int rather than T0@Class2, with regression coverage for the example.

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.