Repository: python/mypy
Pull request: Ouput bad comment during syntax error
fixes #4103 

Problem statement:
When there's a syntax error, print the comment used
Given the Python code:

```python
def big_function(
        x1, # type: Optional[int]
        x2, # type: Optional[int]
        x3, # type: Optional[int]
        x4, # type: Optional[int]
        x5, # type: Optional[int]]
        x6, # type: Optional[int]
        x7, # type: Optional[int]
):
    # type: (...) -> int
    return 123
```

Sometimes it's difficult to spot which comment has a syntax error. It would be nicer if mypy could print the comment it's looking at, to make it more obvious.

```
$ mypy --py2 bar.py
bar.py:1: error: syntax error in type comment
```

Hints from issue discussion:
I agree, but I think this should be reported upstream to https://github.com/python/typed_ast.
It looks to me like the comment parsing happens in mypy: https://github.com/python/mypy/blob/master/mypy/fastparse2.py#L310-L312 . Presumably `n.type_comment` has the comment we're interested in?
Yes, I think it is possible for Python 2 to do this in mypy without changes to ``typed_ast``. On the contrary Python 3 type annotations are parsed by Python parser and should be syntactically correct.

Commit message: Fix and add tests

Patch to review:
diff --git a/mypy/fastparse.py b/mypy/fastparse.py
index 825dac4417a5..aaf7c85203f9 100644
--- a/mypy/fastparse.py
+++ b/mypy/fastparse.py
@@ -192,7 +192,9 @@ def parse_type_comment(type_comment: str,
         typ = ast3_parse(type_comment, '<type_comment>', 'eval')
     except SyntaxError as e:
         if errors is not None:
-            errors.report(line, e.offset, TYPE_COMMENT_SYNTAX_ERROR, blocker=True)
+            stripped_type = type_comment.split("#", 2)[0].strip()
+            err_msg = "{} '{}'".format(TYPE_COMMENT_SYNTAX_ERROR, stripped_type)
+            errors.report(line, e.offset, err_msg, blocker=True)
             return False, None
         else:
             raise
diff --git a/test-data/unit/check-fastparse.test b/test-data/unit/check-fastparse.test
index 6acdbdbf65e1..610c3d40c7ed 100644
--- a/test-data/unit/check-fastparse.test
+++ b/test-data/unit/check-fastparse.test
@@ -4,7 +4,7 @@
 
 [case testFastParseTypeCommentSyntaxError]
 
-x = None # type: a : b  # E: syntax error in type comment
+x = None # type: a : b  # E: syntax error in type comment 'a : b'
 
 [case testFastParseInvalidTypeComment]
 
diff --git a/test-data/unit/parse-errors.test b/test-data/unit/parse-errors.test
index 5a7b5ce4c08e..79d66aac6937 100644
--- a/test-data/unit/parse-errors.test
+++ b/test-data/unit/parse-errors.test
@@ -113,25 +113,43 @@ file:1: error: invalid syntax
 0
 x = 0 # type: A A
 [out]
-file:2: error: syntax error in type comment
+file:2: error: syntax error in type comment 'A A'
 
 [case testInvalidTypeComment2]
 0
 x = 0 # type: A[
 [out]
-file:2: error: syntax error in type comment
+file:2: error: syntax error in type comment 'A['
 
 [case testInvalidTypeComment3]
 0
 x = 0 # type:
 [out]
-file:2: error: syntax error in type comment
+file:2: error: syntax error in type comment ''
 
 [case testInvalidTypeComment4]
 0
 x = 0 # type: *
 [out]
-file:2: error: syntax error in type comment
+file:2: error: syntax error in type comment '*'
+
+[case testInvalidTypeComment5
+0
+x = 0 # type: * # some comment
+[out]
+file:2: error: syntax error in type comment '*'
+
+[case testInvalidTypeComment6
+0
+x = 0 # type: *# comment #6
+[out]
+file:2: error: syntax error in type comment '*'
+
+[case testInvalidTypeComment7
+0
+x = 0 # type: A B #comment #7
+[out]
+file:2: error: syntax error in type comment '*'
 
 [case testInvalidSignatureInComment1]
 def f(): # type: x


Code review comments:
diff_hunk: @@ -113,25 +113,43 @@ file:1: error: invalid syntax
 0
 x = 0 # type: A A
 [out]
-file:2: error: syntax error in type comment
+file:2: error: syntax error in type comment 'A A'
 
 [case testInvalidTypeComment2]
 0
 x = 0 # type: A[
 [out]
-file:2: error: syntax error in type comment
+file:2: error: syntax error in type comment 'A['
 
 [case testInvalidTypeComment3]
 0
 x = 0 # type:
 [out]
-file:2: error: syntax error in type comment
+file:2: error: syntax error in type comment ''
 
 [case testInvalidTypeComment4]
 0
 x = 0 # type: *
 [out]
-file:2: error: syntax error in type comment
+file:2: error: syntax error in type comment '*'
+
+[case testInvalidTypeComment5
path: test-data/unit/parse-errors.test
text: @user1:
You have missing closing `]` here in in couple tests below.