Skip to content

Commit f1925f4

Browse files
Fix crash in refactoring checker when calling bound lambda (#9867)
Fixes: ``` File "sources/pylint/pylint/checkers/refactoring/refactoring_checker.py", line 2094, in _is_function_def_never_returning and node.returns ^^^^^^^^^^^^ File "sources/pylint/.venv/lib/python3.11/site-packages/astroid/bases.py", line 138, in __getattr__ return getattr(self._proxied, name) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ AttributeError: 'Lambda' object has no attribute 'returns' ``` Crash is reproducible if you have something like this: ```python class C: eq = lambda self, y: self == y ``` As a workaround, use a normal function instead of a lambda. Closes #9865 (cherry picked from commit b78deb6) Co-authored-by: Hashem Nasarat <hashem@hudson-trading.com>
1 parent 7d1626c commit f1925f4

File tree

3 files changed

+23
-7
lines changed

3 files changed

+23
-7
lines changed

doc/whatsnew/fragments/9865.bugfix

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fix crash in refactoring checker when calling a lambda bound as a method.
2+
3+
Closes #9865

pylint/checkers/refactoring/refactoring_checker.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2083,13 +2083,18 @@ def _is_function_def_never_returning(
20832083
Returns:
20842084
bool: True if the function never returns, False otherwise.
20852085
"""
2086-
if isinstance(node, (nodes.FunctionDef, astroid.BoundMethod)) and node.returns:
2087-
return (
2088-
isinstance(node.returns, nodes.Attribute)
2089-
and node.returns.attrname == "NoReturn"
2090-
or isinstance(node.returns, nodes.Name)
2091-
and node.returns.name == "NoReturn"
2092-
)
2086+
if isinstance(node, (nodes.FunctionDef, astroid.BoundMethod)):
2087+
try:
2088+
returns: nodes.NodeNG | None = node.returns
2089+
except AttributeError:
2090+
return False # the BoundMethod proxy may be a lambda without a returns
2091+
if returns is not None:
2092+
return (
2093+
isinstance(returns, nodes.Attribute)
2094+
and returns.attrname == "NoReturn"
2095+
or isinstance(returns, nodes.Name)
2096+
and returns.name == "NoReturn"
2097+
)
20932098
try:
20942099
return node.qname() in self._never_returning_functions
20952100
except (TypeError, AttributeError):
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
"""Regression for https://github.com/pylint-dev/pylint/issues/9865."""
2+
# pylint: disable=missing-docstring,too-few-public-methods,unnecessary-lambda-assignment
3+
class C:
4+
eq = lambda self, y: self == y
5+
6+
def test_lambda_method():
7+
ret = C().eq(1)
8+
return ret

0 commit comments

Comments
 (0)