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,13 @@ 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+ @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 ]:
13221329 """Modified visit -- keep track of the stack of nodes"""
13231330 if node is None :
13241331 return None
@@ -1327,6 +1334,8 @@ def visit(self, node: Optional[AST]) -> Optional[ProperType]:
13271334 method = 'visit_' + node .__class__ .__name__
13281335 visitor = getattr (self , method , None )
13291336 if visitor is not None :
1337+ if visitor == self .visit_BinOp :
1338+ return visitor (node , is_type_comment )
13301339 return visitor (node )
13311340 else :
13321341 return self .invalid_type (node )
@@ -1422,7 +1431,7 @@ def _extract_argument_name(self, n: ast3.expr) -> Optional[str]:
14221431 def visit_Name (self , n : Name ) -> Type :
14231432 return UnboundType (n .id , line = self .line , column = self .convert_column (n .col_offset ))
14241433
1425- def visit_BinOp (self , n : ast3 .BinOp ) -> Type :
1434+ def visit_BinOp (self , n : ast3 .BinOp , is_type_comment : bool = False ) -> Type :
14261435 if not isinstance (n .op , ast3 .BitOr ):
14271436 return self .invalid_type (n )
14281437
@@ -1431,7 +1440,7 @@ def visit_BinOp(self, n: ast3.BinOp) -> Type:
14311440 return UnionType ([left , right ],
14321441 line = self .line ,
14331442 column = self .convert_column (n .col_offset ),
1434- uses_pep604_syntax = True )
1443+ pep604_syntax = Pep604Syntax ( True , is_type_comment ) )
14351444
14361445 def visit_NameConstant (self , n : NameConstant ) -> Type :
14371446 if isinstance (n .value , bool ):
0 commit comments