From 35838494750492a692d30b0af3cebab8bd438f38 Mon Sep 17 00:00:00 2001 From: krassowski <5832902+krassowski@users.noreply.github.com> Date: Tue, 27 Sep 2022 15:51:30 +0100 Subject: [PATCH] Add end coordinates for column and line number --- pyflakes/checker.py | 10 +++++++--- pyflakes/messages.py | 2 ++ pyflakes/test/test_doctests.py | 5 +++++ 3 files changed, 14 insertions(+), 3 deletions(-) diff --git a/pyflakes/checker.py b/pyflakes/checker.py index 29d15d34..f4b39d7a 100644 --- a/pyflakes/checker.py +++ b/pyflakes/checker.py @@ -1270,9 +1270,13 @@ def getDocstring(self, node): def handleNode(self, node, parent): if node is None: return - if self.offset and getattr(node, 'lineno', None) is not None: - node.lineno += self.offset[0] - node.col_offset += self.offset[1] + if self.offset: + if getattr(node, 'lineno', None) is not None: + node.lineno += self.offset[0] + node.col_offset += self.offset[1] + if getattr(node, 'end_lineno', None) is not None: + node.end_lineno += self.offset[0] + node.end_col_offset += self.offset[1] if self.futuresAllowed and not (isinstance(node, ast.ImportFrom) or self.isDocstring(node)): self.futuresAllowed = False diff --git a/pyflakes/messages.py b/pyflakes/messages.py index f45fd467..1e400feb 100644 --- a/pyflakes/messages.py +++ b/pyflakes/messages.py @@ -11,6 +11,8 @@ def __init__(self, filename, loc): self.filename = filename self.lineno = loc.lineno self.col = loc.col_offset + self.end_col = getattr(loc, 'end_col_offset', None) + self.end_lineno = getattr(loc, 'end_lineno', None) def __str__(self): return '{}:{}:{}: {}'.format(self.filename, self.lineno, self.col+1, diff --git a/pyflakes/test/test_doctests.py b/pyflakes/test/test_doctests.py index 6c8e69ef..ef6a44ba 100644 --- a/pyflakes/test/test_doctests.py +++ b/pyflakes/test/test_doctests.py @@ -293,6 +293,11 @@ def doctest_stuff(): ''', m.UndefinedName).messages[0] self.assertEqual(exc.lineno, 5) self.assertEqual(exc.col, 20) + # end_col_offset and end_lineno are new in Python 3.8 + if sys.version_info >= (3, 8): + # check that offsets are also added to end_col and end_lineno + self.assertEqual(exc.end_lineno, 5) + self.assertEqual(exc.end_col, 21) def test_offsetAfterDoctests(self): exc = self.flakes('''