microsoft / microsoft/pyright

sequence pattern on NamedTuple infers `tuple` type

Open
#11,243 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

**Describe the bug**

In a match statement, when a sequence pattern is applied to a NamedTuple variable, the variable is inferred as a `tuple` rather than it's original NamedTuple type.

**Code or Screenshots**

```python
from typing import NamedTuple

class A(NamedTuple):
x: int
y: int

def f(a: A):
...

def m(a: A):
match a:
case _, _:
f(a)
```

```
nt.py
nt.py:13:15 - error: Argument of type "tuple[int, int]" cannot be assigned to parameter "a" of type "A" in function "f"
"tuple[int, int]" is not assignable to "A" (reportArgumentType)
1 error, 0 warnings, 0 informations
```

workaround: use `case A(_, _):` instead of `case _, _:`

**VS Code extension or command-line**

command-line tool, 1.1.408

Contributor guide

Open the contributing guide

Research direction

Start by running the command-line tool against the reproduced nt.py example and compare the inferred type inside the sequence pattern with the original NamedTuple type. Trace the pattern-matching type inference involved in that diagnostic; done means the example no longer reports A as tuple[int, int] while preserving the workaround's behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
devtools
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
52/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.