3232from mypy .types import (
3333 Type , CallableType , AnyType , UnboundType , TupleType , TypeList , EllipsisType , CallableArgument ,
3434 TypeOfAny , Instance , RawExpressionType , ProperType ,
35- UnionType )
35+ UnionType , Pep604Syntax )
3636from mypy import defaults
3737from mypy import message_registry , errorcodes as codes
3838from mypy .errors import Errors
@@ -241,7 +241,8 @@ def parse_type_comment(type_comment: str,
241241 converted = TypeConverter (errors ,
242242 line = line ,
243243 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 )
245246 return ignored , converted
246247
247248
@@ -1318,7 +1319,8 @@ def visit(self, node: ast3.expr) -> ProperType: ...
13181319 @overload
13191320 def visit (self , node : Optional [AST ]) -> Optional [ProperType ]: ...
13201321
1321- def visit (self , node : Optional [AST ]) -> Optional [ProperType ]:
1322+ def visit (self , node : Optional [AST ],
1323+ is_type_comment : Optional [bool ] = False ) -> Optional [ProperType ]:
13221324 """Modified visit -- keep track of the stack of nodes"""
13231325 if node is None :
13241326 return None
@@ -1327,6 +1329,8 @@ def visit(self, node: Optional[AST]) -> Optional[ProperType]:
13271329 method = 'visit_' + node .__class__ .__name__
13281330 visitor = getattr (self , method , None )
13291331 if visitor is not None :
1332+ if visitor == self .visit_BinOp :
1333+ return visitor (node , is_type_comment )
13301334 return visitor (node )
13311335 else :
13321336 return self .invalid_type (node )
@@ -1422,7 +1426,7 @@ def _extract_argument_name(self, n: ast3.expr) -> Optional[str]:
14221426 def visit_Name (self , n : Name ) -> Type :
14231427 return UnboundType (n .id , line = self .line , column = self .convert_column (n .col_offset ))
14241428
1425- def visit_BinOp (self , n : ast3 .BinOp ) -> Type :
1429+ def visit_BinOp (self , n : ast3 .BinOp , is_type_comment : Optional [ bool ] = False ) -> Type :
14261430 if not isinstance (n .op , ast3 .BitOr ):
14271431 return self .invalid_type (n )
14281432
@@ -1431,7 +1435,7 @@ def visit_BinOp(self, n: ast3.BinOp) -> Type:
14311435 return UnionType ([left , right ],
14321436 line = self .line ,
14331437 column = self .convert_column (n .col_offset ),
1434- uses_pep604_syntax = True )
1438+ pep604_syntax = Pep604Syntax ( True , is_type_comment ) )
14351439
14361440 def visit_NameConstant (self , n : NameConstant ) -> Type :
14371441 if isinstance (n .value , bool ):
0 commit comments