microsoft / microsoft/pyright

Numerous FPs when `Literal['abc']` and var named `abc` coexist (but only if `enableExperimentalFeatures=true`)

Open
#10,973 4 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**
I haven't been able to narrow down _exactly_ what triggers the issue, but it relates to a variable having a name identical to the value of a `Literal` type.

**Code or Screenshots**
Code sample in [pyright playground](https://pyright-play.net/?strict=true&code=MQAgDgngTglg5gCwC4C4QGcmwMZIFCiSyKogCmAdgIYBGANmQKIAeYZsAtpUlXQGJkqSAK5Qy6ALxZhZPADMoAew4gA%2BqrnCRY9SBgcwiqEhBUKFRTyQxFFdPKUqkEMDApw9BoyYAyMJOy8eHjoymSqACYwuGhRuADabkgANCB%2BAVC88QDktmTZqdlIAO6K2QC65SASIADeAL4hYapwlKpkrFDVIAAUeCADgwMwIHJGvTCpHIoRZACUehQYzXFIAHT%2BZBzoPXP9Q4MwciDTs9U1uRT5%2Bwcge2NdeYvLxmQRPaFckdFIcyg3am6MHeeTmQA)

```python
# pyright: strict
# pyright: enableExperimentalFeatures=true
from __future__ import annotations
from typing import Literal

some_dict: dict[int, Literal['one', 'two']] = {}
some_gen_expr = (
i for (i, mode) in some_dict.items()
if mode == 'one'
)
for one in sorted(some_dict):
_ = id(one)

```

**Output:**

**Expected:** No output.

**Note:** No errors occur (expected behaviour) if the variable `one` is renamed (change `one` to `half_of_two` on the last two lines).

**Actual output:**
```
$ pyright -p /dev/null bug.py | sed -e "s@$PWD@...@g"
.../bug.py
.../bug.py:7:1 - error: Type of "some_gen_expr" is partially unknown
  Type of "some_gen_expr" is "Generator[Unknown, None, None]" (reportUnknownVariableType)
.../bug.py:8:16 - error: Type of "i" is unknown (reportUnknownVariableType)
.../bug.py:8:19 - error: Type of "mode" is unknown (reportUnknownVariableType)
.../bug.py:11:5 - error: Type of "one" is unknown (reportUnknownVariableType)
.../bug.py:11:19 - error: Argument type is partially unknown
  Argument corresponds to parameter "iterable" in function "sorted"
  Argument type is "dict[Unknown, Unknown]" (reportUnknownArgumentType)
.../bug.py:12:12 - error: Argument type is unknown
  Argument corresponds to parameter "obj" in function "id" (reportUnknownArgumentType)
6 errors, 0 warnings, 0 informations
```

**VS Code extension or command-line**

Verified with vim-lsp and command-line:

```
$ pyright --version
pyright 1.1.405
```

Contributor guide

Open the contributing guide

Research direction

Reproduce the provided Python sample with pyright 1.1.405 and enableExperimentalFeatures=true, using the linked playground or the shown command; compare it with the renamed-variable case. Trace the diagnostic path for the generator and Literal handling, then verify that the sample produces no output without unrelated diagnostics.

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
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.