Skip to content

Commit

Permalink
[overgeneral-exceptions] Only handle qualified names (#8411)
Browse files Browse the repository at this point in the history
* [overgeneral-exceptions] Only handle qualified names

* fix changelog
  • Loading branch information
Pierre-Sassoulas authored Mar 9, 2023
1 parent b2ab82a commit 62889d4
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 17 deletions.
7 changes: 7 additions & 0 deletions doc/whatsnew/fragments/8411.user_action
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
The 'overgeneral-exceptions' option now only takes fully qualified name
into account (``builtins.Exception`` not ``Exception``). If you overrode
this option, you need to use the fully qualified name now.

There's still a warning, but it will be removed in 3.1.0.

Refs #8411
29 changes: 12 additions & 17 deletions pylint/checkers/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,16 +210,17 @@ def visit_name(self, node: nodes.Name) -> None:
"notimplemented-raised", node=self._node, confidence=HIGH
)
return

try:
exceptions = list(_annotated_unpack_infer(node))
exceptions = [
c
for _, c in _annotated_unpack_infer(node)
if isinstance(c, nodes.ClassDef)
]
except astroid.InferenceError:
return

for _, exception in exceptions:
if isinstance(
exception, nodes.ClassDef
) and self._checker._is_overgeneral_exception(exception):
for exception in exceptions:
if self._checker._is_overgeneral_exception(exception):
self._checker.add_message(
"broad-exception-raised",
args=exception.name,
Expand Down Expand Up @@ -306,13 +307,13 @@ class ExceptionsChecker(checkers.BaseChecker):

def open(self) -> None:
self._builtin_exceptions = _builtin_exceptions()
# TODO 3.1: Remove this check and put it elsewhere
for exc_name in self.linter.config.overgeneral_exceptions:
if "." not in exc_name:
warnings.warn_explicit(
"Specifying exception names in the overgeneral-exceptions option"
" without module name is deprecated and support for it"
" will be removed in pylint 3.0."
f" Use fully qualified name (maybe 'builtins.{exc_name}' ?) instead.",
f"'{exc_name}' is not a proper value for the 'overgeneral-exceptions' option. "
f"Use fully qualified name (maybe 'builtins.{exc_name}' ?) instead. "
"This will cease to be checked at runtime in 3.1.0.",
category=UserWarning,
filename="pylint: Command line or configuration file",
lineno=1,
Expand Down Expand Up @@ -646,13 +647,7 @@ def visit_tryexcept(self, node: nodes.TryExcept) -> None:
exceptions_classes += [exc for _, exc in exceptions]

def _is_overgeneral_exception(self, exception: nodes.ClassDef) -> bool:
return (
exception.qname() in self.linter.config.overgeneral_exceptions
# TODO: 3.0: not a qualified name, deprecated
or "." not in exception.name
and exception.name in self.linter.config.overgeneral_exceptions
and exception.root().name == utils.EXCEPTIONS_MODULE
)
return exception.qname() in self.linter.config.overgeneral_exceptions


def register(linter: PyLinter) -> None:
Expand Down

0 comments on commit 62889d4

Please sign in to comment.