Skip to content

Commit

Permalink
Update FunctionDef.infer_call_result to infer None with no Returns
Browse files Browse the repository at this point in the history
Ref pylint-dev#485. If the function was inferred (unlike many compiler-builtins)
and it contains no Return nodes, then the implicit return value is None.
  • Loading branch information
nelfin committed May 24, 2021
1 parent 2e8417f commit 9bdab20
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
2 changes: 1 addition & 1 deletion astroid/scoped_nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -1744,7 +1744,7 @@ def infer_call_result(self, caller=None, context=None):

first_return = next(returns, None)
if not first_return:
if self.body and isinstance(self.body[-1], node_classes.Assert):
if self.body:
yield node_classes.Const(None)
return

Expand Down
12 changes: 12 additions & 0 deletions tests/unittest_scoped_nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,18 @@ def func():
self.assertIsInstance(func_vals[0], nodes.Const)
self.assertIsNone(func_vals[0].value)

def test_no_returns_is_implicitly_none(self):
code = """
def f():
print('non-empty, non-pass, no return statements')
value = f()
value
"""
node = builder.extract_node(code)
inferred = next(node.infer())
assert isinstance(inferred, nodes.Const)
assert inferred.value is None

def test_func_instance_attr(self):
"""test instance attributes for functions"""
data = """
Expand Down

0 comments on commit 9bdab20

Please sign in to comment.