diff options
author | Claudiu Popa <pcmanticore@gmail.com> | 2020-03-10 11:17:42 +0100 |
---|---|---|
committer | Claudiu Popa <pcmanticore@gmail.com> | 2020-03-10 11:17:42 +0100 |
commit | 3de18c673972e238f600be3e56401df73717ecd8 (patch) | |
tree | dbef0f21dcf4f3305e4de0e8ccecf55592595342 /astroid/helpers.py | |
parent | f3863b1d6d7ce5219f98062e7eda97f932e676cc (diff) | |
download | astroid-git-3de18c673972e238f600be3e56401df73717ecd8.tar.gz |
Infer the __len__ result of a subclass of an integer
Rather than failing the inference altogether, we can infer the default
int value for subclasses of ints.
Diffstat (limited to 'astroid/helpers.py')
-rw-r--r-- | astroid/helpers.py | 13 |
1 files changed, 11 insertions, 2 deletions
diff --git a/astroid/helpers.py b/astroid/helpers.py index be133b38..d94c9542 100644 --- a/astroid/helpers.py +++ b/astroid/helpers.py @@ -254,12 +254,16 @@ def object_len(node, context=None): return len(inferred_node.elts) if isinstance(inferred_node, nodes.Dict): return len(inferred_node.items) + + node_type = object_type(inferred_node, context=context) + if not node_type: + raise exceptions.InferenceError(node=node) + try: - node_type = object_type(inferred_node, context=context) len_call = next(node_type.igetattr("__len__", context=context)) except exceptions.AttributeInferenceError: raise exceptions.AstroidTypeError( - "object of type '{}' has no len()".format(len_call.pytype()) + "object of type '{}' has no len()".format(node_type.pytype()) ) result_of_len = next(len_call.infer_call_result(node, context)) @@ -268,6 +272,11 @@ def object_len(node, context=None): 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" + ): + # Fake a result as we don't know the arguments of the instance call. + return 0 raise exceptions.AstroidTypeError( "'{}' object cannot be interpreted as an integer".format(result_of_len) ) |