microsoft / microsoft/pyright

TypeForm error when used in function with specific structure

Open
#10,793 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

```python
from typing import Any

from typing_extensions import TypeForm

class ClassX: ...

class ClassY: ...

class ClassZ:
def meth(self, arg: Any): ...

def typeform_fn(arg: TypeForm[Any]) -> ClassZ: ...

def anyfn(*args: Any) -> Any: ...

def main(unrelated: list[list[int]]):
# it works without these loops
for vs in unrelated:
for _v in vs:
if anyfn(ClassX):
return

# error: Argument of type "UnionType" cannot be assigned to parameter "arg" of type "TypeForm[Any]" in function "typeform_fn"
z = typeform_fn(ClassX | ClassY)
z.meth(ClassX())
```
In my original code, it infers the wrong type in a generic TypeForm instead of argument error.

Contributor guide

Open the contributing guide

Research direction

Start by running the supplied Python reproducer and compare diagnostics with and without the nested loops. Trace the TypeForm and generic inference behavior involved in the call to typeform_fn(ClassX | ClassY). Done means the reported error or inferred type matches the intended behavior for this structure without regressing the working variant.

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.