Skip to content

Commit 8c55e49

Browse files
committed
Fix exception causes all over the codebase
1 parent 92f5568 commit 8c55e49

8 files changed

+40
-37
lines changed

ChangeLog

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ Release Date: TBA
2323

2424
Close PyCQA/pylint#3583
2525

26+
* Fixed exception-chaining error messages.
27+
2628

2729
What's New in astroid 2.4.3?
2830
============================

astroid/brain/brain_builtin_inference.py

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -165,13 +165,13 @@ def _container_generic_inference(node, context, node_type, transform):
165165
if not transformed:
166166
try:
167167
inferred = next(arg.infer(context=context))
168-
except (InferenceError, StopIteration):
169-
raise UseInferenceDefault()
168+
except (InferenceError, StopIteration) as exc:
169+
raise UseInferenceDefault from exc
170170
if inferred is util.Uninferable:
171-
raise UseInferenceDefault()
171+
raise UseInferenceDefault
172172
transformed = transform(inferred)
173173
if not transformed or transformed is util.Uninferable:
174-
raise UseInferenceDefault()
174+
raise UseInferenceDefault
175175
return transformed
176176

177177

@@ -267,8 +267,8 @@ def _get_elts(arg, context):
267267
is_iterable = lambda n: isinstance(n, (nodes.List, nodes.Tuple, nodes.Set))
268268
try:
269269
inferred = next(arg.infer(context))
270-
except (InferenceError, NameInferenceError):
271-
raise UseInferenceDefault()
270+
except (InferenceError, NameInferenceError) as exc:
271+
raise UseInferenceDefault from exc
272272
if isinstance(inferred, nodes.Dict):
273273
items = inferred.items
274274
elif is_iterable(inferred):
@@ -371,12 +371,12 @@ def infer_super(node, context=None):
371371
else:
372372
try:
373373
mro_pointer = next(node.args[0].infer(context=context))
374-
except InferenceError:
375-
raise UseInferenceDefault
374+
except InferenceError as exc:
375+
raise UseInferenceDefault from exc
376376
try:
377377
mro_type = next(node.args[1].infer(context=context))
378-
except InferenceError:
379-
raise UseInferenceDefault
378+
except InferenceError as exc:
379+
raise UseInferenceDefault from exc
380380

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

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

443443
raise UseInferenceDefault
444444

@@ -505,8 +505,8 @@ def infer_property(node, context=None):
505505
getter = node.args[0]
506506
try:
507507
inferred = next(getter.infer(context=context))
508-
except InferenceError:
509-
raise UseInferenceDefault
508+
except InferenceError as exc:
509+
raise UseInferenceDefault from exc
510510

511511
if not isinstance(inferred, (nodes.FunctionDef, nodes.Lambda)):
512512
raise UseInferenceDefault
@@ -673,12 +673,12 @@ def infer_isinstance(callnode, context=None):
673673
class_container = _class_or_tuple_to_container(
674674
class_or_tuple_node, context=context
675675
)
676-
except InferenceError:
677-
raise UseInferenceDefault
676+
except InferenceError as exc:
677+
raise UseInferenceDefault from exc
678678
try:
679679
isinstance_bool = helpers.object_isinstance(obj_node, class_container, context)
680680
except AstroidTypeError as exc:
681-
raise UseInferenceDefault("TypeError: " + str(exc))
681+
raise UseInferenceDefault("TypeError: " + str(exc)) from exc
682682
except MroError as exc:
683683
raise UseInferenceDefault from exc
684684
if isinstance_bool is util.Uninferable:

astroid/brain/brain_namedtuple_enum.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ def _infer_first(node, context):
5151
raise UseInferenceDefault()
5252
else:
5353
return value
54-
except StopIteration:
55-
raise InferenceError()
54+
except StopIteration as exc:
55+
raise InferenceError from exc
5656

5757

5858
def _find_func_form_arguments(node, context):
@@ -88,7 +88,7 @@ def infer_func_form(node, base_type, context=None, enum=False):
8888
name, names = _find_func_form_arguments(node, context)
8989
try:
9090
attributes = names.value.replace(",", " ").split()
91-
except AttributeError:
91+
except AttributeError as exc:
9292
if not enum:
9393
attributes = [
9494
_infer_first(const, context).value for const in names.elts
@@ -117,11 +117,11 @@ def infer_func_form(node, base_type, context=None, enum=False):
117117
_infer_first(const, context).value for const in names.elts
118118
]
119119
else:
120-
raise AttributeError
120+
raise AttributeError from exc
121121
if not attributes:
122-
raise AttributeError
123-
except (AttributeError, exceptions.InferenceError):
124-
raise UseInferenceDefault()
122+
raise AttributeError from exc
123+
except (AttributeError, exceptions.InferenceError) as exc:
124+
raise UseInferenceDefault from exc
125125

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

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

411411
if func.qname() != "typing.NamedTuple":
412412
raise UseInferenceDefault

astroid/brain/brain_random.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ def infer_random_sample(node, context=None):
4747

4848
try:
4949
elts = random.sample(inferred_sequence.elts, length.value)
50-
except ValueError:
51-
raise astroid.UseInferenceDefault
50+
except ValueError as exc:
51+
raise astroid.UseInferenceDefault from exc
5252

5353
new_node = astroid.List(
5454
lineno=node.lineno, col_offset=node.col_offset, parent=node.scope()

astroid/brain/brain_six.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,8 @@ def _six_fail_hook(modname):
145145
attribute = modname[start_index:].lstrip(".").replace(".", "_")
146146
try:
147147
import_attr = module.getattr(attribute)[0]
148-
except AttributeInferenceError:
149-
raise AstroidBuildingError(modname=modname)
148+
except AttributeInferenceError as exc:
149+
raise AstroidBuildingError(modname=modname) from exc
150150
if isinstance(import_attr, nodes.Import):
151151
submodule = MANAGER.ast_from_module_name(import_attr.names[0][0])
152152
return submodule

astroid/decorators.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,9 +134,9 @@ def raise_if_nothing_inferred(func, instance, args, kwargs):
134134
# generator is empty
135135
if error.args:
136136
# pylint: disable=not-a-mapping
137-
raise exceptions.InferenceError(**error.args[0])
137+
raise exceptions.InferenceError(**error.args[0]) from error
138138
raise exceptions.InferenceError(
139139
"StopIteration raised without any error information."
140-
)
140+
) from error
141141

142142
yield from generator

astroid/protocols.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -498,8 +498,9 @@ def _infer_context_manager(self, mgr, context):
498498
elif isinstance(inferred, bases.Instance):
499499
try:
500500
enter = next(inferred.igetattr("__enter__", context=context))
501-
except (exceptions.InferenceError, exceptions.AttributeInferenceError):
502-
raise exceptions.InferenceError(node=inferred)
501+
except (exceptions.InferenceError,
502+
exceptions.AttributeInferenceError) as exc:
503+
raise exceptions.InferenceError(node=inferred) from exc
503504
if not isinstance(enter, bases.BoundMethod):
504505
raise exceptions.InferenceError(node=enter)
505506
yield from enter.infer_call_result(self, context)

astroid/scoped_nodes.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2571,7 +2571,7 @@ def igetattr(self, name, context=None, class_context=True):
25712571
else:
25722572
raise exceptions.InferenceError(
25732573
error.message, target=self, attribute=name, context=context
2574-
)
2574+
) from error
25752575

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

0 commit comments

Comments
 (0)