Skip to content

Commit ef08fdb

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 except ImportError: import something_wrong ``` we should not raise a diagnostic as the content of the try will probably contain the right import
1 parent 0dd3bd4 commit ef08fdb

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
@@ -858,8 +858,19 @@ impl PythonArchEval {
858858
self.visit_sub_stmts(session, &try_stmt.finalbody);
859859
for handler in try_stmt.handlers.iter() {
860860
handler.as_except_handler().map(|h| {
861+
//Prevent import error in catch clause of ImportError too
862+
let mut added_safe_import = false;
863+
if let Some(type_) = &h.type_ {
864+
if type_.is_name_expr() && type_.as_name_expr().unwrap().id.to_string() == "ImportError" {
865+
added_safe_import = true;
866+
self.safe_import.push(true);
867+
}
868+
}
861869
h.type_.as_ref().map(|test_clause| self.visit_expr(session, test_clause));
862-
self.visit_sub_stmts(session, &h.body)
870+
self.visit_sub_stmts(session, &h.body);
871+
if added_safe_import {
872+
self.safe_import.pop();
873+
}
863874
});
864875
}
865876
}

0 commit comments

Comments
 (0)