Skip to content

Commit ae5771e

Browse files
committed
Fixed check for type comments
1 parent 21e7919 commit ae5771e

File tree

4 files changed

+21
-10
lines changed

4 files changed

+21
-10
lines changed

mypy/fastparse.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
from mypy.types import (
3333
Type, CallableType, AnyType, UnboundType, TupleType, TypeList, EllipsisType, CallableArgument,
3434
TypeOfAny, Instance, RawExpressionType, ProperType,
35-
UnionType)
35+
UnionType, Pep604Syntax)
3636
from mypy import defaults
3737
from mypy import message_registry, errorcodes as codes
3838
from 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):

mypy/typeanal.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -605,7 +605,9 @@ def visit_star_type(self, t: StarType) -> Type:
605605
return StarType(self.anal_type(t.type), t.line)
606606

607607
def visit_union_type(self, t: UnionType) -> Type:
608-
if (t.uses_pep604_syntax is True
608+
if (t.pep604_syntax
609+
and t.pep604_syntax.uses_pep_604_syntax is True
610+
and t.pep604_syntax.is_type_comment is False
609611
and self.api.is_stub_file is False
610612
and self.options.python_version < (3, 10)
611613
and self.api.is_future_flag_set('annotations') is False):

mypy/types.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1719,18 +1719,23 @@ def serialize(self) -> JsonDict:
17191719
assert False, "Synthetic types don't serialize"
17201720

17211721

1722+
class Pep604Syntax(NamedTuple):
1723+
uses_pep_604_syntax: bool
1724+
is_type_comment: bool
1725+
1726+
17221727
class UnionType(ProperType):
17231728
"""The union type Union[T1, ..., Tn] (at least one type argument)."""
17241729

1725-
__slots__ = ('items',)
1730+
__slots__ = ('items', 'pep604_syntax')
17261731

17271732
def __init__(self, items: Sequence[Type], line: int = -1, column: int = -1,
1728-
uses_pep604_syntax: bool = False) -> None:
1733+
pep604_syntax: Optional[Pep604Syntax] = None) -> None:
17291734
super().__init__(line, column)
17301735
self.items = flatten_nested_unions(items)
17311736
self.can_be_true = any(item.can_be_true for item in items)
17321737
self.can_be_false = any(item.can_be_false for item in items)
1733-
self.uses_pep604_syntax = uses_pep604_syntax
1738+
self.pep604_syntax = pep604_syntax
17341739

17351740
def __hash__(self) -> int:
17361741
return hash(frozenset(self.items))

test-data/unit/check-union-or-syntax.test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ y: 42 | int # E: Invalid type: try using Literal[42] instead?
6161
z: str | 42 | int # E: Invalid type: try using Literal[42] instead?
6262

6363
[case testUnionOrSyntaxInComment]
64-
# flags: --python-version 3.10
64+
# flags: --python-version 3.6
6565
x = 1 # type: int | str
6666

6767
[case testUnionOrSyntaxFutureImport]

0 commit comments

Comments
 (0)