Skip to content

Commit 88dd26a

Browse files
committed
Deprecate type comments
1 parent 5186052 commit 88dd26a

2 files changed

Lines changed: 33 additions & 1 deletion

File tree

mypy/fastparse.py

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -927,6 +927,9 @@ def do_func_def(
927927
arg_types = [None] * len(args)
928928
return_type = None
929929
elif n.type_comment is not None:
930+
self.fail(
931+
message_registry.TYPE_COMMENT_DEPRECATED, lineno, n.col_offset, blocker=False
932+
)
930933
try:
931934
func_type_ast = ast3_parse(n.type_comment, "<func_type>", "func_type")
932935
assert isinstance(func_type_ast, FunctionType)
@@ -1138,7 +1141,13 @@ def make_argument(
11381141
arg_type = None
11391142
if annotation is not None:
11401143
arg_type = TypeConverter(self.errors, line=arg.lineno).visit(annotation)
1141-
else:
1144+
elif type_comment is not None:
1145+
self.fail(
1146+
message_registry.TYPE_COMMENT_DEPRECATED,
1147+
arg.lineno,
1148+
arg.col_offset,
1149+
blocker=False,
1150+
)
11421151
arg_type = self.translate_type_comment(arg, type_comment)
11431152
if argument_elide_name(arg.arg):
11441153
pos_only = True
@@ -1266,6 +1275,10 @@ def visit_Delete(self, n: ast3.Delete) -> DelStmt:
12661275
def visit_Assign(self, n: ast3.Assign) -> AssignmentStmt:
12671276
lvalues = self.translate_expr_list(n.targets)
12681277
rvalue = self.visit(n.value)
1278+
if n.type_comment is not None:
1279+
self.fail(
1280+
message_registry.TYPE_COMMENT_DEPRECATED, n.lineno, n.col_offset, blocker=False
1281+
)
12691282
typ = self.translate_type_comment(n, n.type_comment)
12701283
s = AssignmentStmt(lvalues, rvalue, type=typ, new_syntax=False)
12711284
return self.set_line(s, n)
@@ -1293,6 +1306,10 @@ def visit_AugAssign(self, n: ast3.AugAssign) -> OperatorAssignmentStmt:
12931306

12941307
# For(expr target, expr iter, stmt* body, stmt* orelse, string? type_comment)
12951308
def visit_For(self, n: ast3.For) -> ForStmt:
1309+
if n.type_comment is not None:
1310+
self.fail(
1311+
message_registry.TYPE_COMMENT_DEPRECATED, n.lineno, n.col_offset, blocker=False
1312+
)
12961313
target_type = self.translate_type_comment(n, n.type_comment)
12971314
node = ForStmt(
12981315
self.visit(n.target),
@@ -1305,6 +1322,10 @@ def visit_For(self, n: ast3.For) -> ForStmt:
13051322

13061323
# AsyncFor(expr target, expr iter, stmt* body, stmt* orelse, string? type_comment)
13071324
def visit_AsyncFor(self, n: ast3.AsyncFor) -> ForStmt:
1325+
if n.type_comment is not None:
1326+
self.fail(
1327+
message_registry.TYPE_COMMENT_DEPRECATED, n.lineno, n.col_offset, blocker=False
1328+
)
13081329
target_type = self.translate_type_comment(n, n.type_comment)
13091330
node = ForStmt(
13101331
self.visit(n.target),
@@ -1332,6 +1353,10 @@ def visit_If(self, n: ast3.If) -> IfStmt:
13321353

13331354
# With(withitem* items, stmt* body, string? type_comment)
13341355
def visit_With(self, n: ast3.With) -> WithStmt:
1356+
if n.type_comment is not None:
1357+
self.fail(
1358+
message_registry.TYPE_COMMENT_DEPRECATED, n.lineno, n.col_offset, blocker=False
1359+
)
13351360
target_type = self.translate_type_comment(n, n.type_comment)
13361361
node = WithStmt(
13371362
[self.visit(i.context_expr) for i in n.items],
@@ -1343,6 +1368,10 @@ def visit_With(self, n: ast3.With) -> WithStmt:
13431368

13441369
# AsyncWith(withitem* items, stmt* body, string? type_comment)
13451370
def visit_AsyncWith(self, n: ast3.AsyncWith) -> WithStmt:
1371+
if n.type_comment is not None:
1372+
self.fail(
1373+
message_registry.TYPE_COMMENT_DEPRECATED, n.lineno, n.col_offset, blocker=False
1374+
)
13461375
target_type = self.translate_type_comment(n, n.type_comment)
13471376
s = WithStmt(
13481377
[self.visit(i.context_expr) for i in n.items],

mypy/message_registry.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,9 @@ def with_additional_msg(self, info: str) -> ErrorMessage:
311311
TYPE_COMMENT_SYNTAX_ERROR_VALUE: Final = ErrorMessage(
312312
'Syntax error in type comment "{}"', codes.SYNTAX
313313
)
314+
TYPE_COMMENT_DEPRECATED: Final = ErrorMessage(
315+
"Using type comments with mypy is deprecated, use inline annotations instead", codes.MISC
316+
)
314317
ELLIPSIS_WITH_OTHER_TYPEPARAMS: Final = ErrorMessage(
315318
"Ellipses cannot accompany other parameter types in function type signature", codes.SYNTAX
316319
)

0 commit comments

Comments
 (0)