Skip to content

Commit 971c426

Browse files
author
Guido van Rossum
committed
Fix bug with exception variable reuse in deferred node.
See #1748 (comment)
1 parent 48fa2ef commit 971c426

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

mypy/checker.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1712,7 +1712,8 @@ def visit_try_without_finally(self, s: TryStmt, try_frame: bool) -> None:
17121712
'accept outside except: blocks even in '
17131713
'python 2)'.format(s.vars[i].name))
17141714
var = cast(Var, s.vars[i].node)
1715-
var.type = DeletedType(source=source)
1715+
if not self.current_node_deferred:
1716+
var.type = DeletedType(source=source)
17161717
self.binder.cleanse(s.vars[i])
17171718
if s.else_body:
17181719
self.accept(s.else_body)

test-data/unit/check-statements.test

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -687,6 +687,17 @@ e = 1 # E: Assignment to variable 'e' outside except: block
687687
e = E1() # E: Assignment to variable 'e' outside except: block
688688
[builtins fixtures/exception.pyi]
689689

690+
[case testExceptionVariableReuseInDeferredNode]
691+
def f(*a: BaseException) -> int:
692+
x
693+
try: pass
694+
except BaseException as err: pass
695+
try: pass
696+
except BaseException as err: f(err)
697+
x = f()
698+
[builtins fixtures/exception.pyi]
699+
700+
690701
[case testArbitraryExpressionAsExceptionType]
691702
import typing
692703
a = BaseException

0 commit comments

Comments
 (0)