Skip to content

Commit c429e45

Browse files
committed
Review changes + mypyc fix
1 parent 61d4c6c commit c429e45

File tree

6 files changed

+18
-9
lines changed

6 files changed

+18
-9
lines changed

mypy/fastparse.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1431,7 +1431,7 @@ def visit_BinOp(self, n: ast3.BinOp) -> Type:
14311431
return UnionType([left, right],
14321432
line=self.line,
14331433
column=self.convert_column(n.col_offset),
1434-
is_binary_op=True)
1434+
uses_pep604_syntax=True)
14351435

14361436
def visit_NameConstant(self, n: NameConstant) -> Type:
14371437
if isinstance(n.value, bool):

mypy/semanal.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ class SemanticAnalyzer(NodeVisitor[None],
206206
patches = None # type: List[Tuple[int, Callable[[], None]]]
207207
loop_depth = 0 # Depth of breakable loops
208208
cur_mod_id = '' # Current module id (or None) (phase 2)
209-
is_stub_file = False # Are we analyzing a stub file?
209+
_is_stub_file = False # Are we analyzing a stub file?
210210
_is_typeshed_stub_file = False # Are we analyzing a typeshed stub file?
211211
imports = None # type: Set[str] # Imported modules (during phase 2 analysis)
212212
# Note: some imports (and therefore dependencies) might
@@ -280,6 +280,10 @@ def __init__(self,
280280

281281
# mypyc doesn't properly handle implementing an abstractproperty
282282
# with a regular attribute so we make them properties
283+
@property
284+
def is_stub_file(self) -> bool:
285+
return self._is_stub_file
286+
283287
@property
284288
def is_typeshed_stub_file(self) -> bool:
285289
return self._is_typeshed_stub_file
@@ -507,7 +511,7 @@ def file_context(self,
507511
self.cur_mod_node = file_node
508512
self.cur_mod_id = file_node.fullname
509513
scope.enter_file(self.cur_mod_id)
510-
self.is_stub_file = file_node.path.lower().endswith('.pyi')
514+
self._is_stub_file = file_node.path.lower().endswith('.pyi')
511515
self._is_typeshed_stub_file = is_typeshed_file(file_node.path)
512516
self.globals = file_node.names
513517
self.tvar_scope = TypeVarLikeScope()

mypy/semanal_shared.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,11 @@ def is_future_flag_set(self, flag: str) -> bool:
7878
"""Is the specific __future__ feature imported"""
7979
raise NotImplementedError
8080

81+
@property
82+
@abstractmethod
83+
def is_stub_file(self) -> bool:
84+
raise NotImplementedError
85+
8186

8287
@trait
8388
class SemanticAnalyzerInterface(SemanticAnalyzerCoreInterface):

mypy/typeanal.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -605,11 +605,11 @@ 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.is_binary_op is True
608+
if (t.uses_pep604_syntax is True
609609
and self.api.is_stub_file is False
610610
and self.options.python_version < (3, 10)
611611
and self.api.is_future_flag_set('annotations') is False):
612-
self.fail("Alternative syntax for unions requires Python 3.10 or newer", t)
612+
self.fail("X | Y syntax for unions requires Python 3.10", t)
613613
return UnionType(self.anal_array(t.items), t.line)
614614

615615
def visit_partial_type(self, t: PartialType) -> Type:

mypy/types.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1722,15 +1722,15 @@ def serialize(self) -> JsonDict:
17221722
class UnionType(ProperType):
17231723
"""The union type Union[T1, ..., Tn] (at least one type argument)."""
17241724

1725-
__slots__ = ('items',)
1725+
__slots__ = ('items', 'uses_pep604_syntax')
17261726

17271727
def __init__(self, items: Sequence[Type], line: int = -1, column: int = -1,
1728-
is_binary_op: bool = False) -> None:
1728+
uses_pep604_syntax: bool = False) -> None:
17291729
super().__init__(line, column)
17301730
self.items = flatten_nested_unions(items)
17311731
self.can_be_true = any(item.can_be_true for item in items)
17321732
self.can_be_false = any(item.can_be_false for item in items)
1733-
self.is_binary_op = is_binary_op
1733+
self.uses_pep604_syntax = uses_pep604_syntax
17341734

17351735
def __hash__(self) -> int:
17361736
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
@@ -72,7 +72,7 @@ x: int | None
7272

7373
[case testUnionOrSyntaxMissingFutureImport]
7474
# flags: --python-version 3.9
75-
x: int | None # E: Alternative syntax for unions requires Python 3.10 or newer
75+
x: int | None # E: X | Y syntax for unions requires Python 3.10
7676

7777
[case testUnionOrSyntaxInStubFile]
7878
# flags: --python-version 3.6

0 commit comments

Comments
 (0)