32
32
from mypy .types import (
33
33
Type , CallableType , AnyType , UnboundType , TupleType , TypeList , EllipsisType , CallableArgument ,
34
34
TypeOfAny , Instance , RawExpressionType , ProperType ,
35
- UnionType )
35
+ UnionType , Pep604Syntax )
36
36
from mypy import defaults
37
37
from mypy import message_registry , errorcodes as codes
38
38
from mypy .errors import Errors
@@ -241,7 +241,8 @@ def parse_type_comment(type_comment: str,
241
241
converted = TypeConverter (errors ,
242
242
line = line ,
243
243
override_column = column ,
244
- assume_str_is_unicode = assume_str_is_unicode ).visit (typ .body )
244
+ assume_str_is_unicode = assume_str_is_unicode
245
+ ).visit (typ .body , is_type_comment = True )
245
246
return ignored , converted
246
247
247
248
@@ -1318,7 +1319,13 @@ def visit(self, node: ast3.expr) -> ProperType: ...
1318
1319
@overload
1319
1320
def visit (self , node : Optional [AST ]) -> Optional [ProperType ]: ...
1320
1321
1321
- def visit (self , node : Optional [AST ]) -> Optional [ProperType ]:
1322
+ @overload
1323
+ def visit (self , node : ast3 .expr , is_type_comment : bool ) -> ProperType : ...
1324
+
1325
+ @overload
1326
+ def visit (self , node : Optional [AST ], is_type_comment : bool ) -> Optional [ProperType ]: ...
1327
+
1328
+ def visit (self , node : Optional [AST ], is_type_comment : bool = False ) -> Optional [ProperType ]:
1322
1329
"""Modified visit -- keep track of the stack of nodes"""
1323
1330
if node is None :
1324
1331
return None
@@ -1327,6 +1334,8 @@ def visit(self, node: Optional[AST]) -> Optional[ProperType]:
1327
1334
method = 'visit_' + node .__class__ .__name__
1328
1335
visitor = getattr (self , method , None )
1329
1336
if visitor is not None :
1337
+ if visitor == self .visit_BinOp :
1338
+ return visitor (node , is_type_comment )
1330
1339
return visitor (node )
1331
1340
else :
1332
1341
return self .invalid_type (node )
@@ -1422,7 +1431,7 @@ def _extract_argument_name(self, n: ast3.expr) -> Optional[str]:
1422
1431
def visit_Name (self , n : Name ) -> Type :
1423
1432
return UnboundType (n .id , line = self .line , column = self .convert_column (n .col_offset ))
1424
1433
1425
- def visit_BinOp (self , n : ast3 .BinOp ) -> Type :
1434
+ def visit_BinOp (self , n : ast3 .BinOp , is_type_comment : bool = False ) -> Type :
1426
1435
if not isinstance (n .op , ast3 .BitOr ):
1427
1436
return self .invalid_type (n )
1428
1437
@@ -1431,7 +1440,7 @@ def visit_BinOp(self, n: ast3.BinOp) -> Type:
1431
1440
return UnionType ([left , right ],
1432
1441
line = self .line ,
1433
1442
column = self .convert_column (n .col_offset ),
1434
- uses_pep604_syntax = True )
1443
+ pep604_syntax = Pep604Syntax ( True , is_type_comment ) )
1435
1444
1436
1445
def visit_NameConstant (self , n : NameConstant ) -> Type :
1437
1446
if isinstance (n .value , bool ):
0 commit comments