Skip to content

Commit bacc89f

Browse files
committed
[IMP] prevent import error diagnostic in ImportError catch clause of try..catch
If a missing impotr is in a catch clause like ```python try: pass catch ImportError: import something_wrong ``` we should not raise a diagnostic as the content of the try will probably contain the right import
1 parent 8c16351 commit bacc89f

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

server/src/core/python_arch_eval.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -857,8 +857,19 @@ impl PythonArchEval {
857857
self.visit_sub_stmts(session, &try_stmt.finalbody);
858858
for handler in try_stmt.handlers.iter() {
859859
handler.as_except_handler().map(|h| {
860+
//Prevent import error in catch clause of ImportError too
861+
let mut added_safe_import = false;
862+
if let Some(type_) = &h.type_ {
863+
if type_.is_name_expr() && type_.as_name_expr().unwrap().id.to_string() == "ImportError" {
864+
added_safe_import = true;
865+
self.safe_import.push(true);
866+
}
867+
}
860868
h.type_.as_ref().map(|test_clause| self.visit_expr(session, test_clause));
861-
self.visit_sub_stmts(session, &h.body)
869+
self.visit_sub_stmts(session, &h.body);
870+
if added_safe_import {
871+
self.safe_import.pop();
872+
}
862873
});
863874
}
864875
}

0 commit comments

Comments
 (0)