Skip to content

Commit 8de78b8

Browse files
cdce8phauntsaninja
andauthored
Improve MappingPattern rest location (#20407)
The `rest` attribute doesn't have any line or column information. Fall back to the `MappingPattern` info. Closes #20259 --------- Co-authored-by: Shantanu Jain <hauntsaninja@gmail.com>
1 parent ab38885 commit 8de78b8

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

mypy/fastparse.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1799,7 +1799,7 @@ def visit_MatchMapping(self, n: ast3.MatchMapping) -> MappingPattern:
17991799
if n.rest is None:
18001800
rest = None
18011801
else:
1802-
rest = NameExpr(n.rest)
1802+
rest = self.set_line(NameExpr(n.rest), n)
18031803

18041804
node = MappingPattern(keys, values, rest)
18051805
return self.set_line(node, n)

test-data/unit/check-python310.test

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3194,3 +3194,15 @@ def fun(val: str | None):
31943194
case types.NoneType():
31953195
reveal_type(val) # N: Revealed type is "None"
31963196
[builtins fixtures/tuple.pyi]
3197+
3198+
[case testMatchMappingRestErrorLocation]
3199+
rest: list[int]
3200+
data: list[int]
3201+
match data:
3202+
case [1, *rest]:
3203+
...
3204+
config: dict[str, object]
3205+
match config:
3206+
case {"a": 1, **rest}: # E: Incompatible types in capture pattern (pattern captures type "dict[str, object]", variable has type "list[int]")
3207+
...
3208+
[builtins fixtures/dict.pyi]

0 commit comments

Comments
 (0)