From d653ffd8d38ee53257735cd024b793ff34883d50 Mon Sep 17 00:00:00 2001 From: Pierre Sassoulas Date: Thu, 9 Mar 2023 12:51:07 +0100 Subject: [PATCH 1/2] [overgeneral-exceptions] Only handle qualified names --- doc/whatsnew/fragments/8410.user_action | 7 ++++++ pylint/checkers/exceptions.py | 29 ++++++++++--------------- 2 files changed, 19 insertions(+), 17 deletions(-) create mode 100644 doc/whatsnew/fragments/8410.user_action diff --git a/doc/whatsnew/fragments/8410.user_action b/doc/whatsnew/fragments/8410.user_action new file mode 100644 index 0000000000..e0ef3923cd --- /dev/null +++ b/doc/whatsnew/fragments/8410.user_action @@ -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 #8410 diff --git a/pylint/checkers/exceptions.py b/pylint/checkers/exceptions.py index c2bd246d7f..f28ff5ff8e 100644 --- a/pylint/checkers/exceptions.py +++ b/pylint/checkers/exceptions.py @@ -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, @@ -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, @@ -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: From a05bc74740cc58f4ff32ef5cce82cfa256e1d73c Mon Sep 17 00:00:00 2001 From: Pierre Sassoulas Date: Thu, 9 Mar 2023 13:24:38 +0100 Subject: [PATCH 2/2] fix changelog --- doc/whatsnew/fragments/{8410.user_action => 8411.user_action} | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename doc/whatsnew/fragments/{8410.user_action => 8411.user_action} (96%) diff --git a/doc/whatsnew/fragments/8410.user_action b/doc/whatsnew/fragments/8411.user_action similarity index 96% rename from doc/whatsnew/fragments/8410.user_action rename to doc/whatsnew/fragments/8411.user_action index e0ef3923cd..42f9c502ab 100644 --- a/doc/whatsnew/fragments/8410.user_action +++ b/doc/whatsnew/fragments/8411.user_action @@ -4,4 +4,4 @@ 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 #8410 +Refs #8411