Skip to content

Commit

Permalink
[used before def] handle walrus declaration in match subject correctly (
Browse files Browse the repository at this point in the history
#14665)

The subject needs to be processed outside of match statement and not as
part of the first branch.

Fixes #14659.
  • Loading branch information
ilinum authored Feb 10, 2023
1 parent c23e831 commit 4261e51
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 4261e51

Please sign in to comment.