Skip to content

Commit

Permalink
Rename bad-exception-context to bad-exception-cause (#7125)
Browse files Browse the repository at this point in the history
Fixes #3694

Co-authored-by: Victor Taïx <victor.taix@grenoble-inp.org>
Co-authored-by: Pierre Sassoulas <pierre.sassoulas@gmail.com>
  • Loading branch information
3 people authored Jul 18, 2022
1 parent 893b173 commit bc247dc
Show file tree
Hide file tree
Showing 11 changed files with 35 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ def divide(x, y):
try:
result = x / y
except ZeroDivisionError:
raise ValueError(f"Division by zero when dividing {x} by {y} !") from result # [bad-exception-context]
raise ValueError(f"Division by zero when dividing {x} by {y} !") from result # [bad-exception-cause]
return result
4 changes: 2 additions & 2 deletions doc/user_guide/checkers/features.rst
Original file line number Diff line number Diff line change
Expand Up @@ -420,8 +420,8 @@ Exceptions checker Messages
:catching-non-exception (E0712): *Catching an exception which doesn't inherit from Exception: %s*
Used when a class which doesn't inherit from Exception is used as an
exception in an except clause.
:bad-exception-context (E0703): *Exception context set to something which is not an exception, nor None*
Used when using the syntax "raise ... from ...", where the exception context
:bad-exception-cause (E0703): *Exception cause set to something which is not an exception, nor None*
Used when using the syntax "raise ... from ...", where the exception cause
is not an exception, nor None.
:notimplemented-raised (E0711): *NotImplemented raised - should raise NotImplementedError*
Used when NotImplemented is raised instead of NotImplementedError
Expand Down
2 changes: 1 addition & 1 deletion doc/user_guide/messages/messages_overview.rst
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ All messages in the error category:
error/await-outside-async
error/bad-configuration-section
error/bad-except-order
error/bad-exception-context
error/bad-exception-cause
error/bad-format-character
error/bad-plugin-value
error/bad-reversed-sequence
Expand Down
3 changes: 3 additions & 0 deletions doc/whatsnew/fragments/3694.other
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
``bad-exception-context`` has been renamed to ``bad-exception-cause`` as it is about the cause and not the context.

Closes #3694
35 changes: 17 additions & 18 deletions pylint/checkers/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

from pylint import checkers
from pylint.checkers import utils
from pylint.interfaces import HIGH
from pylint.interfaces import HIGH, INFERENCE
from pylint.typing import MessageDefinitionTuple

if TYPE_CHECKING:
Expand Down Expand Up @@ -61,9 +61,7 @@ def _is_raising(body: list[nodes.NodeNG]) -> bool:

OVERGENERAL_EXCEPTIONS = ("BaseException", "Exception")

MSGS: dict[
str, MessageDefinitionTuple
] = { # pylint: disable=consider-using-namedtuple-or-dataclass
MSGS: dict[str, MessageDefinitionTuple] = {
"E0701": (
"Bad except clauses order (%s)",
"bad-except-order",
Expand All @@ -77,13 +75,6 @@ def _is_raising(body: list[nodes.NodeNG]) -> bool:
"Used when something which is neither a class nor an instance "
"is raised (i.e. a `TypeError` will be raised).",
),
"E0703": (
"Exception context set to something which is not an exception, nor None",
"bad-exception-context",
'Used when using the syntax "raise ... from ...", '
"where the exception context is not an exception, "
"nor None.",
),
"E0704": (
"The raise statement is not inside an except clause",
"misplaced-bare-raise",
Expand All @@ -94,6 +85,14 @@ def _is_raising(body: list[nodes.NodeNG]) -> bool:
"as an exception is raised inside the try block, but it is "
"nevertheless a code smell that must not be relied upon.",
),
"E0705": (
"Exception cause set to something which is not an exception, nor None",
"bad-exception-cause",
'Used when using the syntax "raise ... from ...", '
"where the exception cause is not an exception, "
"nor None.",
{"old_names": [("E0703", "bad-exception-context")]},
),
"E0710": (
"Raising a new style class which doesn't inherit from BaseException",
"raising-non-exception",
Expand Down Expand Up @@ -262,7 +261,7 @@ def open(self) -> None:
"raising-bad-type",
"raising-non-exception",
"notimplemented-raised",
"bad-exception-context",
"bad-exception-cause",
"raising-format-tuple",
"raise-missing-from",
)
Expand All @@ -274,7 +273,7 @@ def visit_raise(self, node: nodes.Raise) -> None:
if node.cause is None:
self._check_raise_missing_from(node)
else:
self._check_bad_exception_context(node)
self._check_bad_exception_cause(node)

expr = node.exc
ExceptionRaiseRefVisitor(self, node).visit(expr)
Expand Down Expand Up @@ -305,22 +304,22 @@ def _check_misplaced_bare_raise(self, node: nodes.Raise) -> None:
if not current or not isinstance(current.parent, expected):
self.add_message("misplaced-bare-raise", node=node)

def _check_bad_exception_context(self, node: nodes.Raise) -> None:
"""Verify that the exception context is properly set.
def _check_bad_exception_cause(self, node: nodes.Raise) -> None:
"""Verify that the exception cause is properly set.
An exception context can be only `None` or an exception.
An exception cause can be only `None` or an exception.
"""
cause = utils.safe_infer(node.cause)
if cause in (astroid.Uninferable, None):
return

if isinstance(cause, nodes.Const):
if cause.value is not None:
self.add_message("bad-exception-context", node=node)
self.add_message("bad-exception-cause", node=node, confidence=INFERENCE)
elif not isinstance(cause, nodes.ClassDef) and not utils.inherit_from_std_ex(
cause
):
self.add_message("bad-exception-context", node=node)
self.add_message("bad-exception-cause", node=node, confidence=INFERENCE)

def _check_raise_missing_from(self, node: nodes.Raise) -> None:
if node.exc is None:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""Check that raise ... from .. uses a proper exception context """
"""Check that raise ... from .. uses a proper exception cause """

# pylint: disable=unreachable, import-error, multiple-imports

Expand All @@ -11,16 +11,16 @@ class ExceptionSubclass(Exception):

def test():
""" docstring """
raise IndexError from 1 # [bad-exception-context]
raise IndexError from 1 # [bad-exception-cause]
raise IndexError from None
raise IndexError from ZeroDivisionError
raise IndexError from object() # [bad-exception-context]
raise IndexError from object() # [bad-exception-cause]
raise IndexError from ExceptionSubclass
raise IndexError from socket.error
raise IndexError() from None
raise IndexError() from ZeroDivisionError
raise IndexError() from ZeroDivisionError()
raise IndexError() from object() # [bad-exception-context]
raise IndexError() from object() # [bad-exception-cause]
raise IndexError() from unknown

def function():
Expand All @@ -29,4 +29,4 @@ def function():
try:
pass
except function as exc: # [catching-non-exception]
raise Exception from exc # [bad-exception-context]
raise Exception from exc # [bad-exception-cause]
5 changes: 5 additions & 0 deletions tests/functional/b/bad_exception_cause.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
bad-exception-cause:14:4:14:27:test:Exception cause set to something which is not an exception, nor None:INFERENCE
bad-exception-cause:17:4:17:34:test:Exception cause set to something which is not an exception, nor None:INFERENCE
bad-exception-cause:23:4:23:36:test:Exception cause set to something which is not an exception, nor None:INFERENCE
catching-non-exception:31:7:31:15::"Catching an exception which doesn't inherit from Exception: function":UNDEFINED
bad-exception-cause:32:4:32:28::Exception cause set to something which is not an exception, nor None:INFERENCE
5 changes: 0 additions & 5 deletions tests/functional/b/bad_exception_context.txt

This file was deleted.

2 changes: 1 addition & 1 deletion tests/functional/r/raising/raising_non_exception.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""The following code should emit a raising-non-exception.
Previously, it didn't, due to a bug in the check for bad-exception-context,
Previously, it didn't, due to a bug in the check for bad-exception-cause,
which prevented further checking on the Raise node.
"""
# pylint: disable=import-error, too-few-public-methods, useless-object-inheritance
Expand Down

0 comments on commit bc247dc

Please sign in to comment.