Skip to content
Draft
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
31 changes: 30 additions & 1 deletion mypy/fastparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -930,6 +930,9 @@ def do_func_def(
arg_types = [None] * len(args)
return_type = None
elif n.type_comment is not None:
self.fail(
message_registry.TYPE_COMMENT_DEPRECATED, lineno, n.col_offset, blocker=False
)
try:
func_type_ast = ast3_parse(n.type_comment, "<func_type>", "func_type")
assert isinstance(func_type_ast, FunctionType)
Expand Down Expand Up @@ -1141,7 +1144,13 @@ def make_argument(
arg_type = None
if annotation is not None:
arg_type = TypeConverter(self.errors, line=arg.lineno).visit(annotation)
else:
elif type_comment is not None:
self.fail(
message_registry.TYPE_COMMENT_DEPRECATED,
arg.lineno,
arg.col_offset,
blocker=False,
)
arg_type = self.translate_type_comment(arg, type_comment)
if argument_elide_name(arg.arg):
pos_only = True
Expand Down Expand Up @@ -1269,6 +1278,10 @@ def visit_Delete(self, n: ast3.Delete) -> DelStmt:
def visit_Assign(self, n: ast3.Assign) -> AssignmentStmt:
lvalues = self.translate_expr_list(n.targets)
rvalue = self.visit(n.value)
if n.type_comment is not None:
self.fail(
message_registry.TYPE_COMMENT_DEPRECATED, n.lineno, n.col_offset, blocker=False
)
typ = self.translate_type_comment(n, n.type_comment)
s = AssignmentStmt(lvalues, rvalue, type=typ, new_syntax=False)
return self.set_line(s, n)
Expand Down Expand Up @@ -1296,6 +1309,10 @@ def visit_AugAssign(self, n: ast3.AugAssign) -> OperatorAssignmentStmt:

# For(expr target, expr iter, stmt* body, stmt* orelse, string? type_comment)
def visit_For(self, n: ast3.For) -> ForStmt:
if n.type_comment is not None:
self.fail(
message_registry.TYPE_COMMENT_DEPRECATED, n.lineno, n.col_offset, blocker=False
)
target_type = self.translate_type_comment(n, n.type_comment)
node = ForStmt(
self.visit(n.target),
Expand All @@ -1308,6 +1325,10 @@ def visit_For(self, n: ast3.For) -> ForStmt:

# AsyncFor(expr target, expr iter, stmt* body, stmt* orelse, string? type_comment)
def visit_AsyncFor(self, n: ast3.AsyncFor) -> ForStmt:
if n.type_comment is not None:
self.fail(
message_registry.TYPE_COMMENT_DEPRECATED, n.lineno, n.col_offset, blocker=False
)
target_type = self.translate_type_comment(n, n.type_comment)
node = ForStmt(
self.visit(n.target),
Expand Down Expand Up @@ -1335,6 +1356,10 @@ def visit_If(self, n: ast3.If) -> IfStmt:

# With(withitem* items, stmt* body, string? type_comment)
def visit_With(self, n: ast3.With) -> WithStmt:
if n.type_comment is not None:
self.fail(
message_registry.TYPE_COMMENT_DEPRECATED, n.lineno, n.col_offset, blocker=False
)
target_type = self.translate_type_comment(n, n.type_comment)
node = WithStmt(
[self.visit(i.context_expr) for i in n.items],
Expand All @@ -1346,6 +1371,10 @@ def visit_With(self, n: ast3.With) -> WithStmt:

# AsyncWith(withitem* items, stmt* body, string? type_comment)
def visit_AsyncWith(self, n: ast3.AsyncWith) -> WithStmt:
if n.type_comment is not None:
self.fail(
message_registry.TYPE_COMMENT_DEPRECATED, n.lineno, n.col_offset, blocker=False
)
target_type = self.translate_type_comment(n, n.type_comment)
s = WithStmt(
[self.visit(i.context_expr) for i in n.items],
Expand Down
3 changes: 3 additions & 0 deletions mypy/message_registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,9 @@ def with_additional_msg(self, info: str) -> ErrorMessage:
TYPE_COMMENT_SYNTAX_ERROR_VALUE: Final = ErrorMessage(
'Syntax error in type comment "{}"', codes.SYNTAX
)
TYPE_COMMENT_DEPRECATED: Final = ErrorMessage(
"Using type comments with mypy is deprecated, use inline annotations instead", codes.MISC
)
ELLIPSIS_WITH_OTHER_TYPEARGS: Final = ErrorMessage(
"Ellipses cannot accompany other argument types in function type signature", codes.SYNTAX
)
Expand Down
Loading