Include ImportError in non-fixable try-catch imports (#4793)

This commit is contained in:
Charlie Marsh 2023-06-01 15:53:49 -04:00 committed by GitHub
parent be740106e0
commit b7038cee13
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 14 deletions

View File

@ -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;
}
_ => {}
}
}
}

View File

@ -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:
@ -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: