microsoft / microsoft/pyright

[match-case]: mapping pattern narrowing inconsistent with equivalent `isinstance` narrowing

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

Code sample in [pyright playground](https://pyright-play.net/?code=GYJw9gtgBALgngBwJYDsDmUkQWEMoCGAzkQKZ4D68CpAUKJFAMZgA2rpTMSYKRAdAQBGTTNlz4AsgQTJ0tWgBNSwWKSIwKqDQRRNSACgAeALiiKkXKAB9MKGAEooAWgB8UAHK9SJ2lH%2BYqkhE2jC6%2BsYANFDSsqhoDr4ByYQk5JrUhkbRFlxOUADEUICg5IDwf34BpKxkSSn%2BxGSUmVF2jv5FZQrKqjDqmhAEMEwAFsZmufi2qG1unt61-gNDw1CmFXVMxKRQAN4AvmaFUKQAjgCuSABuBBz2sGCYIXxhelnRsXIJ63UBDelUiDe5ksbSOgBlyco-ZhbKAUBZQ1KNDKAlrTfJFCFAA)

```python
from typing import assert_type
from collections.abc import Mapping

def test_instance(x: dict | int) -> None:
if isinstance(x, Mapping):
assert_type(x, dict) # ✅️
else:
assert_type(x, int) # ✅️

def test_match(x: dict | int) -> None:
match x:
case {}: # equivalent to isinstance(x, Mapping)
assert_type(x, dict) # ❌️
case _:
assert_type(x, int) # ❌️
```

Additionally, there are some failures related to `TypeDict` with mapping patterns: Code sample in [pyright playground](https://pyright-play.net/?code=GYJw9gtgBALgngBwJYDsDmUkQWEMoAqiApgCYAiSAxjADRQCGAzk8XgPrwLEBQokUKmAA2w4jSRgUTAHQMARlUzZc%2BALIMEydDx6liwWMSYx2qEwxRViACgAeALiilq%2BAD6YUMAJRQAtAB8UAByUsQOPFBRmIZITOYwltb29BpaqGjeEdE5jCxsply2dvQuNN6R0cTCrNm5UcysHEUpnj66%2BoYwxqYQDDBUABb2TmXubb6BIWF1UX0Dg1COlfVUzMRQAN4Avk5QAMRQxACOAK5IAG4MYl6wYJjx0olWxama2pkr9dGNBZwkrTGFW%2BgnWUHYsxBeSahQBJQmuh4VGEjSgAEEbERuBRXFkoAhGh0DEYTGYnkliOwMY50VAPKgfP4gqEUOEvkhYo8LC9WmkPlkvjlfs04fQ0cCqjU2SDhbDuK0GRU9MTuqT5kMqSNafSvJNmTMvurFssQWtWFtdlFDidzlcbvgYPc4gkKbz3hkJVDoX8WvDxYLomaNhCA-VZf95fDFVaoIAZckA8H-ekXyhl09H0aPABhIMSkKAAbVlfiKAF1dMjUQAhTEkHHlSEoBgQcJQEwgIldHpk7nWdjVmmVtPRqYs6XRDkPF08%2BF8j2QoX5ZOvKCVz1HKXzn6LuXLxUdkm9foa-tOQc6xkjg05I1LTdRINbABEjebj6c7EtByOZ0u12It0dKAbGdclpzedJ0F8Sw82fJtiEfTwljXGVtwjZdV1DUFzRDL0GlQ30M11RFOgPdgjXYO1TkpIo%2By1M8JiZaZWUhG8TW%2BB9Nlg18nDbGxvF2TCcmtH87X-B17mArlnmSGd3UgxgUDzECe2KGQ0GIGAbC4%2BDvHoNtvGQ75wwIldDPvMEcNw4zRQRIA)

Contributor guide

Open the contributing guide

Research direction

Reproduce both cases from the linked Pyright Playground samples, comparing test_instance with test_match and checking the TypeDict mapping-pattern failures. Start with the assert_type results and trace the narrowing behavior in Pyright’s relevant tests and implementation. Done means equivalent Mapping and match narrowing agree, with the reported TypeDict cases covered.

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.