Skip to content

Commit

Permalink
Fix a crash in inference caused by Uninferable container elements
Browse files Browse the repository at this point in the history
Close #866
  • Loading branch information
PCManticore committed Jan 1, 2021
1 parent 00ccda6 commit 58212bd
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 0 deletions.
4 changes: 4 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ Release Date: TBA

Closes #703

* Fix a crash in inference caused by `Uninferable` container elements

Close #866

* Add `python 3.9` support.

* The flat attribute of ``numpy.ndarray`` is now inferred as an ``numpy.ndarray`` itself.
Expand Down
2 changes: 2 additions & 0 deletions astroid/brain/brain_builtin_inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,8 @@ def _container_generic_transform(arg, context, klass, iterables, build_elts):
# TODO: Does not handle deduplication for sets.
elts = []
for element in arg.elts:
if not element:
continue
inferred = helpers.safe_infer(element, context=context)
if inferred:
evaluated_object = nodes.EvaluatedObject(
Expand Down
14 changes: 14 additions & 0 deletions tests/unittest_inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -5884,5 +5884,19 @@ def test(self):
assert list(inferred.nodes_of_class(nodes.Const)) == []


def test_infer_list_of_uninferables_does_not_crash():
code = """
x = [A] * 1
f = [x, [A] * 2]
x = list(f) + [] # List[Uninferable]
tuple(x[0])
"""
node = extract_node(code)
inferred = next(node.infer())
assert isinstance(inferred, nodes.Tuple)
# Would not be able to infer the first element.
assert not inferred.elts


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

0 comments on commit 58212bd

Please sign in to comment.