Skip to content

Commit 9cf5be0

Browse files
DanielNoordcdce8p
authored andcommitted
Add future argument to all NodeNG.statement() calls (pylint-dev#1235)
* Add ``future`` argument to all ``NodeNG.statement()`` calls * Add typing from ``statement()`` Co-authored-by: Marc Mueller <[email protected]>
1 parent a18758b commit 9cf5be0

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
@@ -401,8 +401,10 @@ def ilookup(self, name):
401401
context = InferenceContext()
402402
return _infer_stmts(stmts, context, frame)
403403

404-
def _get_filtered_node_statements(self, nodes):
405-
statements = [(node, node.statement()) for node in nodes]
404+
def _get_filtered_node_statements(
405+
self, nodes: typing.List[NodeNG]
406+
) -> typing.List[typing.Tuple[NodeNG, Statement]]:
407+
statements = [(node, node.statement(future=True)) for node in nodes]
406408
# Next we check if we have ExceptHandlers that are parent
407409
# of the underlying variable, in which case the last one survives
408410
if len(statements) > 1 and all(
@@ -452,15 +454,22 @@ def _filter_stmts(self, stmts, frame, offset):
452454
#
453455
# def test(b=1):
454456
# ...
455-
456-
if self.statement() is myframe and myframe.parent:
457+
if (
458+
self.parent
459+
and self.statement(future=True) is myframe
460+
and myframe.parent
461+
):
457462
myframe = myframe.parent.frame()
458-
mystmt = self.statement()
463+
464+
mystmt: Optional[Statement] = None
465+
if self.parent:
466+
mystmt = self.statement(future=True)
467+
459468
# line filtering if we are in the same frame
460469
#
461470
# take care node may be missing lineno information (this is the case for
462471
# nodes inserted for living objects)
463-
if myframe is frame and mystmt.fromlineno is not None:
472+
if myframe is frame and mystmt and mystmt.fromlineno is not None:
464473
assert mystmt.fromlineno is not None, mystmt
465474
mylineno = mystmt.fromlineno + offset
466475
else:
@@ -581,7 +590,7 @@ def _filter_stmts(self, stmts, frame, offset):
581590
_stmt_parents = []
582591
else:
583592
continue
584-
elif not optional_assign and stmt.parent is mystmt.parent:
593+
elif not optional_assign and mystmt and stmt.parent is mystmt.parent:
585594
_stmts = []
586595
_stmt_parents = []
587596
elif isinstance(node, DelName):
@@ -2023,13 +2032,15 @@ def assign_type(self):
20232032
"""
20242033
return self
20252034

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

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

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)