Skip to content

Commit c8cce4e

Browse files
Better error message
1 parent 72c5e55 commit c8cce4e

File tree

2 files changed

+37
-16
lines changed

2 files changed

+37
-16
lines changed

pylint/checkers/exceptions.py

+30-9
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
from pylint import checkers
1717
from pylint.checkers import utils
18+
from pylint.interfaces import HIGH
1819
from pylint.typing import MessageDefinitionTuple
1920

2021
if TYPE_CHECKING:
@@ -131,7 +132,7 @@ def _is_raising(body: list) -> bool:
131132
"try-except-raise block!",
132133
),
133134
"W0707": (
134-
"Consider explicitly re-raising using 'raise NewError(...) from old_error'",
135+
"Consider explicitly re-raising using %s'%s from %s'",
135136
"raise-missing-from",
136137
"Python 3's exception chaining means it shows the traceback of the "
137138
"current exception, but also the original exception. Not using `raise "
@@ -333,19 +334,39 @@ def _check_raise_missing_from(self, node: nodes.Raise) -> None:
333334
# like `exc.with_traceback(whatever)`. We won't analyze these, we'll just assume
334335
# there's a violation on two simple cases: `raise SomeException(whatever)` and `raise
335336
# SomeException`.
337+
338+
need_to_raise = False
339+
except_as_exc = ""
340+
exception_name = "exc"
336341
if containing_except_node.name is None:
337342
# The `except` doesn't have an `as exception:` part, meaning there's no way that
338343
# the `raise` is raising the same exception.
339-
self.add_message("raise-missing-from", node=node)
340-
elif isinstance(node.exc, nodes.Call) and isinstance(node.exc.func, nodes.Name):
341-
# We have a `raise SomeException(whatever)`.
342-
self.add_message("raise-missing-from", node=node)
344+
if isinstance(containing_except_node.type, nodes.Name):
345+
class_of_old_error = containing_except_node.type.name
346+
elif isinstance(containing_except_node.type, nodes.Tuple):
347+
class_of_old_error = ", ".join(
348+
n.name for n in containing_except_node.type.elts
349+
)
350+
except_as_exc = f"'except {class_of_old_error} as {exception_name}' and "
351+
need_to_raise = True
343352
elif (
344-
isinstance(node.exc, nodes.Name)
345-
and node.exc.name != containing_except_node.name.name
353+
isinstance(node.exc, nodes.Call)
354+
and isinstance(node.exc.func, nodes.Name)
355+
or (
356+
isinstance(node.exc, nodes.Name)
357+
and node.exc.name != containing_except_node.name.name
358+
)
346359
):
347-
# We have a `raise SomeException`.
348-
self.add_message("raise-missing-from", node=node)
360+
# We have a `raise SomeException(whatever)` or a `raise SomeException`
361+
exception_name = containing_except_node.name.name
362+
need_to_raise = True
363+
if need_to_raise:
364+
self.add_message(
365+
"raise-missing-from",
366+
node=node,
367+
args=(except_as_exc, node.as_string(), exception_name),
368+
confidence=HIGH,
369+
)
349370

350371
def _check_catching_non_exception(self, handler, exc, part):
351372
if isinstance(exc, nodes.Tuple):
+7-7
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
raise-missing-from:11:4:11:18::Consider explicitly re-raising using 'raise NewError(...) from old_error':UNDEFINED
2-
raise-missing-from:19:4:19:25::Consider explicitly re-raising using 'raise NewError(...) from old_error':UNDEFINED
3-
raise-missing-from:25:4:25:18::Consider explicitly re-raising using 'raise NewError(...) from old_error':UNDEFINED
4-
raise-missing-from:31:4:31:18::Consider explicitly re-raising using 'raise NewError(...) from old_error':UNDEFINED
5-
raise-missing-from:45:20:45:34::Consider explicitly re-raising using 'raise NewError(...) from old_error':UNDEFINED
6-
raise-missing-from:53:4:53:20::Consider explicitly re-raising using 'raise NewError(...) from old_error':UNDEFINED
7-
raise-missing-from:59:4:59:47::Consider explicitly re-raising using 'raise NewError(...) from old_error':UNDEFINED
1+
raise-missing-from:11:4:11:18::Consider explicitly re-raising using 'except ZeroDivisionError as exc' and 'raise KeyError from exc':HIGH
2+
raise-missing-from:19:4:19:25::Consider explicitly re-raising using 'except ZeroDivisionError as exc' and 'raise (foo + bar).baz from exc':HIGH
3+
raise-missing-from:25:4:25:18::Consider explicitly re-raising using 'raise KeyError from e':HIGH
4+
raise-missing-from:31:4:31:18::Consider explicitly re-raising using 'raise KeyError from e':HIGH
5+
raise-missing-from:45:20:45:34::Consider explicitly re-raising using 'raise KeyError from e':HIGH
6+
raise-missing-from:53:4:53:20::Consider explicitly re-raising using 'raise KeyError() from e':HIGH
7+
raise-missing-from:59:4:59:47::Consider explicitly re-raising using 'raise KeyError(whatever, whatever=whatever) from e':HIGH

0 commit comments

Comments
 (0)