Skip to content

[overgeneral-exceptions] Only handle qualified names #8411

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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