Skip to content

Fix exception causes all over the codebase #806

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ Release Date: TBA

Close PyCQA/pylint#3583

* Fixed exception-chaining error messages.


What's New in astroid 2.4.3?
============================
Expand Down
38 changes: 19 additions & 19 deletions astroid/brain/brain_builtin_inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,13 +165,13 @@ def _container_generic_inference(node, context, node_type, transform):
if not transformed:
try:
inferred = next(arg.infer(context=context))
except (InferenceError, StopIteration):
raise UseInferenceDefault()
except (InferenceError, StopIteration) as exc:
raise UseInferenceDefault from exc
if inferred is util.Uninferable:
raise UseInferenceDefault()
raise UseInferenceDefault
transformed = transform(inferred)
if not transformed or transformed is util.Uninferable:
raise UseInferenceDefault()
raise UseInferenceDefault
return transformed


Expand Down Expand Up @@ -267,8 +267,8 @@ def _get_elts(arg, context):
is_iterable = lambda n: isinstance(n, (nodes.List, nodes.Tuple, nodes.Set))
try:
inferred = next(arg.infer(context))
except (InferenceError, NameInferenceError):
raise UseInferenceDefault()
except (InferenceError, NameInferenceError) as exc:
raise UseInferenceDefault from exc
if isinstance(inferred, nodes.Dict):
items = inferred.items
elif is_iterable(inferred):
Expand Down Expand Up @@ -371,12 +371,12 @@ def infer_super(node, context=None):
else:
try:
mro_pointer = next(node.args[0].infer(context=context))
except InferenceError:
raise UseInferenceDefault
except InferenceError as exc:
raise UseInferenceDefault from exc
try:
mro_type = next(node.args[1].infer(context=context))
except InferenceError:
raise UseInferenceDefault
except InferenceError as exc:
raise UseInferenceDefault from exc

if mro_pointer is util.Uninferable or mro_type is util.Uninferable:
# No way we could understand this.
Expand All @@ -397,8 +397,8 @@ def _infer_getattr_args(node, context):
try:
obj = next(node.args[0].infer(context=context))
attr = next(node.args[1].infer(context=context))
except InferenceError:
raise UseInferenceDefault
except InferenceError as exc:
raise UseInferenceDefault from exc

if obj is util.Uninferable or attr is util.Uninferable:
# If one of the arguments is something we can't infer,
Expand Down Expand Up @@ -437,8 +437,8 @@ def infer_getattr(node, context=None):
# Try to infer the default and return it instead.
try:
return next(node.args[2].infer(context=context))
except InferenceError:
raise UseInferenceDefault
except InferenceError as exc:
raise UseInferenceDefault from exc

raise UseInferenceDefault

Expand Down Expand Up @@ -505,8 +505,8 @@ def infer_property(node, context=None):
getter = node.args[0]
try:
inferred = next(getter.infer(context=context))
except InferenceError:
raise UseInferenceDefault
except InferenceError as exc:
raise UseInferenceDefault from exc

if not isinstance(inferred, (nodes.FunctionDef, nodes.Lambda)):
raise UseInferenceDefault
Expand Down Expand Up @@ -673,12 +673,12 @@ def infer_isinstance(callnode, context=None):
class_container = _class_or_tuple_to_container(
class_or_tuple_node, context=context
)
except InferenceError:
raise UseInferenceDefault
except InferenceError as exc:
raise UseInferenceDefault from exc
try:
isinstance_bool = helpers.object_isinstance(obj_node, class_container, context)
except AstroidTypeError as exc:
raise UseInferenceDefault("TypeError: " + str(exc))
raise UseInferenceDefault("TypeError: " + str(exc)) from exc
except MroError as exc:
raise UseInferenceDefault from exc
if isinstance_bool is util.Uninferable:
Expand Down
18 changes: 9 additions & 9 deletions astroid/brain/brain_namedtuple_enum.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ def _infer_first(node, context):
raise UseInferenceDefault()
else:
return value
except StopIteration:
raise InferenceError()
except StopIteration as exc:
raise InferenceError from exc


def _find_func_form_arguments(node, context):
Expand Down Expand Up @@ -88,7 +88,7 @@ def infer_func_form(node, base_type, context=None, enum=False):
name, names = _find_func_form_arguments(node, context)
try:
attributes = names.value.replace(",", " ").split()
except AttributeError:
except AttributeError as exc:
if not enum:
attributes = [
_infer_first(const, context).value for const in names.elts
Expand Down Expand Up @@ -117,11 +117,11 @@ def infer_func_form(node, base_type, context=None, enum=False):
_infer_first(const, context).value for const in names.elts
]
else:
raise AttributeError
raise AttributeError from exc
if not attributes:
raise AttributeError
except (AttributeError, exceptions.InferenceError):
raise UseInferenceDefault()
raise AttributeError from exc
except (AttributeError, exceptions.InferenceError) as exc:
raise UseInferenceDefault from exc

attributes = [attr for attr in attributes if " " not in attr]

Expand Down Expand Up @@ -405,8 +405,8 @@ def infer_typing_namedtuple(node, context=None):
# so we extract the args and infer a named tuple.
try:
func = next(node.func.infer())
except InferenceError:
raise UseInferenceDefault
except InferenceError as exc:
raise UseInferenceDefault from exc

if func.qname() != "typing.NamedTuple":
raise UseInferenceDefault
Expand Down
4 changes: 2 additions & 2 deletions astroid/brain/brain_random.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ def infer_random_sample(node, context=None):

try:
elts = random.sample(inferred_sequence.elts, length.value)
except ValueError:
raise astroid.UseInferenceDefault
except ValueError as exc:
raise astroid.UseInferenceDefault from exc

new_node = astroid.List(
lineno=node.lineno, col_offset=node.col_offset, parent=node.scope()
Expand Down
4 changes: 2 additions & 2 deletions astroid/brain/brain_six.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,8 @@ def _six_fail_hook(modname):
attribute = modname[start_index:].lstrip(".").replace(".", "_")
try:
import_attr = module.getattr(attribute)[0]
except AttributeInferenceError:
raise AstroidBuildingError(modname=modname)
except AttributeInferenceError as exc:
raise AstroidBuildingError(modname=modname) from exc
if isinstance(import_attr, nodes.Import):
submodule = MANAGER.ast_from_module_name(import_attr.names[0][0])
return submodule
Expand Down
4 changes: 2 additions & 2 deletions astroid/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,9 @@ def raise_if_nothing_inferred(func, instance, args, kwargs):
# generator is empty
if error.args:
# pylint: disable=not-a-mapping
raise exceptions.InferenceError(**error.args[0])
raise exceptions.InferenceError(**error.args[0]) from error
raise exceptions.InferenceError(
"StopIteration raised without any error information."
)
) from error

yield from generator
4 changes: 2 additions & 2 deletions astroid/protocols.py
Original file line number Diff line number Diff line change
Expand Up @@ -498,8 +498,8 @@ def _infer_context_manager(self, mgr, context):
elif isinstance(inferred, bases.Instance):
try:
enter = next(inferred.igetattr("__enter__", context=context))
except (exceptions.InferenceError, exceptions.AttributeInferenceError):
raise exceptions.InferenceError(node=inferred)
except (exceptions.InferenceError, exceptions.AttributeInferenceError) as exc:
raise exceptions.InferenceError(node=inferred) from exc
if not isinstance(enter, bases.BoundMethod):
raise exceptions.InferenceError(node=enter)
yield from enter.infer_call_result(self, context)
Expand Down
2 changes: 1 addition & 1 deletion astroid/scoped_nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -2571,7 +2571,7 @@ def igetattr(self, name, context=None, class_context=True):
else:
raise exceptions.InferenceError(
error.message, target=self, attribute=name, context=context
)
) from error

def has_dynamic_getattr(self, context=None):
"""Check if the class has a custom __getattr__ or __getattribute__.
Expand Down