microsoft / microsoft/pyright

False positive when callback protocol uses a function-scoped type variable

Open
#10,927 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
Python
Stars
15.6k
Forks
1.8k
Avg merge
12h 13m
Merged PRs (30d)
52

Description

This appears to be a regression as the example code in #8177 fails to type check.

Please consider the following code:
```python
from typing import Protocol

class A[T,U]: ...

class BProto(Protocol):
def __call__[T](self) -> A[list[T], T]: ...

def make_a[T]() -> A[list[T], T]: ...

def func1() -> BProto:
return make_a
```

Pyright 1.1.405 run from command-line reports the following above the above code:
```
error: Type "() -> A[list[T@make_a], T@make_a]" is not assignable to return type "BProto"
Type "() -> A[list[T@make_a], T@make_a]" is not assignable to type "() -> A[list[T@__call__], T@__call__]"
Function return type "A[list[T@make_a], T@make_a]" is incompatible with type "A[list[T@__call__], T@__call__]"
"A[list[T@make_a], T@make_a]" is not assignable to "A[list[T@__call__], T@__call__]"
Type parameter "T@A" is covariant, but "list[T@make_a]" is not a subtype of "list[T@__call__]"
"list[T@make_a]" is not assignable to "list[T@__call__]"
Type parameter "U@A" is covariant, but "T@make_a" is not a subtype of "T@__call__"
Type "T@make_a" is not assignable to type "T@__call__" (reportReturnType)
```
But `make_a` is an instance of `BProto`, so I would expect there not to be any type error.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the reported snippet with Pyright 1.1.405 from the command line and confirm the false-positive return-type diagnostic. Trace the callable protocol and function-scoped type-variable compatibility checks, then verify that `func1` accepts `make_a` without an error while other diagnostics remain unchanged.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
compilers, 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.