Skip to content

Commit

Permalink
Review changes + mypyc fix
Browse files Browse the repository at this point in the history
  • Loading branch information
cdce8p committed Nov 1, 2020
1 parent 61d4c6c commit 21e7919
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 8 deletions.
2 changes: 1 addition & 1 deletion mypy/fastparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -1431,7 +1431,7 @@ def visit_BinOp(self, n: ast3.BinOp) -> Type:
return UnionType([left, right],
line=self.line,
column=self.convert_column(n.col_offset),
is_binary_op=True)
uses_pep604_syntax=True)

def visit_NameConstant(self, n: NameConstant) -> Type:
if isinstance(n.value, bool):
Expand Down
8 changes: 6 additions & 2 deletions mypy/semanal.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ class SemanticAnalyzer(NodeVisitor[None],
patches = None # type: List[Tuple[int, Callable[[], None]]]
loop_depth = 0 # Depth of breakable loops
cur_mod_id = '' # Current module id (or None) (phase 2)
is_stub_file = False # Are we analyzing a stub file?
_is_stub_file = False # Are we analyzing a stub file?
_is_typeshed_stub_file = False # Are we analyzing a typeshed stub file?
imports = None # type: Set[str] # Imported modules (during phase 2 analysis)
# Note: some imports (and therefore dependencies) might
Expand Down Expand Up @@ -280,6 +280,10 @@ def __init__(self,

# mypyc doesn't properly handle implementing an abstractproperty
# with a regular attribute so we make them properties
@property
def is_stub_file(self) -> bool:
return self._is_stub_file

@property
def is_typeshed_stub_file(self) -> bool:
return self._is_typeshed_stub_file
Expand Down Expand Up @@ -507,7 +511,7 @@ def file_context(self,
self.cur_mod_node = file_node
self.cur_mod_id = file_node.fullname
scope.enter_file(self.cur_mod_id)
self.is_stub_file = file_node.path.lower().endswith('.pyi')
self._is_stub_file = file_node.path.lower().endswith('.pyi')
self._is_typeshed_stub_file = is_typeshed_file(file_node.path)
self.globals = file_node.names
self.tvar_scope = TypeVarLikeScope()
Expand Down
5 changes: 5 additions & 0 deletions mypy/semanal_shared.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@ def is_future_flag_set(self, flag: str) -> bool:
"""Is the specific __future__ feature imported"""
raise NotImplementedError

@property
@abstractmethod
def is_stub_file(self) -> bool:
raise NotImplementedError


@trait
class SemanticAnalyzerInterface(SemanticAnalyzerCoreInterface):
Expand Down
4 changes: 2 additions & 2 deletions mypy/typeanal.py
Original file line number Diff line number Diff line change
Expand Up @@ -605,11 +605,11 @@ def visit_star_type(self, t: StarType) -> Type:
return StarType(self.anal_type(t.type), t.line)

def visit_union_type(self, t: UnionType) -> Type:
if (t.is_binary_op is True
if (t.uses_pep604_syntax is True
and self.api.is_stub_file is False
and self.options.python_version < (3, 10)
and self.api.is_future_flag_set('annotations') is False):
self.fail("Alternative syntax for unions requires Python 3.10 or newer", t)
self.fail("X | Y syntax for unions requires Python 3.10", t)
return UnionType(self.anal_array(t.items), t.line)

def visit_partial_type(self, t: PartialType) -> Type:
Expand Down
4 changes: 2 additions & 2 deletions mypy/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -1725,12 +1725,12 @@ class UnionType(ProperType):
__slots__ = ('items',)

def __init__(self, items: Sequence[Type], line: int = -1, column: int = -1,
is_binary_op: bool = False) -> None:
uses_pep604_syntax: bool = False) -> None:
super().__init__(line, column)
self.items = flatten_nested_unions(items)
self.can_be_true = any(item.can_be_true for item in items)
self.can_be_false = any(item.can_be_false for item in items)
self.is_binary_op = is_binary_op
self.uses_pep604_syntax = uses_pep604_syntax

def __hash__(self) -> int:
return hash(frozenset(self.items))
Expand Down
2 changes: 1 addition & 1 deletion test-data/unit/check-union-or-syntax.test
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ x: int | None

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

[case testUnionOrSyntaxInStubFile]
# flags: --python-version 3.6
Expand Down

0 comments on commit 21e7919

Please sign in to comment.