diff --git a/doc/whatsnew/fragments/7565.false_positive b/doc/whatsnew/fragments/7565.false_positive new file mode 100644 index 00000000000..647eb4611fa --- /dev/null +++ b/doc/whatsnew/fragments/7565.false_positive @@ -0,0 +1,3 @@ +Fix computation of never-returning function: `Never` is handled in addition to `NoReturn`, and priority is given to the explicit `--never-returning-functions` option. + +Closes #7565. diff --git a/pylint/checkers/refactoring/refactoring_checker.py b/pylint/checkers/refactoring/refactoring_checker.py index edfff0385e7..1af9ce60066 100644 --- a/pylint/checkers/refactoring/refactoring_checker.py +++ b/pylint/checkers/refactoring/refactoring_checker.py @@ -2054,17 +2054,21 @@ def _is_function_def_never_returning( Returns: bool: True if the function never returns, False otherwise. """ - if isinstance(node, (nodes.FunctionDef, astroid.BoundMethod)) and node.returns: - return ( + try: + if node.qname() in self._never_returning_functions: + return True + except (TypeError, AttributeError): + pass + return ( + isinstance(node, (nodes.FunctionDef, astroid.BoundMethod)) + and node.returns + and ( isinstance(node.returns, nodes.Attribute) - and node.returns.attrname == "NoReturn" + and node.returns.attrname in {"NoReturn", "Never"} or isinstance(node.returns, nodes.Name) - and node.returns.name == "NoReturn" + and node.returns.name in {"NoReturn", "Never"} ) - try: - return node.qname() in self._never_returning_functions - except (TypeError, AttributeError): - return False + ) def _check_return_at_the_end(self, node: nodes.FunctionDef) -> None: """Check for presence of a *single* return statement at the end of a