microsoft / microsoft/pyright

bug: bidirectional inference when using typeguard

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

I can't see why pyright doesn't complain at `return g` here. I had expected it would say that `Callable[P, R]` isn't necessarily `F`.

```
from typing import Callable
from typing import TypeGuard

class DecoratorProblem[F, **P, R]:
def is_pr(self, f: object) -> TypeGuard[Callable[P, R]]:
return isinstance(f, Callable)

def decorator(self, f: F) -> F:
assert self.is_pr(f)
g = self._decorator(f)
return g # pyright allows Callable[P, R] to be assigned to F? Why?

def _decorator(self, f: Callable[P, R]) -> Callable[P, R]:
def wrapper(*args: P.args, **kwargs: P.kwargs) -> R:
return f(*args, **kwargs)
return wrapper
```

see also: https://github.com/microsoft/pyright/discussions/10724

Contributor guide

Open the contributing guide

Research direction

Use the Python code sample as the minimal reproduction and run it through pyright, focusing on the type accepted at `return g` after the `TypeGuard` assertion. Read the related discussion for context, then trace the bidirectional inference involved and add a regression test that verifies whether the expected diagnostic is reported.

Written by the indexing model from the issue text.

Assessment

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