From 8ec2b471862b50b8f0197d52658cd0b36045a7a1 Mon Sep 17 00:00:00 2001 From: Andrew Haigh Date: Sat, 24 Apr 2021 09:16:16 +1000 Subject: [PATCH] Update FunctionDef.infer_call_result to infer None with no Returns Ref #485. If the function was inferred (unlike many compiler-builtins) and it contains no Return nodes, then the implicit return value is None. --- astroid/scoped_nodes.py | 2 +- tests/unittest_scoped_nodes.py | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/astroid/scoped_nodes.py b/astroid/scoped_nodes.py index d7717e64e6..5a569428b0 100644 --- a/astroid/scoped_nodes.py +++ b/astroid/scoped_nodes.py @@ -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 diff --git a/tests/unittest_scoped_nodes.py b/tests/unittest_scoped_nodes.py index dd6102c6ff..9197c0d8de 100644 --- a/tests/unittest_scoped_nodes.py +++ b/tests/unittest_scoped_nodes.py @@ -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(): + pass + 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 = """