Skip to content

Commit

Permalink
assert only functions are properly inferred as returning None
Browse files Browse the repository at this point in the history
Close #668
  • Loading branch information
PCManticore committed Jun 2, 2019
1 parent 33065ec commit 4575339
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
4 changes: 4 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ Release Date: TBA
Close PyCQA/pylint#2326
Close PyCQA/pylint#2021

* ``assert`` only functions are properly inferred as returning ``None``

Close #668

* Add support for Python 3.8's `NamedExpr` nodes, which is part of assignment expressions.

Close #674
Expand Down
8 changes: 7 additions & 1 deletion astroid/scoped_nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -1656,7 +1656,13 @@ def infer_call_result(self, caller=None, context=None):

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

raise exceptions.InferenceError(
"The function does not have any return statements"
)

for returnnode in itertools.chain((first_return,), returns):
if returnnode.value is None:
Expand Down
14 changes: 14 additions & 0 deletions astroid/tests/unittest_inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -5289,5 +5289,19 @@ def both_branches():
assert [third[0].value, third[1].value] == [1, 2]


def test_assert_last_function_returns_none_on_inference():
code = """
def check_equal(a, b):
res = do_something_with_these(a, b)
assert a == b == res
check_equal(a, b)
"""
node = extract_node(code)
inferred = next(node.infer())
assert isinstance(inferred, nodes.Const)
assert inferred.value is None


if __name__ == "__main__":
unittest.main()

0 comments on commit 4575339

Please sign in to comment.