From aa4f5bed58dc6521a6c2c0927ca0e0da48fd5ea5 Mon Sep 17 00:00:00 2001 From: Tushar Sadhwani <86737547+tushar-deepsource@users.noreply.github.com> Date: Wed, 29 Dec 2021 20:53:33 +0530 Subject: Add future=True to frame calls (#1305) Co-authored-by: Pierre Sassoulas Co-authored-by: Jacob Walls --- astroid/arguments.py | 2 +- astroid/bases.py | 4 ++-- astroid/builder.py | 4 ++-- astroid/helpers.py | 2 +- astroid/nodes/node_classes.py | 6 +++--- astroid/nodes/scoped_nodes/scoped_nodes.py | 26 ++++++++++++++------------ astroid/protocols.py | 2 +- 7 files changed, 24 insertions(+), 22 deletions(-) (limited to 'astroid') diff --git a/astroid/arguments.py b/astroid/arguments.py index fadb5a8b..9ae8bab2 100644 --- a/astroid/arguments.py +++ b/astroid/arguments.py @@ -226,7 +226,7 @@ class CallSite: return positional[0].infer(context=context) if boundnode is None: # XXX can do better ? - boundnode = funcnode.parent.frame() + boundnode = funcnode.parent.frame(future=True) if isinstance(boundnode, nodes.ClassDef): # Verify that we're accessing a method diff --git a/astroid/bases.py b/astroid/bases.py index c39e887a..9fbdc3a3 100644 --- a/astroid/bases.py +++ b/astroid/bases.py @@ -368,7 +368,7 @@ class UnboundMethod(Proxy): special_attributes = lazy_descriptor(lambda: objectmodel.UnboundMethodModel()) def __repr__(self): - frame = self._proxied.parent.frame() + frame = self._proxied.parent.frame(future=True) return "<{} {} of {} at 0x{}".format( self.__class__.__name__, self._proxied.name, frame.qname(), id(self) ) @@ -404,7 +404,7 @@ class UnboundMethod(Proxy): # instance of the class given as first argument. if ( self._proxied.name == "__new__" - and self._proxied.parent.frame().qname() == "builtins.object" + and self._proxied.parent.frame(future=True).qname() == "builtins.object" ): if caller.args: node_context = context.extra_context.get(caller.args[0]) diff --git a/astroid/builder.py b/astroid/builder.py index ea9d7dc8..150eeed8 100644 --- a/astroid/builder.py +++ b/astroid/builder.py @@ -234,7 +234,7 @@ class AstroidBuilder(raw_building.InspectBuilder): This adds name to locals and handle members definition. """ try: - frame = node.frame() + frame = node.frame(future=True) for inferred in node.expr.infer(): if inferred is util.Uninferable: continue @@ -263,7 +263,7 @@ class AstroidBuilder(raw_building.InspectBuilder): if ( frame.name == "__init__" and values - and values[0].frame().name != "__init__" + and values[0].frame(future=True).name != "__init__" ): values.insert(0, node) else: diff --git a/astroid/helpers.py b/astroid/helpers.py index eadd8cb6..6e74558b 100644 --- a/astroid/helpers.py +++ b/astroid/helpers.py @@ -255,7 +255,7 @@ def object_len(node, context=None): # prevent self referential length calls from causing a recursion error # see https://github.com/PyCQA/astroid/issues/777 - node_frame = node.frame() + node_frame = node.frame(future=True) if ( isinstance(node_frame, scoped_nodes.FunctionDef) and node_frame.name == "__len__" diff --git a/astroid/nodes/node_classes.py b/astroid/nodes/node_classes.py index 4e33bb5d..0f196812 100644 --- a/astroid/nodes/node_classes.py +++ b/astroid/nodes/node_classes.py @@ -4815,9 +4815,9 @@ class NamedExpr(mixins.AssignTypeMixin, NodeNG): raise ParentMissingError(target=self.parent) if not self.parent.parent.parent: raise ParentMissingError(target=self.parent.parent) - return self.parent.parent.parent.frame() + return self.parent.parent.parent.frame(future=True) - return self.parent.frame() + return self.parent.frame(future=True) def scope(self) -> "LocalsDictNodeNG": """The first parent node defining a new scope. @@ -4849,7 +4849,7 @@ class NamedExpr(mixins.AssignTypeMixin, NodeNG): :param stmt: The statement that defines the given name. """ - self.frame().set_local(name, stmt) + self.frame(future=True).set_local(name, stmt) class Unknown(mixins.AssignTypeMixin, NodeNG): diff --git a/astroid/nodes/scoped_nodes/scoped_nodes.py b/astroid/nodes/scoped_nodes/scoped_nodes.py index f2720c4c..15e9c1c9 100644 --- a/astroid/nodes/scoped_nodes/scoped_nodes.py +++ b/astroid/nodes/scoped_nodes/scoped_nodes.py @@ -244,7 +244,7 @@ class LocalsDictNodeNG(node_classes.LookupMixIn, node_classes.NodeNG): # pylint: disable=no-member; github.com/pycqa/astroid/issues/278 if self.parent is None: return self.name - return f"{self.parent.frame().qname()}.{self.name}" + return f"{self.parent.frame(future=True).qname()}.{self.name}" def scope(self: T) -> T: """The first parent node defining a new scope. @@ -1452,7 +1452,7 @@ class Lambda(mixins.FilterStmtsMixin, LocalsDictNodeNG): :rtype: tuple(str, list(NodeNG)) """ if node in self.args.defaults or node in self.args.kw_defaults: - frame = self.parent.frame() + frame = self.parent.frame(future=True) # line offset to avoid that def func(f=func) resolve the default # value to the defined function offset = -1 @@ -1595,7 +1595,7 @@ class FunctionDef(mixins.MultiLineBlockMixin, node_classes.Statement, Lambda): parent=parent, ) if parent: - frame = parent.frame() + frame = parent.frame(future=True) frame.set_local(name, self) # pylint: disable=arguments-differ; different than Lambdas @@ -1641,7 +1641,7 @@ class FunctionDef(mixins.MultiLineBlockMixin, node_classes.Statement, Lambda): :type: list(NodeNG) """ - frame = self.parent.frame() + frame = self.parent.frame(future=True) if not isinstance(frame, ClassDef): return [] @@ -1668,7 +1668,7 @@ class FunctionDef(mixins.MultiLineBlockMixin, node_classes.Statement, Lambda): # original method. if ( isinstance(meth, FunctionDef) - and assign_node.frame() == frame + and assign_node.frame(future=True) == frame ): decorators.append(assign.value) return decorators @@ -1687,7 +1687,7 @@ class FunctionDef(mixins.MultiLineBlockMixin, node_classes.Statement, Lambda): if decorator.func.name in BUILTIN_DESCRIPTORS: return decorator.func.name - frame = self.parent.frame() + frame = self.parent.frame(future=True) type_name = "function" if isinstance(frame, ClassDef): if self.name == "__new__": @@ -1815,7 +1815,9 @@ class FunctionDef(mixins.MultiLineBlockMixin, node_classes.Statement, Lambda): """ # check we are defined in a ClassDef, because this is usually expected # (e.g. pylint...) when is_method() return True - return self.type != "function" and isinstance(self.parent.frame(), ClassDef) + return self.type != "function" and isinstance( + self.parent.frame(future=True), ClassDef + ) @decorators_mod.cached def decoratornames(self, context=None): @@ -1999,7 +2001,7 @@ class FunctionDef(mixins.MultiLineBlockMixin, node_classes.Statement, Lambda): # if any methods in a class body refer to either __class__ or super. # In our case, we want to be able to look it up in the current scope # when `__class__` is being used. - frame = self.parent.frame() + frame = self.parent.frame(future=True) if isinstance(frame, ClassDef): return self, [frame] return super().scope_lookup(node, name, offset) @@ -2124,12 +2126,12 @@ def get_wrapping_class(node): :rtype: ClassDef or None """ - klass = node.frame() + klass = node.frame(future=True) while klass is not None and not isinstance(klass, ClassDef): if klass.parent is None: klass = None else: - klass = klass.parent.frame() + klass = klass.parent.frame(future=True) return klass @@ -2260,7 +2262,7 @@ class ClassDef(mixins.FilterStmtsMixin, LocalsDictNodeNG, node_classes.Statement parent=parent, ) if parent is not None: - parent.frame().set_local(name, self) + parent.frame(future=True).set_local(name, self) for local_name, node in self.implicit_locals(): self.add_local_node(node, local_name) @@ -2517,7 +2519,7 @@ class ClassDef(mixins.FilterStmtsMixin, LocalsDictNodeNG, node_classes.Statement # class A(name.Name): # def name(self): ... - frame = self.parent.frame() + frame = self.parent.frame(future=True) # line offset to avoid that class A(A) resolve the ancestor to # the defined class offset = -1 diff --git a/astroid/protocols.py b/astroid/protocols.py index 58b60834..ef7f5c96 100644 --- a/astroid/protocols.py +++ b/astroid/protocols.py @@ -411,7 +411,7 @@ def arguments_assigned_stmts( if ( context.callcontext and node - and getattr(callee, "name", None) == node.frame().name + and getattr(callee, "name", None) == node.frame(future=True).name ): # reset call context/name callcontext = context.callcontext -- cgit v1.2.1