Skip to content

Commit

Permalink
Fix "function never returning" computation
Browse files Browse the repository at this point in the history
This fixes 2 bugs in the computation:
* `Never` is handled in addition to `NoReturn`.
* Give priority to the explicit `--never-returning-functions` option.

Closes #7565
  • Loading branch information
svenpanne committed Apr 3, 2024
1 parent e5e6ca7 commit e8cab13
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions pylint/checkers/refactoring/refactoring_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Check notice on line 2067 in pylint/checkers/refactoring/refactoring_checker.py

View workflow job for this annotation

GitHub Actions / pylint

R6201

Consider using set for membership test
or isinstance(node.returns, nodes.Name)
and node.returns.name == "NoReturn"
and node.returns.name in ("NoReturn", "Never")

Check notice on line 2069 in pylint/checkers/refactoring/refactoring_checker.py

View workflow job for this annotation

GitHub Actions / pylint

R6201

Consider using set for membership test
)
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
Expand Down

0 comments on commit e8cab13

Please sign in to comment.