Skip to content

Commit 83db085

Browse files
authored
[backport] [used before def] handle walrus declaration in match subject correctly (#14672)
Backport of #14665.
1 parent c090ca2 commit 83db085

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

mypy/partially_defined.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -396,8 +396,8 @@ def visit_if_stmt(self, o: IfStmt) -> None:
396396
self.tracker.end_branch_statement()
397397

398398
def visit_match_stmt(self, o: MatchStmt) -> None:
399-
self.tracker.start_branch_statement()
400399
o.subject.accept(self)
400+
self.tracker.start_branch_statement()
401401
for i in range(len(o.patterns)):
402402
pattern = o.patterns[i]
403403
pattern.accept(self)

test-data/unit/check-python310.test

+13
Original file line numberDiff line numberDiff line change
@@ -1813,6 +1813,19 @@ def f1(x: int) -> int:
18131813

18141814
[typing fixtures/typing-medium.pyi]
18151815

1816+
[case testUsedBeforeDefMatchWalrus]
1817+
# flags: --enable-error-code used-before-def
1818+
import typing
1819+
1820+
def f0(x: int) -> None:
1821+
a = y # E: Cannot determine type of "y" # E: Name "y" is used before definition
1822+
match y := x:
1823+
case 1:
1824+
b = y
1825+
case 2:
1826+
c = y
1827+
d = y
1828+
18161829
[case testTypeAliasWithNewUnionSyntaxAndNoneLeftOperand]
18171830
from typing import overload
18181831
class C:

0 commit comments

Comments
 (0)