diff options
| author | Sergei Lebedev <185856+superbobry@users.noreply.github.com> | 2022-02-27 20:12:36 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-02-27 21:12:36 +0100 |
| commit | b6d17107f2e02df4ce5080536bb783a25273b33f (patch) | |
| tree | 73bd302ce1e215d9f29ad72681a4f934a95a0c9b | |
| parent | 0acb961d7375131c3d1e7a3580f974b6e8c5ef94 (diff) | |
| download | astroid-git-b6d17107f2e02df4ce5080536bb783a25273b33f.tar.gz | |
Changed NodeNG.tolineno to use end_lineno when it is available (#1351)
Co-authored-by: Daniƫl van Noord <13665637+DanielNoord@users.noreply.github.com>
| -rw-r--r-- | ChangeLog | 5 | ||||
| -rw-r--r-- | astroid/nodes/node_ng.py | 2 | ||||
| -rw-r--r-- | astroid/rebuilder.py | 14 | ||||
| -rw-r--r-- | tests/unittest_builder.py | 11 | ||||
| -rw-r--r-- | tests/unittest_nodes_lineno.py | 2 | ||||
| -rw-r--r-- | tests/unittest_scoped_nodes.py | 10 |
6 files changed, 34 insertions, 10 deletions
@@ -34,6 +34,11 @@ Release date: TBA Closes #1330 +* Use the ``end_lineno`` attribute for the ``NodeNG.tolineno`` property + when it is available. + + Closes #1350 + * Add ``is_dataclass`` attribute to ``ClassDef`` nodes. * Use ``sysconfig`` instead of ``distutils`` to determine the location of diff --git a/astroid/nodes/node_ng.py b/astroid/nodes/node_ng.py index 1819f5d8..73d12a37 100644 --- a/astroid/nodes/node_ng.py +++ b/astroid/nodes/node_ng.py @@ -445,6 +445,8 @@ class NodeNG: @decorators.cachedproperty def tolineno(self) -> Optional[int]: """The last line that this node appears on in the source code.""" + if self.end_lineno is not None: + return self.end_lineno if not self._astroid_fields: # can't have children last_child = None diff --git a/astroid/rebuilder.py b/astroid/rebuilder.py index 2c6fdb29..0796b1aa 100644 --- a/astroid/rebuilder.py +++ b/astroid/rebuilder.py @@ -2197,11 +2197,21 @@ class TreeRebuilder: def visit_tryexcept(self, node: "ast.Try", parent: NodeNG) -> nodes.TryExcept: """visit a TryExcept node by returning a fresh instance of it""" if sys.version_info >= (3, 8): + # TryExcept excludes the 'finally' but that will be included in the + # end_lineno from 'node'. Therefore, we check all non 'finally' + # children to find the correct end_lineno and column. + end_lineno = node.end_lineno + end_col_offset = node.end_col_offset + all_children: List["ast.AST"] = [*node.body, *node.handlers, *node.orelse] + for child in reversed(all_children): + end_lineno = child.end_lineno + end_col_offset = child.end_col_offset + break newnode = nodes.TryExcept( lineno=node.lineno, col_offset=node.col_offset, - end_lineno=node.end_lineno, - end_col_offset=node.end_col_offset, + end_lineno=end_lineno, + end_col_offset=end_col_offset, parent=parent, ) else: diff --git a/tests/unittest_builder.py b/tests/unittest_builder.py index 151abcbe..df6d50b4 100644 --- a/tests/unittest_builder.py +++ b/tests/unittest_builder.py @@ -76,11 +76,14 @@ class FromToLineNoTest(unittest.TestCase): strarg = callfunc.args[0] self.assertIsInstance(strarg, nodes.Const) if hasattr(sys, "pypy_version_info"): - lineno = 4 + self.assertEqual(strarg.fromlineno, 4) + self.assertEqual(strarg.tolineno, 4) else: - lineno = 5 if not PY38_PLUS else 4 - self.assertEqual(strarg.fromlineno, lineno) - self.assertEqual(strarg.tolineno, lineno) + if not PY38_PLUS: + self.assertEqual(strarg.fromlineno, 5) + else: + self.assertEqual(strarg.fromlineno, 4) + self.assertEqual(strarg.tolineno, 5) namearg = callfunc.args[1] self.assertIsInstance(namearg, nodes.Name) self.assertEqual(namearg.fromlineno, 5) diff --git a/tests/unittest_nodes_lineno.py b/tests/unittest_nodes_lineno.py index 73cf0207..0f1b7e4a 100644 --- a/tests/unittest_nodes_lineno.py +++ b/tests/unittest_nodes_lineno.py @@ -784,7 +784,7 @@ class TestLinenoColOffset: assert (t3.lineno, t3.col_offset) == (10, 0) assert (t3.end_lineno, t3.end_col_offset) == (17, 8) assert (t3.body[0].lineno, t3.body[0].col_offset) == (10, 0) - assert (t3.body[0].end_lineno, t3.body[0].end_col_offset) == (17, 8) + assert (t3.body[0].end_lineno, t3.body[0].end_col_offset) == (15, 8) assert (t3.finalbody[0].lineno, t3.finalbody[0].col_offset) == (17, 4) assert (t3.finalbody[0].end_lineno, t3.finalbody[0].end_col_offset) == (17, 8) diff --git a/tests/unittest_scoped_nodes.py b/tests/unittest_scoped_nodes.py index 9b2bc291..0009278d 100644 --- a/tests/unittest_scoped_nodes.py +++ b/tests/unittest_scoped_nodes.py @@ -1092,15 +1092,19 @@ class ClassNodeTest(ModuleLoader, unittest.TestCase): print(x) @f(a=2, - b=3) + b=3, + ) def g2(): pass """ astroid = builder.parse(data) self.assertEqual(astroid["g1"].fromlineno, 4) self.assertEqual(astroid["g1"].tolineno, 5) - self.assertEqual(astroid["g2"].fromlineno, 9) - self.assertEqual(astroid["g2"].tolineno, 10) + if not PY38_PLUS: + self.assertEqual(astroid["g2"].fromlineno, 9) + else: + self.assertEqual(astroid["g2"].fromlineno, 10) + self.assertEqual(astroid["g2"].tolineno, 11) def test_metaclass_error(self) -> None: astroid = builder.parse( |
