microsoft / microsoft/pyright

Strange regression in lamda parameter type inference in 1.1.405

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

**Describe the bug**
In v1.1.404, the following code typechecks

```python
def foo():
for t in [1]:
pass
r: set[int] = set()
a: set[int] = set()
while r:
if a:
s = sorted(a, key=lambda v: v)
for t in s:
if -t in r:
a = a
```

In particular, the lambda parameter `v` is inferred to be of type `int`.

In v1.1.405, the following errors are detected:
```
error: Argument type is partially unknown
Argument corresponds to parameter "key" in function "sorted"
Argument type is "(v: Unknown) -> Unknown" (reportUnknownArgumentType)
error: Type of parameter "v" is unknown (reportUnknownLambdaType)
error: Return type of lambda is unknown (reportUnknownLambdaType)
```

The above code is a mostly minimized repro, based on real code that started failing when upgrading to v1.1.405. Removing any line in the repo above hides the error.

The issues was confirmed via VSCode + Pylance (with the pyright backend) as well as the cli.

NOTE: When working on the minimized reproducer, I noticed some variants of this code fail on v1.1.404 as well, suggesting the underlying issue exists in both, but some recent change make it trigger more often in v1.1.405.

Contributor guide

Open the contributing guide

Research direction

Start by running the minimized reproducer with the Pyright CLI and compare its behavior between v1.1.404 and v1.1.405; the report names no source file or test. Trace how the `sorted` call and its identity lambda infer the parameter type in the affected version. Done means the reproducer infers `v` as `int` without the reported unknown-type errors, with regression coverage added.

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.