Skip to content

Commit d3180ea

Browse files
Reraise failures in astroid as AstroidError
1 parent 896c8ed commit d3180ea

File tree

4 files changed

+19
-2
lines changed

4 files changed

+19
-2
lines changed

doc/whatsnew/2/2.14/summary.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,10 @@ Other Changes
141141

142142
Closes #4301
143143

144+
* We made a greater effort to reraise failures stemming from the ``astroid``
145+
library as ``AstroidError``, with the effect that pylint emits ``astroid-error``
146+
rather than merely ``fatal``. Regardless, please report any such issues you encounter!
147+
144148
* We have improved our recognition of inline disable and enable comments. It is
145149
now possible to disable ``bad-option-value`` inline (as long as you disable it before
146150
the bad option value is raised, i.e. ``disable=bad-option-value,bad-message`` not ``disable=bad-message,bad-option-value`` ) as well as certain other

pylint/checkers/imports.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -771,7 +771,7 @@ def _get_imported_module(self, importnode, modname):
771771
message = f"Cannot import {modname!r} due to syntax error {str(exc.error)!r}" # pylint: disable=no-member; false positive
772772
self.add_message("syntax-error", line=importnode.lineno, args=message)
773773

774-
except astroid.AstroidBuildingException:
774+
except astroid.AstroidBuildingError:
775775
if not self.linter.is_message_enabled("import-error"):
776776
return None
777777
if _ignore_import_failure(importnode, modname, self._ignored_modules):
@@ -784,6 +784,8 @@ def _get_imported_module(self, importnode, modname):
784784

785785
dotted_modname = get_import_name(importnode, modname)
786786
self.add_message("import-error", args=repr(dotted_modname), node=importnode)
787+
except Exception as e: # pragma: no cover
788+
raise astroid.AstroidError from e
787789
return None
788790

789791
def _add_imported_module(

pylint/checkers/utils.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import astroid.objects
2222
from astroid import TooManyLevelsError, nodes
2323
from astroid.context import InferenceContext
24+
from astroid.exceptions import AstroidError
2425

2526
from pylint.constants import TYPING_TYPE_CHECKS_GUARDS
2627

@@ -1259,6 +1260,8 @@ def safe_infer(
12591260
value = next(infer_gen)
12601261
except astroid.InferenceError:
12611262
return None
1263+
except Exception as e: # pragma: no cover
1264+
raise AstroidError from e
12621265

12631266
if value is not astroid.Uninferable:
12641267
inferred_types.add(_get_python_type_of_node(value))
@@ -1280,6 +1283,8 @@ def safe_infer(
12801283
return None # There is some kind of ambiguity
12811284
except StopIteration:
12821285
return value
1286+
except Exception as e: # pragma: no cover
1287+
raise AstroidError from e
12831288
return value if len(inferred_types) <= 1 else None
12841289

12851290

@@ -1291,6 +1296,8 @@ def infer_all(
12911296
return list(node.infer(context=context))
12921297
except astroid.InferenceError:
12931298
return []
1299+
except Exception as e: # pragma: no cover
1300+
raise AstroidError from e
12941301

12951302

12961303
def has_known_bases(

pylint/lint/pylinter.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -695,6 +695,7 @@ def _check_file(
695695
:param callable check_astroid_module: callable checking an AST taking the following arguments
696696
- ast: AST of the module
697697
:param FileItem file: data about the file
698+
:raises AstroidError: for any failures stemming from astroid
698699
"""
699700
self.set_current_module(file.name, file.filepath)
700701
# get the module representation
@@ -708,7 +709,10 @@ def _check_file(
708709
# fix the current file (if the source file was not available or
709710
# if it's actually a c extension)
710711
self.current_file = ast_node.file
711-
check_astroid_module(ast_node)
712+
try:
713+
check_astroid_module(ast_node)
714+
except Exception as e: # pragma: no cover
715+
raise astroid.AstroidError from e
712716
# warn about spurious inline messages handling
713717
spurious_messages = self.file_state.iter_spurious_suppression_messages(
714718
self.msgs_store

0 commit comments

Comments
 (0)