From 3de18c673972e238f600be3e56401df73717ecd8 Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Tue, 10 Mar 2020 11:17:42 +0100 Subject: 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. --- astroid/helpers.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) (limited to 'astroid') 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) ) -- cgit v1.2.1