Skip to content

Commit 1bbcd53

Browse files
authored
Check for deleted vars in 'raise from' (python#9272)
Thanks to @isra17. Fixes python#9270
1 parent b10d540 commit 1bbcd53

File tree

2 files changed

+6
-0
lines changed

2 files changed

+6
-0
lines changed

mypy/checker.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3317,6 +3317,9 @@ def visit_raise_stmt(self, s: RaiseStmt) -> None:
33173317
def type_check_raise(self, e: Expression, s: RaiseStmt,
33183318
optional: bool = False) -> None:
33193319
typ = get_proper_type(self.expr_checker.accept(e))
3320+
if isinstance(typ, DeletedType):
3321+
self.msg.deleted_as_rvalue(typ, e)
3322+
return
33203323
exc_type = self.named_type('builtins.BaseException')
33213324
expected_type = UnionType([exc_type, TypeType(exc_type)])
33223325
if optional:

test-data/unit/check-statements.test

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -446,9 +446,12 @@ raise x # E: Exception must be derived from BaseException
446446
e = None # type: BaseException
447447
f = None # type: MyError
448448
a = None # type: A
449+
x = None # type: BaseException
450+
del x
449451
raise e from a # E: Exception must be derived from BaseException
450452
raise e from e
451453
raise e from f
454+
raise e from x # E: Trying to read deleted variable 'x'
452455
class A: pass
453456
class MyError(BaseException): pass
454457
[builtins fixtures/exception.pyi]

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy