Skip to content

Commit

Permalink
Handle inference fail when calculating lenght of a node
Browse files Browse the repository at this point in the history
  • Loading branch information
Pierre-Sassoulas committed Jun 30, 2021
1 parent 366daef commit b4a283f
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
2 changes: 2 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ What's New in astroid 2.6.2?
============================
Release date: TBA

* Fix a crash when the inference of the length of a node failed

Closes PyCQA/pylint#4633

What's New in astroid 2.6.1?
============================
Expand Down
11 changes: 8 additions & 3 deletions astroid/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -290,14 +290,19 @@ def object_len(node, context=None):
f"object of type '{node_type.pytype()}' has no len()"
) from e

result_of_len = next(len_call.infer_call_result(node, context))
inferred = len_call.infer_call_result(node, context)
if inferred is util.Uninferable:
raise InferenceError(node=node, context=context)
result_of_len = next(inferred, None)
if (
isinstance(result_of_len, nodes.Const)
and result_of_len.pytype() == "builtins.int"
):
return result_of_len.value
if isinstance(result_of_len, bases.Instance) and result_of_len.is_subtype_of(
"builtins.int"
if (
result_of_len is None
or isinstance(result_of_len, bases.Instance)
and result_of_len.is_subtype_of("builtins.int")
):
# Fake a result as we don't know the arguments of the instance call.
return 0
Expand Down

0 comments on commit b4a283f

Please sign in to comment.