Skip to content

Commit

Permalink
Rename bad-exception-context to bad-exception-cause
Browse files Browse the repository at this point in the history
  • Loading branch information
Victor Taïx committed Jul 4, 2022
1 parent 2dc0712 commit 899d796
Show file tree
Hide file tree
Showing 11 changed files with 28 additions and 24 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
4 changes: 4 additions & 0 deletions doc/whatsnew/2/2.15/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ Other Changes

Closes #6953

* ``bad-exception-context`` has been renamed to ``bad-exception-cause`` as it is about the cause and not the context.

Closes #3694


Internal changes
================
Expand Down
20 changes: 10 additions & 10 deletions pylint/checkers/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,10 @@ def _is_raising(body: list) -> bool:
"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",
"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 context is not an exception, "
"where the exception cause is not an exception, "
"nor None.",
),
"E0704": (
Expand Down Expand Up @@ -259,7 +259,7 @@ def open(self):
"raising-bad-type",
"raising-non-exception",
"notimplemented-raised",
"bad-exception-context",
"bad-exception-cause",
"raising-format-tuple",
"raise-missing-from",
)
Expand All @@ -271,7 +271,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 @@ -302,22 +302,22 @@ def _check_misplaced_bare_raise(self, node):
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)
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)

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
Expand Up @@ -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 context set to something which is not an exception, nor None:UNDEFINED
bad-exception-cause:17:4:17:34:test:Exception context set to something which is not an exception, nor None:UNDEFINED
bad-exception-cause:23:4:23:36:test:Exception context set to something which is not an exception, nor None:UNDEFINED
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 context set to something which is not an exception, nor None:UNDEFINED
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 899d796

Please sign in to comment.