Skip to content

Give "as" variables in with statements separate scopes #12254

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 19 commits into from
Feb 25, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add match statement test case
  • Loading branch information
JukkaL committed Feb 25, 2022
commit cfc1017f5bdc05f4a74ce37d313b1db807931a1f
14 changes: 0 additions & 14 deletions mypy/renaming.py
Original file line number Diff line number Diff line change
Expand Up @@ -452,20 +452,6 @@ def visit_import_from(self, imp: ImportFrom) -> None:
def visit_import_all(self, imp: ImportAll) -> None:
self.record_bad('*')

#def visit_match_stmt(self, s: MatchStmt) -> None:
# for i in range(len(s.patterns)):
# s.patterns[i].accept(self)
# guard = s.guards[i]
# if guard is not None:
# guard.accept(self)
# # We already entered a block, so visit this block's statements directly
# for stmt in s.bodies[i].body:
# stmt.accept(self)

#def visit_capture_pattern(self, p: AsPattern) -> None:
# if p.name is not None:
# self.analyze_lvalue(p.name)

def analyze_lvalue(self, lvalue: Lvalue) -> None:
if isinstance(lvalue, NameExpr):
name = lvalue.name
Expand Down
41 changes: 41 additions & 0 deletions test-data/unit/check-python310.test
Original file line number Diff line number Diff line change
Expand Up @@ -1175,3 +1175,44 @@ def foo(value) -> int: # E: Missing return statement
return 1
case 2:
return 2

[case testWithStatementScopeAndMatchStatement]
from m import A, B

with A() as x:
pass
with B() as x: \
# E: Incompatible types in assignment (expression has type "B", variable has type "A")
pass

with A() as y:
pass
with B() as y: \
# E: Incompatible types in assignment (expression has type "B", variable has type "A")
pass

with A() as z:
pass
with B() as z: \
# E: Incompatible types in assignment (expression has type "B", variable has type "A")
pass

with A() as zz:
pass
with B() as zz: \
# E: Incompatible types in assignment (expression has type "B", variable has type "A")
pass

match x:
case str(y) as z:
zz = y

[file m.pyi]
from typing import Any

class A:
def __enter__(self) -> A: ...
def __exit__(self, x, y, z) -> None: ...
class B:
def __enter__(self) -> B: ...
def __exit__(self, x, y, z) -> None: ...