From 77e205dc64ead82e38bb901e08117ef6f6162c5c Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Tue, 10 Mar 2020 10:33:35 +0100 Subject: Add a new EvaluatedObject container This container is used to store values that have already been evaluated. For instance, 79d5a3a783cf0b5a729e4e467508e955a0cca55f added support for inferring `tuple()` call arguments, but as a result, the `elts` of a `Tuple` can be objects not *references*. As a result, `Tuple.elts` can contain class objects rather than references (names) to class object. The `EvaluatedObject` helps with that, as we still have to call `.infer()` (albeit a no-op) to grab the inferred value of an element. --- astroid/as_string.py | 3 +++ astroid/brain/brain_builtin_inference.py | 11 ++++++++--- astroid/node_classes.py | 34 ++++++++++++++++++++++++++++++++ astroid/nodes.py | 1 + 4 files changed, 46 insertions(+), 3 deletions(-) (limited to 'astroid') diff --git a/astroid/as_string.py b/astroid/as_string.py index 84038ba3..da677a79 100644 --- a/astroid/as_string.py +++ b/astroid/as_string.py @@ -608,6 +608,9 @@ class AsStringVisitor: def visit_property(self, node): return node.function.accept(self) + def visit_evaluatedobject(self, node): + return node.original.accept(self) + def _import_string(names): """return a list of (name, asname) formatted as a string""" diff --git a/astroid/brain/brain_builtin_inference.py b/astroid/brain/brain_builtin_inference.py index b8c2b0ea..7e18e820 100644 --- a/astroid/brain/brain_builtin_inference.py +++ b/astroid/brain/brain_builtin_inference.py @@ -177,9 +177,14 @@ def _container_generic_transform(arg, context, klass, iterables, build_elts): elts = [elt.value for elt in arg.elts] else: # TODO: Does not handle deduplication for sets. - elts = filter( - None, map(partial(helpers.safe_infer, context=context), arg.elts) - ) + elts = [] + for element in arg.elts: + inferred = helpers.safe_infer(element, context=context) + if inferred: + evaluated_object = nodes.EvaluatedObject( + original=element, value=inferred + ) + elts.append(evaluated_object) elif isinstance(arg, nodes.Dict): # Dicts need to have consts as strings already. if not all(isinstance(elt[0], nodes.Const) for elt in arg.items): diff --git a/astroid/node_classes.py b/astroid/node_classes.py index 2e03f4ea..f16b61bd 100644 --- a/astroid/node_classes.py +++ b/astroid/node_classes.py @@ -4726,6 +4726,40 @@ class Unknown(mixins.AssignTypeMixin, NodeNG): yield util.Uninferable +class EvaluatedObject(NodeNG): + """Contains an object that has already been inferred + + This class is useful to pre-evaluate a particular node, + with the resulting class acting as the non-evaluated node. + """ + + name = "EvaluatedObject" + _astroid_fields = ("original",) + _other_fields = ("value",) + + original = None + """The original node that has already been evaluated + + :type: NodeNG + """ + + value = None + """The inferred value + + :type: Union[Uninferable, NodeNG] + """ + + def __init__(self, original, value): + self.original = original + self.value = value + self.lineno = self.original.lineno + self.parent = self.original.parent + self.col_offset = self.original.col_offset + + def infer(self, context=None, **kwargs): + yield self.value + + # constants ############################################################## CONST_CLS = { diff --git a/astroid/nodes.py b/astroid/nodes.py index bf6911ac..82f425ca 100644 --- a/astroid/nodes.py +++ b/astroid/nodes.py @@ -86,6 +86,7 @@ from astroid.node_classes import ( # Node not present in the builtin ast module. DictUnpack, Unknown, + EvaluatedObject, ) from astroid.scoped_nodes import ( Module, -- cgit v1.2.1