summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAnthony Sottile <asottile@umich.edu>2019-02-01 08:41:28 -0800
committerIan Stapleton Cordasco <graffatcolmingov@gmail.com>2019-02-01 10:41:28 -0600
commitba64624f38a55f162b90120b4c0ba62018c6fd08 (patch)
treefb2cd1126c7cba896c9ccb20d2ea2ec4dfbe3cae
parent2c29431eaeb9c5b681f1615bf568fc68c6469486 (diff)
downloadpyflakes-ba64624f38a55f162b90120b4c0ba62018c6fd08.tar.gz
Fix line number of comment syntax error message (#420)
-rw-r--r--pyflakes/checker.py9
-rw-r--r--pyflakes/test/test_type_annotations.py7
2 files changed, 15 insertions, 1 deletions
diff --git a/pyflakes/checker.py b/pyflakes/checker.py
index 70aaff2..4c88af2 100644
--- a/pyflakes/checker.py
+++ b/pyflakes/checker.py
@@ -507,6 +507,13 @@ class DoctestScope(ModuleScope):
"""Scope for a doctest."""
+class DummyNode(object):
+ """Used in place of an `ast.AST` to set error message positions"""
+ def __init__(self, lineno, col_offset):
+ self.lineno = lineno
+ self.col_offset = col_offset
+
+
# Globally defined names which are not attributes of the builtins module, or
# are only present on some platforms.
_MAGIC_GLOBALS = ['__file__', '__builtins__', 'WindowsError']
@@ -1056,7 +1063,7 @@ class Checker(object):
part = part.replace('...', 'Ellipsis')
self.deferFunction(functools.partial(
self.handleStringAnnotation,
- part, node, lineno, col_offset,
+ part, DummyNode(lineno, col_offset), lineno, col_offset,
messages.CommentAnnotationSyntaxError,
))
diff --git a/pyflakes/test/test_type_annotations.py b/pyflakes/test/test_type_annotations.py
index 5af4441..48635bb 100644
--- a/pyflakes/test/test_type_annotations.py
+++ b/pyflakes/test/test_type_annotations.py
@@ -297,6 +297,13 @@ class TestTypeAnnotations(TestCase):
pass
""", m.CommentAnnotationSyntaxError)
+ def test_typeCommentsSyntaxErrorCorrectLine(self):
+ checker = self.flakes("""\
+ x = 1
+ # type: definitely not a PEP 484 comment
+ """, m.CommentAnnotationSyntaxError)
+ self.assertEqual(checker.messages[0].lineno, 2)
+
def test_typeCommentsAssignedToPreviousNode(self):
# This test demonstrates an issue in the implementation which
# associates the type comment with a node above it, however the type