microsoft / microsoft/pyright

sequence or mapping pattern on generic variables makes case unreachable

Open
#10,689 0 comments 1 reaction 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**

Applying sequence or mapping pattern on a generic variable makes that case unreachable, and false positive `reportUnnecessaryComparison` is reported (if enabled).

**Code or Screenshots**

[pyright-play.net](https://pyright-play.net/?code=MQAgDgngTglg5gCwC4C4RQKZgPZSQVQDtCMBjDAZwoEMoIBhbAWzFpgu0IF4koBXDAChBAEwwAzEOIDaAFQC6ACgAeaWQEoUgkDpBNqSUghCrtu86WoUMIAPoAaO1vMvdYWISSKARNYCO3uo6oCLsYAA21BAYIiBWIHyEmNRG1ABG4UKuOpbWIADe3gDW3mi2AL7O2ebuMJ4%2B%2BmCBwSChFBFRMXEUCUkYKQjpmWauuTa2VdU6rFTC4oqKAIz2AEzq6oLzhSVo3gBu3uXqQA)

```python
# pyright: reportUnnecessaryComparison=true

def f[T](x: T):
match x:
case _, _:
print("seq") # displayed as unreachable
case {"k": _}:
print("map") # displayed as unreachable
case _:
pass

f((1,2))
f({"k": "v"})
```

```
$ pyright ma.py
ma.py
ma.py:3:13 - warning: TypeVar "T" appears only once in generic function signature
Use "object" instead (reportInvalidTypeVarUse)
ma.py:5:14 - error: Pattern will never be matched for subject type "T@f" (reportUnnecessaryComparison)
ma.py:7:14 - error: Pattern will never be matched for subject type "T@f" (reportUnnecessaryComparison)
2 errors, 1 warning, 0 informations
```

```
$ python ma.py
seq
map
```

**VS Code extension or command-line**

command-line tool, 1.1.403

Contributor guide

Open the contributing guide

Research direction

Reproduce the diagnostic with the linked pyright-play example or ma.py using reportUnnecessaryComparison=true, then run the command-line tool on the sample. Trace how the two match patterns are classified for generic T; done means tuple and mapping cases are not reported as unreachable while the sample calls continue to work.

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
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.