Skip to content

Commit

Permalink
Use inheritance to allow most TypeVisitors not to deal with "syntheti…
Browse files Browse the repository at this point in the history
…c" types

This is a nearly-complete fix to python#730 --
the trick is that some types aren't real, they're synthetic AST constructs, and
so we shouldn't have to deal with those in every type visitor. Only the type
visitors that deal with types before the synthetic constructs are analyzed away.
  • Loading branch information
sixolet committed Apr 20, 2017
1 parent 9aef23b commit 8f7d5c7
Show file tree
Hide file tree
Showing 10 changed files with 30 additions and 45 deletions.
4 changes: 1 addition & 3 deletions mypy/erasetype.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,8 @@ def erase_type(typ: Type) -> Type:


class EraseTypeVisitor(TypeVisitor[Type]):
def visit_unbound_type(self, t: UnboundType) -> Type:
assert False, 'Not supported'

def visit_type_list(self, t: TypeList) -> Type:
def visit_unbound_type(self, t: UnboundType) -> Type:
assert False, 'Not supported'

def visit_any(self, t: AnyType) -> Type:
Expand Down
3 changes: 0 additions & 3 deletions mypy/expandtype.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,6 @@ def __init__(self, variables: Mapping[TypeVarId, Type]) -> None:
def visit_unbound_type(self, t: UnboundType) -> Type:
return t

def visit_type_list(self, t: TypeList) -> Type:
assert False, 'Not supported'

def visit_any(self, t: AnyType) -> Type:
return t

Expand Down
7 changes: 0 additions & 7 deletions mypy/fixup.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,9 +177,6 @@ def visit_callable_type(self, ct: CallableType) -> None:
val.accept(self)
v.upper_bound.accept(self)

def visit_ellipsis_type(self, e: EllipsisType) -> None:
pass # Nothing to descend into.

def visit_overloaded(self, t: Overloaded) -> None:
for ct in t.items():
ct.accept(self)
Expand Down Expand Up @@ -210,10 +207,6 @@ def visit_typeddict_type(self, tdt: TypedDictType) -> None:
if tdt.fallback is not None:
tdt.fallback.accept(self)

def visit_type_list(self, tl: TypeList) -> None:
for t in tl.items:
t.accept(self)

def visit_type_var(self, tvt: TypeVarType) -> None:
if tvt.values:
for vt in tvt.values:
Expand Down
4 changes: 2 additions & 2 deletions mypy/indirection.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from abc import abstractmethod

from mypy.visitor import NodeVisitor
from mypy.types import TypeVisitor
from mypy.types import SyntheticTypeVisitor
from mypy.nodes import MODULE_REF
import mypy.nodes as nodes
import mypy.types as types
Expand All @@ -19,7 +19,7 @@ def extract_module_names(type_name: Optional[str]) -> List[str]:
return []


class TypeIndirectionVisitor(TypeVisitor[Set[str]]):
class TypeIndirectionVisitor(SyntheticTypeVisitor[Set[str]]):
"""Returns all module references within a particular type."""

def __init__(self) -> None:
Expand Down
3 changes: 0 additions & 3 deletions mypy/join.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,6 @@ def visit_union_type(self, t: UnionType) -> Type:
else:
return UnionType.make_simplified_union([self.s, t])

def visit_type_list(self, t: TypeList) -> Type:
assert False, 'Not supported'

def visit_any(self, t: AnyType) -> Type:
return t

Expand Down
3 changes: 0 additions & 3 deletions mypy/meet.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,6 @@ def visit_unbound_type(self, t: UnboundType) -> Type:
else:
return AnyType()

def visit_type_list(self, t: TypeList) -> Type:
assert False, 'Not supported'

def visit_any(self, t: AnyType) -> Type:
return self.s

Expand Down
3 changes: 0 additions & 3 deletions mypy/sametypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,6 @@ def __init__(self, right: Type) -> None:
def visit_unbound_type(self, left: UnboundType) -> bool:
return True

def visit_type_list(self, t: TypeList) -> bool:
assert False, 'Not supported'

def visit_any(self, left: AnyType) -> bool:
return isinstance(self.right, AnyType)

Expand Down
3 changes: 0 additions & 3 deletions mypy/server/astdiff.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,6 @@ def __init__(self, right: Type) -> None:
def visit_unbound_type(self, left: UnboundType) -> bool:
return False

def visit_type_list(self, t: TypeList) -> bool:
assert False, 'Not supported'

def visit_any(self, left: AnyType) -> bool:
return isinstance(self.right, AnyType)

Expand Down
6 changes: 0 additions & 6 deletions mypy/subtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,6 @@ def __init__(self, right: Type,
def visit_unbound_type(self, left: UnboundType) -> bool:
return True

def visit_type_list(self, t: TypeList) -> bool:
assert False, 'Not supported'

def visit_any(self, left: AnyType) -> bool:
return True

Expand Down Expand Up @@ -563,9 +560,6 @@ def visit_unbound_type(self, left: UnboundType) -> bool:
# from unions, which could filter out some bogus messages.
return True

def visit_type_list(self, left: TypeList) -> bool:
assert False, 'Should not happen'

def visit_any(self, left: AnyType) -> bool:
return isinstance(self.right, AnyType)

Expand Down
39 changes: 27 additions & 12 deletions mypy/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -975,6 +975,7 @@ def __init__(self, type: Type, line: int = -1, column: int = -1) -> None:
super().__init__(line, column)

def accept(self, visitor: 'TypeVisitor[T]') -> T:
assert isinstance(visitor, SyntheticTypeVisitor)
return visitor.visit_star_type(self)


Expand Down Expand Up @@ -1116,6 +1117,7 @@ class EllipsisType(Type):
"""

def accept(self, visitor: 'TypeVisitor[T]') -> T:
assert isinstance(visitor, SyntheticTypeVisitor)
return visitor.visit_ellipsis_type(self)

def serialize(self) -> JsonDict:
Expand Down Expand Up @@ -1199,9 +1201,6 @@ def _notimplemented_helper(self, name: str) -> NotImplementedError:
def visit_unbound_type(self, t: UnboundType) -> T:
pass

def visit_type_list(self, t: TypeList) -> T:
raise self._notimplemented_helper('type_list')

@abstractmethod
def visit_any(self, t: AnyType) -> T:
pass
Expand Down Expand Up @@ -1244,9 +1243,6 @@ def visit_tuple_type(self, t: TupleType) -> T:
def visit_typeddict_type(self, t: TypedDictType) -> T:
pass

def visit_star_type(self, t: StarType) -> T:
raise self._notimplemented_helper('star_type')

@abstractmethod
def visit_union_type(self, t: UnionType) -> T:
pass
Expand All @@ -1255,15 +1251,34 @@ def visit_union_type(self, t: UnionType) -> T:
def visit_partial_type(self, t: PartialType) -> T:
pass

def visit_ellipsis_type(self, t: EllipsisType) -> T:
raise self._notimplemented_helper('ellipsis_type')

@abstractmethod
def visit_type_type(self, t: TypeType) -> T:
pass


class TypeTranslator(TypeVisitor[Type]):
class SyntheticTypeVisitor(TypeVisitor[T]):
"""A TypeVisitor that also knows how to visit synthetic AST constructs.
Not just real types."""

@abstractmethod
def visit_star_type(self, t: StarType) -> T:
pass

@abstractmethod
def visit_type_list(self, t: TypeList) -> T:
pass

@abstractmethod
def visit_callable_argument(self, t: CallableArgument) -> T:
pass

@abstractmethod
def visit_ellipsis_type(self, t: EllipsisType) -> T:
pass


class TypeTranslator(SyntheticTypeVisitor[Type]):
"""Identity type transformation.
Subclass this and override some methods to implement a non-trivial
Expand Down Expand Up @@ -1351,7 +1366,7 @@ def visit_type_type(self, t: TypeType) -> Type:
return TypeType(t.item.accept(self), line=t.line, column=t.column)


class TypeStrVisitor(TypeVisitor[str]):
class TypeStrVisitor(SyntheticTypeVisitor[str]):
"""Visitor for pretty-printing types into strings.
This is mostly for debugging/testing.
Expand Down Expand Up @@ -1510,7 +1525,7 @@ def keywords_str(self, a: Iterable[Tuple[str, Type]]) -> str:
])


class TypeQuery(Generic[T], TypeVisitor[T]):
class TypeQuery(SyntheticTypeVisitor[T]):
"""Visitor for performing queries of types.
strategy is used to combine results for a series of types
Expand Down

0 comments on commit 8f7d5c7

Please sign in to comment.