Skip to content

Commit da5dffc

Browse files
Fix match subject ignoring redefinitions (#15306)
Fixes #14746
1 parent ac6dc18 commit da5dffc

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

mypy/renaming.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ def visit_assignment_stmt(self, s: AssignmentStmt) -> None:
182182
self.analyze_lvalue(lvalue)
183183

184184
def visit_match_stmt(self, s: MatchStmt) -> None:
185+
s.subject.accept(self)
185186
for i in range(len(s.patterns)):
186187
with self.enter_block():
187188
s.patterns[i].accept(self)

test-data/unit/check-python310.test

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1933,3 +1933,28 @@ def match_stmt_error5(x: Optional[str]) -> None:
19331933
pass
19341934
nested()
19351935
[builtins fixtures/tuple.pyi]
1936+
1937+
[case testMatchSubjectRedefinition]
1938+
# flags: --allow-redefinition
1939+
def transform1(a: str) -> int:
1940+
...
1941+
1942+
def transform2(a: int) -> str:
1943+
...
1944+
1945+
def redefinition_good(a: str):
1946+
a = transform1(a)
1947+
1948+
match (a + 1):
1949+
case _:
1950+
...
1951+
1952+
1953+
def redefinition_bad(a: int):
1954+
a = transform2(a)
1955+
1956+
match (a + 1): # E: Unsupported operand types for + ("str" and "int")
1957+
case _:
1958+
...
1959+
1960+
[builtins fixtures/primitives.pyi]

0 commit comments

Comments
 (0)