summaryrefslogtreecommitdiff
path: root/astroid
diff options
context:
space:
mode:
authorDaniƫl van Noord <13665637+DanielNoord@users.noreply.github.com>2021-11-24 13:50:11 +0100
committerGitHub <noreply@github.com>2021-11-24 13:50:11 +0100
commit444315963dcfec56d2539f72895ff98c07f2d31d (patch)
tree3d0ade2e5941cc4e7ede8082eb23473bcd76c561 /astroid
parent7ed1ee9df2a8f384fe2587dff44aba85ef289cf7 (diff)
downloadastroid-git-444315963dcfec56d2539f72895ff98c07f2d31d.tar.gz
Add ``future`` argument to all ``NodeNG.statement()`` calls (#1235)
* Add ``future`` argument to all ``NodeNG.statement()`` calls * Add typing from ``statement()`` Co-authored-by: Marc Mueller <30130371+cdce8p@users.noreply.github.com>
Diffstat (limited to 'astroid')
-rw-r--r--astroid/brain/brain_dataclasses.py2
-rw-r--r--astroid/brain/brain_namedtuple_enum.py2
-rw-r--r--astroid/mixins.py14
-rw-r--r--astroid/nodes/node_classes.py29
-rw-r--r--astroid/nodes/scoped_nodes.py2
-rw-r--r--astroid/protocols.py2
6 files changed, 34 insertions, 17 deletions
diff --git a/astroid/brain/brain_dataclasses.py b/astroid/brain/brain_dataclasses.py
index 7bb2a60f..6ea14511 100644
--- a/astroid/brain/brain_dataclasses.py
+++ b/astroid/brain/brain_dataclasses.py
@@ -304,7 +304,7 @@ def _looks_like_dataclass_field_call(node: Call, check_scope: bool = True) -> bo
If check_scope is False, skips checking the statement and body.
"""
if check_scope:
- stmt = node.statement()
+ stmt = node.statement(future=True)
scope = stmt.scope()
if not (
isinstance(stmt, AnnAssign)
diff --git a/astroid/brain/brain_namedtuple_enum.py b/astroid/brain/brain_namedtuple_enum.py
index 2ce69a90..1ca661fa 100644
--- a/astroid/brain/brain_namedtuple_enum.py
+++ b/astroid/brain/brain_namedtuple_enum.py
@@ -365,7 +365,7 @@ def infer_enum_class(node):
if any(not isinstance(value, nodes.AssignName) for value in values):
continue
- stmt = values[0].statement()
+ stmt = values[0].statement(future=True)
if isinstance(stmt, nodes.Assign):
if isinstance(stmt.targets[0], nodes.Tuple):
targets = stmt.targets[0].itered()
diff --git a/astroid/mixins.py b/astroid/mixins.py
index d7c027a1..3241ecea 100644
--- a/astroid/mixins.py
+++ b/astroid/mixins.py
@@ -16,10 +16,14 @@
"""This module contains some mixins for the different nodes.
"""
import itertools
+from typing import TYPE_CHECKING, Optional
from astroid import decorators
from astroid.exceptions import AttributeInferenceError
+if TYPE_CHECKING:
+ from astroid import nodes
+
class BlockRangeMixIn:
"""override block range"""
@@ -44,9 +48,9 @@ class BlockRangeMixIn:
class FilterStmtsMixin:
"""Mixin for statement filtering and assignment type"""
- def _get_filtered_stmts(self, _, node, _stmts, mystmt):
+ def _get_filtered_stmts(self, _, node, _stmts, mystmt: Optional["nodes.Statement"]):
"""method used in _filter_stmts to get statements and trigger break"""
- if self.statement() is mystmt:
+ if self.statement(future=True) is mystmt:
# original node's statement is the assignment, only keep
# current node (gen exp, list comp)
return [node], True
@@ -60,11 +64,13 @@ class AssignTypeMixin:
def assign_type(self):
return self
- def _get_filtered_stmts(self, lookup_node, node, _stmts, mystmt):
+ def _get_filtered_stmts(
+ self, lookup_node, node, _stmts, mystmt: Optional["nodes.Statement"]
+ ):
"""method used in filter_stmts"""
if self is mystmt:
return _stmts, True
- if self.statement() is mystmt:
+ if self.statement(future=True) is mystmt:
# original node's statement is the assignment, only keep
# current node (gen exp, list comp)
return [node], True
diff --git a/astroid/nodes/node_classes.py b/astroid/nodes/node_classes.py
index 91ee197b..e67e9641 100644
--- a/astroid/nodes/node_classes.py
+++ b/astroid/nodes/node_classes.py
@@ -400,8 +400,10 @@ class LookupMixIn:
context = InferenceContext()
return _infer_stmts(stmts, context, frame)
- def _get_filtered_node_statements(self, nodes):
- statements = [(node, node.statement()) for node in nodes]
+ def _get_filtered_node_statements(
+ self, nodes: typing.List[NodeNG]
+ ) -> typing.List[typing.Tuple[NodeNG, Statement]]:
+ statements = [(node, node.statement(future=True)) for node in nodes]
# Next we check if we have ExceptHandlers that are parent
# of the underlying variable, in which case the last one survives
if len(statements) > 1 and all(
@@ -451,15 +453,22 @@ class LookupMixIn:
#
# def test(b=1):
# ...
-
- if self.statement() is myframe and myframe.parent:
+ if (
+ self.parent
+ and self.statement(future=True) is myframe
+ and myframe.parent
+ ):
myframe = myframe.parent.frame()
- mystmt = self.statement()
+
+ mystmt: Optional[Statement] = None
+ if self.parent:
+ mystmt = self.statement(future=True)
+
# line filtering if we are in the same frame
#
# take care node may be missing lineno information (this is the case for
# nodes inserted for living objects)
- if myframe is frame and mystmt.fromlineno is not None:
+ if myframe is frame and mystmt and mystmt.fromlineno is not None:
assert mystmt.fromlineno is not None, mystmt
mylineno = mystmt.fromlineno + offset
else:
@@ -580,7 +589,7 @@ class LookupMixIn:
_stmt_parents = []
else:
continue
- elif not optional_assign and stmt.parent is mystmt.parent:
+ elif not optional_assign and mystmt and stmt.parent is mystmt.parent:
_stmts = []
_stmt_parents = []
elif isinstance(node, DelName):
@@ -2022,13 +2031,15 @@ class Comprehension(NodeNG):
"""
return self
- def _get_filtered_stmts(self, lookup_node, node, stmts, mystmt):
+ def _get_filtered_stmts(
+ self, lookup_node, node, stmts, mystmt: Optional[Statement]
+ ):
"""method used in filter_stmts"""
if self is mystmt:
if isinstance(lookup_node, (Const, Name)):
return [lookup_node], True
- elif self.statement() is mystmt:
+ elif self.statement(future=True) is mystmt:
# original node's statement is the assignment, only keeps
# current node (gen exp, list comp)
diff --git a/astroid/nodes/scoped_nodes.py b/astroid/nodes/scoped_nodes.py
index 56de158a..46aca3d3 100644
--- a/astroid/nodes/scoped_nodes.py
+++ b/astroid/nodes/scoped_nodes.py
@@ -2739,7 +2739,7 @@ class ClassDef(mixins.FilterStmtsMixin, LocalsDictNodeNG, node_classes.Statement
# Look for AnnAssigns, which are not attributes in the purest sense.
for value in values:
if isinstance(value, node_classes.AssignName):
- stmt = value.statement()
+ stmt = value.statement(future=True)
if isinstance(stmt, node_classes.AnnAssign) and stmt.value is None:
raise AttributeInferenceError(
target=self, attribute=name, context=context
diff --git a/astroid/protocols.py b/astroid/protocols.py
index 4ec92a2a..674766e1 100644
--- a/astroid/protocols.py
+++ b/astroid/protocols.py
@@ -637,7 +637,7 @@ def starred_assigned_stmts(self, node=None, context=None, assign_path=None):
lookups.append((index, len(element.itered())))
_determine_starred_iteration_lookups(starred, element, lookups)
- stmt = self.statement()
+ stmt = self.statement(future=True)
if not isinstance(stmt, (nodes.Assign, nodes.For)):
raise InferenceError(
"Statement {stmt!r} enclosing {node!r} " "must be an Assign or For node.",