Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions mypy/fastparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -470,8 +472,10 @@ def do_func_def(self, n: Union[ast3.FunctionDef, ast3.AsyncFunctionDef],
if self.in_method_scope() and len(arg_types) < len(args):
arg_types.insert(0, AnyType(TypeOfAny.special_form))
except SyntaxError:
self.fail(TYPE_COMMENT_SYNTAX_ERROR, lineno, n.col_offset)
if n.type_comment and n.type_comment[0] != "(":
stripped_type = n.type_comment.split("#", 2)[0].strip()
err_msg = "{} '{}'".format(TYPE_COMMENT_SYNTAX_ERROR, stripped_type)
self.fail(err_msg, lineno, n.col_offset)
if n.type_comment and n.type_comment[0] not in ["(", "#"]:
self.note('Suggestion: wrap argument types in parentheses',
lineno, n.col_offset)
arg_types = [AnyType(TypeOfAny.from_error)] * len(args)
Expand Down
4 changes: 3 additions & 1 deletion mypy/fastparse2.py
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,9 @@ def visit_FunctionDef(self, n: ast27.FunctionDef) -> Statement:
if self.in_method_scope() and len(arg_types) < len(args):
arg_types.insert(0, AnyType(TypeOfAny.special_form))
except SyntaxError:
self.fail(TYPE_COMMENT_SYNTAX_ERROR, lineno, n.col_offset)
stripped_type = type_comment.split("#", 2)[0].strip()
err_msg = "{} '{}'".format(TYPE_COMMENT_SYNTAX_ERROR, stripped_type)
self.fail(err_msg, lineno, n.col_offset)
arg_types = [AnyType(TypeOfAny.from_error)] * len(args)
return_type = AnyType(TypeOfAny.from_error)
else:
Expand Down
6 changes: 3 additions & 3 deletions test-data/unit/check-fastparse.test
Original file line number Diff line number Diff line change
Expand Up @@ -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]

Expand All @@ -14,13 +14,13 @@ x = None # type: a + b # E: Invalid type comment or annotation
-- This happens in both parsers.
[case testFastParseFunctionAnnotationSyntaxError]

def f(): # E: syntax error in type comment # N: Suggestion: wrap argument types in parentheses
def f(): # E: syntax error in type comment 'None -> None' # N: Suggestion: wrap argument types in parentheses
# type: None -> None
pass

[case testFastParseFunctionAnnotationSyntaxErrorSpaces]

def f(): # E: syntax error in type comment # N: Suggestion: wrap argument types in parentheses
def f(): # E: syntax error in type comment 'None -> None' # N: Suggestion: wrap argument types in parentheses
# type: None -> None
pass

Expand Down
2 changes: 1 addition & 1 deletion test-data/unit/check-literal.test
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ reveal_type(g2) # E: Revealed type is 'def (x: Literal['A B'])'

[case testLiteralInvalidTypeComment]
from typing_extensions import Literal
def f(x): # E: syntax error in type comment
def f(x): # E: syntax error in type comment '(A[) -> None'
# type: (A[) -> None
pass

Expand Down
59 changes: 45 additions & 14 deletions test-data/unit/parse-errors.test
Original file line number Diff line number Diff line change
Expand Up @@ -113,86 +113,117 @@ 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 'A B'

[case testInvalidSignatureInComment1]
def f(): # type: x
pass
[out]
file:1: error: syntax error in type comment
file:1: error: syntax error in type comment 'x'
file:1: note: Suggestion: wrap argument types in parentheses

[case testInvalidSignatureInComment2]
def f(): # type:
pass
[out]
file:1: error: syntax error in type comment
file:1: error: syntax error in type comment ''

[case testInvalidSignatureInComment3]
def f(): # type: (
pass
[out]
file:1: error: syntax error in type comment
file:1: error: syntax error in type comment '('

[case testInvalidSignatureInComment4]
def f(): # type: (.
pass
[out]
file:1: error: syntax error in type comment
file:1: error: syntax error in type comment '(.'

[case testInvalidSignatureInComment5]
def f(): # type: (x
pass
[out]
file:1: error: syntax error in type comment
file:1: error: syntax error in type comment '(x'

[case testInvalidSignatureInComment6]
def f(): # type: (x)
pass
[out]
file:1: error: syntax error in type comment
file:1: error: syntax error in type comment '(x)'

[case testInvalidSignatureInComment7]
def f(): # type: (x) -
pass
[out]
file:1: error: syntax error in type comment
file:1: error: syntax error in type comment '(x) -'

[case testInvalidSignatureInComment8]
def f(): # type: (x) ->
pass
[out]
file:1: error: syntax error in type comment
file:1: error: syntax error in type comment '(x) ->'

[case testInvalidSignatureInComment9]
def f(): # type: (x) -> .
pass
[out]
file:1: error: syntax error in type comment
file:1: error: syntax error in type comment '(x) -> .'

[case testInvalidSignatureInComment10]
def f(): # type: (x) -> x x
pass
[out]
file:1: error: syntax error in type comment
file:1: error: syntax error in type comment '(x) -> x x'

[case testInvalidSignatureInComment11]
def f(): # type: # abc comment
pass
[out]
file:1: error: syntax error in type comment ''

[case testInvalidSignatureInComment12]
def f(): # type: (x) -> x x # comment #2
pass
[out]
file:1: error: syntax error in type comment '(x) -> x x'


[case testDuplicateSignatures1]
def f() -> None: # type: () -> None
Expand Down