Skip to content

Commit 4443159

Browse files
DanielNoordcdce8p
andauthored
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>
1 parent 7ed1ee9 commit 4443159

File tree

7 files changed

+35
-19
lines changed

7 files changed

+35
-19
lines changed

astroid/brain/brain_dataclasses.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ def _looks_like_dataclass_field_call(node: Call, check_scope: bool = True) -> bo
304304
If check_scope is False, skips checking the statement and body.
305305
"""
306306
if check_scope:
307-
stmt = node.statement()
307+
stmt = node.statement(future=True)
308308
scope = stmt.scope()
309309
if not (
310310
isinstance(stmt, AnnAssign)

astroid/brain/brain_namedtuple_enum.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,7 @@ def infer_enum_class(node):
365365
if any(not isinstance(value, nodes.AssignName) for value in values):
366366
continue
367367

368-
stmt = values[0].statement()
368+
stmt = values[0].statement(future=True)
369369
if isinstance(stmt, nodes.Assign):
370370
if isinstance(stmt.targets[0], nodes.Tuple):
371371
targets = stmt.targets[0].itered()

astroid/mixins.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,14 @@
1616
"""This module contains some mixins for the different nodes.
1717
"""
1818
import itertools
19+
from typing import TYPE_CHECKING, Optional
1920

2021
from astroid import decorators
2122
from astroid.exceptions import AttributeInferenceError
2223

24+
if TYPE_CHECKING:
25+
from astroid import nodes
26+
2327

2428
class BlockRangeMixIn:
2529
"""override block range"""
@@ -44,9 +48,9 @@ def _elsed_block_range(self, lineno, orelse, last=None):
4448
class FilterStmtsMixin:
4549
"""Mixin for statement filtering and assignment type"""
4650

47-
def _get_filtered_stmts(self, _, node, _stmts, mystmt):
51+
def _get_filtered_stmts(self, _, node, _stmts, mystmt: Optional["nodes.Statement"]):
4852
"""method used in _filter_stmts to get statements and trigger break"""
49-
if self.statement() is mystmt:
53+
if self.statement(future=True) is mystmt:
5054
# original node's statement is the assignment, only keep
5155
# current node (gen exp, list comp)
5256
return [node], True
@@ -60,11 +64,13 @@ class AssignTypeMixin:
6064
def assign_type(self):
6165
return self
6266

63-
def _get_filtered_stmts(self, lookup_node, node, _stmts, mystmt):
67+
def _get_filtered_stmts(
68+
self, lookup_node, node, _stmts, mystmt: Optional["nodes.Statement"]
69+
):
6470
"""method used in filter_stmts"""
6571
if self is mystmt:
6672
return _stmts, True
67-
if self.statement() is mystmt:
73+
if self.statement(future=True) is mystmt:
6874
# original node's statement is the assignment, only keep
6975
# current node (gen exp, list comp)
7076
return [node], True

astroid/nodes/node_classes.py

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -400,8 +400,10 @@ def ilookup(self, name):
400400
context = InferenceContext()
401401
return _infer_stmts(stmts, context, frame)
402402

403-
def _get_filtered_node_statements(self, nodes):
404-
statements = [(node, node.statement()) for node in nodes]
403+
def _get_filtered_node_statements(
404+
self, nodes: typing.List[NodeNG]
405+
) -> typing.List[typing.Tuple[NodeNG, Statement]]:
406+
statements = [(node, node.statement(future=True)) for node in nodes]
405407
# Next we check if we have ExceptHandlers that are parent
406408
# of the underlying variable, in which case the last one survives
407409
if len(statements) > 1 and all(
@@ -451,15 +453,22 @@ def _filter_stmts(self, stmts, frame, offset):
451453
#
452454
# def test(b=1):
453455
# ...
454-
455-
if self.statement() is myframe and myframe.parent:
456+
if (
457+
self.parent
458+
and self.statement(future=True) is myframe
459+
and myframe.parent
460+
):
456461
myframe = myframe.parent.frame()
457-
mystmt = self.statement()
462+
463+
mystmt: Optional[Statement] = None
464+
if self.parent:
465+
mystmt = self.statement(future=True)
466+
458467
# line filtering if we are in the same frame
459468
#
460469
# take care node may be missing lineno information (this is the case for
461470
# nodes inserted for living objects)
462-
if myframe is frame and mystmt.fromlineno is not None:
471+
if myframe is frame and mystmt and mystmt.fromlineno is not None:
463472
assert mystmt.fromlineno is not None, mystmt
464473
mylineno = mystmt.fromlineno + offset
465474
else:
@@ -580,7 +589,7 @@ def _filter_stmts(self, stmts, frame, offset):
580589
_stmt_parents = []
581590
else:
582591
continue
583-
elif not optional_assign and stmt.parent is mystmt.parent:
592+
elif not optional_assign and mystmt and stmt.parent is mystmt.parent:
584593
_stmts = []
585594
_stmt_parents = []
586595
elif isinstance(node, DelName):
@@ -2022,13 +2031,15 @@ def assign_type(self):
20222031
"""
20232032
return self
20242033

2025-
def _get_filtered_stmts(self, lookup_node, node, stmts, mystmt):
2034+
def _get_filtered_stmts(
2035+
self, lookup_node, node, stmts, mystmt: Optional[Statement]
2036+
):
20262037
"""method used in filter_stmts"""
20272038
if self is mystmt:
20282039
if isinstance(lookup_node, (Const, Name)):
20292040
return [lookup_node], True
20302041

2031-
elif self.statement() is mystmt:
2042+
elif self.statement(future=True) is mystmt:
20322043
# original node's statement is the assignment, only keeps
20332044
# current node (gen exp, list comp)
20342045

astroid/nodes/scoped_nodes.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2739,7 +2739,7 @@ def getattr(self, name, context=None, class_context=True):
27392739
# Look for AnnAssigns, which are not attributes in the purest sense.
27402740
for value in values:
27412741
if isinstance(value, node_classes.AssignName):
2742-
stmt = value.statement()
2742+
stmt = value.statement(future=True)
27432743
if isinstance(stmt, node_classes.AnnAssign) and stmt.value is None:
27442744
raise AttributeInferenceError(
27452745
target=self, attribute=name, context=context

astroid/protocols.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -637,7 +637,7 @@ def _determine_starred_iteration_lookups(starred, target, lookups):
637637
lookups.append((index, len(element.itered())))
638638
_determine_starred_iteration_lookups(starred, element, lookups)
639639

640-
stmt = self.statement()
640+
stmt = self.statement(future=True)
641641
if not isinstance(stmt, (nodes.Assign, nodes.For)):
642642
raise InferenceError(
643643
"Statement {stmt!r} enclosing {node!r} " "must be an Assign or For node.",

tests/unittest_builder.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -614,9 +614,8 @@ def test_module_base_props(self) -> None:
614614
self.assertEqual(module.pure_python, 1)
615615
self.assertEqual(module.package, 0)
616616
self.assertFalse(module.is_statement)
617-
self.assertEqual(module.statement(), module)
618617
with pytest.warns(DeprecationWarning) as records:
619-
module.statement()
618+
self.assertEqual(module.statement(), module)
620619
assert len(records) == 1
621620
with self.assertRaises(StatementMissing):
622621
module.statement(future=True)

0 commit comments

Comments
 (0)