Skip to content

Commit 533202e

Browse files
committed
Fixed check for type comments
1 parent c429e45 commit 533202e

File tree

4 files changed

+26
-10
lines changed

4 files changed

+26
-10
lines changed

mypy/fastparse.py

+14-5
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,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):

mypy/typeanal.py

+3-1
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 is not None
609+
and t.pep604_syntax.uses_pep604_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

+8-3
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+
Pep604Syntax = NamedTuple('Pep604Syntax', [
1723+
('uses_pep604_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', 'uses_pep604_syntax')
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

+1-1
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)