summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPierre Sassoulas <pierre.sassoulas@gmail.com>2021-06-30 11:08:14 +0200
committerPierre Sassoulas <pierre.sassoulas@gmail.com>2021-06-30 11:32:12 +0200
commitb4a283f388f495df76489dd6226056953153138e (patch)
treee45154dac8ecb2b7011f29dbe6a0b6d32d4d2e10
parent366daef96f144f5d68843a6c00a67fb800e4a58c (diff)
downloadastroid-git-b4a283f388f495df76489dd6226056953153138e.tar.gz
Handle inference fail when calculating lenght of a node
Closes https://github.com/PyCQA/pylint/issues/4633
-rw-r--r--ChangeLog2
-rw-r--r--astroid/helpers.py11
2 files changed, 10 insertions, 3 deletions
diff --git a/ChangeLog b/ChangeLog
index 5abe8391..3da3ef50 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -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?
============================
diff --git a/astroid/helpers.py b/astroid/helpers.py
index c32b5e0f..3b6ddee2 100644
--- a/astroid/helpers.py
+++ b/astroid/helpers.py
@@ -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