Skip to content

Commit b0fa476

Browse files
committed
Deprecate type comments
1 parent dd55b66 commit b0fa476

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
@@ -930,6 +930,9 @@ def do_func_def(
930930
arg_types = [None] * len(args)
931931
return_type = None
932932
elif n.type_comment is not None:
933+
self.fail(
934+
message_registry.TYPE_COMMENT_DEPRECATED, lineno, n.col_offset, blocker=False
935+
)
933936
try:
934937
func_type_ast = ast3_parse(n.type_comment, "<func_type>", "func_type")
935938
assert isinstance(func_type_ast, FunctionType)
@@ -1141,7 +1144,13 @@ def make_argument(
11411144
arg_type = None
11421145
if annotation is not None:
11431146
arg_type = TypeConverter(self.errors, line=arg.lineno).visit(annotation)
1144-
else:
1147+
elif type_comment is not None:
1148+
self.fail(
1149+
message_registry.TYPE_COMMENT_DEPRECATED,
1150+
arg.lineno,
1151+
arg.col_offset,
1152+
blocker=False,
1153+
)
11451154
arg_type = self.translate_type_comment(arg, type_comment)
11461155
if argument_elide_name(arg.arg):
11471156
pos_only = True
@@ -1269,6 +1278,10 @@ def visit_Delete(self, n: ast3.Delete) -> DelStmt:
12691278
def visit_Assign(self, n: ast3.Assign) -> AssignmentStmt:
12701279
lvalues = self.translate_expr_list(n.targets)
12711280
rvalue = self.visit(n.value)
1281+
if n.type_comment is not None:
1282+
self.fail(
1283+
message_registry.TYPE_COMMENT_DEPRECATED, n.lineno, n.col_offset, blocker=False
1284+
)
12721285
typ = self.translate_type_comment(n, n.type_comment)
12731286
s = AssignmentStmt(lvalues, rvalue, type=typ, new_syntax=False)
12741287
return self.set_line(s, n)
@@ -1296,6 +1309,10 @@ def visit_AugAssign(self, n: ast3.AugAssign) -> OperatorAssignmentStmt:
12961309

12971310
# For(expr target, expr iter, stmt* body, stmt* orelse, string? type_comment)
12981311
def visit_For(self, n: ast3.For) -> ForStmt:
1312+
if n.type_comment is not None:
1313+
self.fail(
1314+
message_registry.TYPE_COMMENT_DEPRECATED, n.lineno, n.col_offset, blocker=False
1315+
)
12991316
target_type = self.translate_type_comment(n, n.type_comment)
13001317
node = ForStmt(
13011318
self.visit(n.target),
@@ -1308,6 +1325,10 @@ def visit_For(self, n: ast3.For) -> ForStmt:
13081325

13091326
# AsyncFor(expr target, expr iter, stmt* body, stmt* orelse, string? type_comment)
13101327
def visit_AsyncFor(self, n: ast3.AsyncFor) -> ForStmt:
1328+
if n.type_comment is not None:
1329+
self.fail(
1330+
message_registry.TYPE_COMMENT_DEPRECATED, n.lineno, n.col_offset, blocker=False
1331+
)
13111332
target_type = self.translate_type_comment(n, n.type_comment)
13121333
node = ForStmt(
13131334
self.visit(n.target),
@@ -1335,6 +1356,10 @@ def visit_If(self, n: ast3.If) -> IfStmt:
13351356

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

13471372
# AsyncWith(withitem* items, stmt* body, string? type_comment)
13481373
def visit_AsyncWith(self, n: ast3.AsyncWith) -> WithStmt:
1374+
if n.type_comment is not None:
1375+
self.fail(
1376+
message_registry.TYPE_COMMENT_DEPRECATED, n.lineno, n.col_offset, blocker=False
1377+
)
13491378
target_type = self.translate_type_comment(n, n.type_comment)
13501379
s = WithStmt(
13511380
[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
@@ -308,6 +308,9 @@ def with_additional_msg(self, info: str) -> ErrorMessage:
308308
TYPE_COMMENT_SYNTAX_ERROR_VALUE: Final = ErrorMessage(
309309
'Syntax error in type comment "{}"', codes.SYNTAX
310310
)
311+
TYPE_COMMENT_DEPRECATED: Final = ErrorMessage(
312+
"Using type comments with mypy is deprecated, use inline annotations instead", codes.MISC
313+
)
311314
ELLIPSIS_WITH_OTHER_TYPEARGS: Final = ErrorMessage(
312315
"Ellipses cannot accompany other argument types in function type signature", codes.SYNTAX
313316
)

0 commit comments

Comments
 (0)