Skip to content

Commit

Permalink
Include ImportError in non-fixable try-catch imports (#4793)
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh authored Jun 1, 2023
1 parent be74010 commit b7038ce
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 14 deletions.
15 changes: 11 additions & 4 deletions crates/ruff/src/checkers/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1976,10 +1976,17 @@ where
let mut handled_exceptions = Exceptions::empty();
for type_ in extract_handled_exceptions(handlers) {
if let Some(call_path) = self.semantic_model.resolve_call_path(type_) {
if call_path.as_slice() == ["", "NameError"] {
handled_exceptions |= Exceptions::NAME_ERROR;
} else if call_path.as_slice() == ["", "ModuleNotFoundError"] {
handled_exceptions |= Exceptions::MODULE_NOT_FOUND_ERROR;
match call_path.as_slice() {
["", "NameError"] => {
handled_exceptions |= Exceptions::NAME_ERROR;
}
["", "ModuleNotFoundError"] => {
handled_exceptions |= Exceptions::MODULE_NOT_FOUND_ERROR;
}
["", "ImportError"] => {
handled_exceptions |= Exceptions::IMPORT_ERROR;
}
_ => {}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ F401_10.py:6:16: F401 `orjson` imported but unused; consider using `importlib.ut
|
= help: Remove unused import: `orjson`

F401_10.py:15:16: F401 [*] `orjson` imported but unused
F401_10.py:15:16: F401 `orjson` imported but unused; consider using `importlib.util.find_spec` to test for availability
|
15 | def import_error():
16 | try:
Expand All @@ -23,13 +23,4 @@ F401_10.py:15:16: F401 [*] `orjson` imported but unused
|
= help: Remove unused import: `orjson`

Suggested fix
12 12 |
13 13 | def import_error():
14 14 | try:
15 |- import orjson
16 15 |
17 16 | return True
18 17 | except ImportError:


0 comments on commit b7038ce

Please sign in to comment.