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
Prev Previous commit
Next Next commit
Fix nested with statements
JukkaL committed Feb 25, 2022

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
commit c81b14eb879cae17f24ee1d836f5802e4c836782
30 changes: 19 additions & 11 deletions mypy/renaming.py
Original file line number Diff line number Diff line change
@@ -396,7 +396,7 @@ def record_assignment(self, name: str, can_be_redefined: bool) -> bool:

class VariableRenameVisitor2(TraverserVisitor):
def __init__(self) -> None:
self.bound_vars = set()
self.bound_vars: List[str] = []
self.bad: List[Set[str]] = []
self.refs: List[Dict[str, List[List[NameExpr]]]] = []

@@ -429,22 +429,30 @@ def visit_func_def(self, fdef: FuncDef) -> None:
def visit_with_stmt(self, stmt: WithStmt) -> None:
for expr in stmt.expr:
expr.accept(self)
names = []
n = 0
for target in stmt.target:
if target is not None:
assert isinstance(target, NameExpr)
name = target.name
d = self.refs[-1]
if name not in d:
d[name] = []
d[name].append([])
self.bound_vars.add(name)
names.append(name)
if name in self.bound_vars:
self.visit_name_expr(target)
else:
d = self.refs[-1]
if name not in d:
d[name] = []
d[name].append([])
self.bound_vars.append(name)
n += 1

#self.analyze_lvalue(target)
super().visit_with_stmt(stmt)
for name in names:
self.bound_vars.remove(name)

for target in stmt.target:
if target:
target.accept(self)
stmt.body.accept(self)

for _ in range(n):
self.bound_vars.pop()

def visit_import(self, imp: Import) -> None:
for id, as_id in imp.ids:
43 changes: 42 additions & 1 deletion test-data/unit/check-statements.test
Original file line number Diff line number Diff line change
@@ -1764,7 +1764,48 @@ class A:
class B:
def __enter__(self) -> B: ...
def __exit__(self, x, y, z) -> None: ...
[builtins fixtures/module.pyi]

[case testWithStatementScope10NestedWith]
from m import A, B

with A() as x:
with B() as x: \
# E: Incompatible types in assignment (expression has type "B", variable has type "A")
reveal_type(x) # N: Revealed type is "m.A"

with B() as x:
with A() as x: \
# E: Incompatible types in assignment (expression has type "A", variable has type "B")
reveal_type(x) # N: Revealed type is "m.B"

[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: ...

[case testWithStatementScope11NestedWith]
from m import A, B

with A() as x:
with A() as y:
reveal_type(y) # N: Revealed type is "m.A"
with B() as y:
reveal_type(y) # N: Revealed type is "m.B"

[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: ...


-- Chained assignment