Skip to content

Commit

Permalink
[backport] [used before def] handle walrus declaration in match subje…
Browse files Browse the repository at this point in the history
…ct correctly (#14672)

Backport of #14665.
  • Loading branch information
ilinum authored Feb 13, 2023
1 parent c090ca2 commit 83db085
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
2 changes: 1 addition & 1 deletion mypy/partially_defined.py
Original file line number Diff line number Diff line change
Expand Up @@ -396,8 +396,8 @@ def visit_if_stmt(self, o: IfStmt) -> None:
self.tracker.end_branch_statement()

def visit_match_stmt(self, o: MatchStmt) -> None:
self.tracker.start_branch_statement()
o.subject.accept(self)
self.tracker.start_branch_statement()
for i in range(len(o.patterns)):
pattern = o.patterns[i]
pattern.accept(self)
Expand Down
13 changes: 13 additions & 0 deletions test-data/unit/check-python310.test
Original file line number Diff line number Diff line change
Expand Up @@ -1813,6 +1813,19 @@ def f1(x: int) -> int:

[typing fixtures/typing-medium.pyi]

[case testUsedBeforeDefMatchWalrus]
# flags: --enable-error-code used-before-def
import typing

def f0(x: int) -> None:
a = y # E: Cannot determine type of "y" # E: Name "y" is used before definition
match y := x:
case 1:
b = y
case 2:
c = y
d = y

[case testTypeAliasWithNewUnionSyntaxAndNoneLeftOperand]
from typing import overload
class C:
Expand Down

0 comments on commit 83db085

Please sign in to comment.