Skip to content

Commit dde184f

Browse files
sixoletJukkaL
authored andcommitted
Use inheritance to allow most TypeVisitors not to deal with "synthetic" types (#3204)
* Use inheritance to allow most TypeVisitors not to deal with "synthetic" types This is a nearly-complete fix to #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.
1 parent 10855cb commit dde184f

11 files changed

+29
-46
lines changed

mypy/erasetype.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,8 @@ def erase_type(typ: Type) -> Type:
2626

2727

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

32-
def visit_type_list(self, t: TypeList) -> Type:
30+
def visit_unbound_type(self, t: UnboundType) -> Type:
3331
assert False, 'Not supported'
3432

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

mypy/expandtype.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,6 @@ def __init__(self, variables: Mapping[TypeVarId, Type]) -> None:
6363
def visit_unbound_type(self, t: UnboundType) -> Type:
6464
return t
6565

66-
def visit_type_list(self, t: TypeList) -> Type:
67-
assert False, 'Not supported'
68-
6966
def visit_any(self, t: AnyType) -> Type:
7067
return t
7168

mypy/fixup.py

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -177,9 +177,6 @@ def visit_callable_type(self, ct: CallableType) -> None:
177177
val.accept(self)
178178
v.upper_bound.accept(self)
179179

180-
def visit_ellipsis_type(self, e: EllipsisType) -> None:
181-
pass # Nothing to descend into.
182-
183180
def visit_overloaded(self, t: Overloaded) -> None:
184181
for ct in t.items():
185182
ct.accept(self)
@@ -210,10 +207,6 @@ def visit_typeddict_type(self, tdt: TypedDictType) -> None:
210207
if tdt.fallback is not None:
211208
tdt.fallback.accept(self)
212209

213-
def visit_type_list(self, tl: TypeList) -> None:
214-
for t in tl.items:
215-
t.accept(self)
216-
217210
def visit_type_var(self, tvt: TypeVarType) -> None:
218211
if tvt.values:
219212
for vt in tvt.values:

mypy/indirection.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from abc import abstractmethod
33

44
from mypy.visitor import NodeVisitor
5-
from mypy.types import TypeVisitor
5+
from mypy.types import SyntheticTypeVisitor
66
from mypy.nodes import MODULE_REF
77
import mypy.nodes as nodes
88
import mypy.types as types
@@ -19,7 +19,7 @@ def extract_module_names(type_name: Optional[str]) -> List[str]:
1919
return []
2020

2121

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

2525
def __init__(self) -> None:

mypy/join.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,6 @@ def visit_union_type(self, t: UnionType) -> Type:
107107
else:
108108
return UnionType.make_simplified_union([self.s, t])
109109

110-
def visit_type_list(self, t: TypeList) -> Type:
111-
assert False, 'Not supported'
112-
113110
def visit_any(self, t: AnyType) -> Type:
114111
return t
115112

mypy/meet.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,9 +139,6 @@ def visit_unbound_type(self, t: UnboundType) -> Type:
139139
else:
140140
return AnyType()
141141

142-
def visit_type_list(self, t: TypeList) -> Type:
143-
assert False, 'Not supported'
144-
145142
def visit_any(self, t: AnyType) -> Type:
146143
return self.s
147144

mypy/sametypes.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,6 @@ def __init__(self, right: Type) -> None:
5555
def visit_unbound_type(self, left: UnboundType) -> bool:
5656
return True
5757

58-
def visit_type_list(self, t: TypeList) -> bool:
59-
assert False, 'Not supported'
60-
6158
def visit_any(self, left: AnyType) -> bool:
6259
return isinstance(self.right, AnyType)
6360

mypy/server/astdiff.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,9 +136,6 @@ def __init__(self, right: Type) -> None:
136136
def visit_unbound_type(self, left: UnboundType) -> bool:
137137
return False
138138

139-
def visit_type_list(self, t: TypeList) -> bool:
140-
assert False, 'Not supported'
141-
142139
def visit_any(self, left: AnyType) -> bool:
143140
return isinstance(self.right, AnyType)
144141

mypy/subtypes.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,6 @@ def __init__(self, right: Type,
109109
def visit_unbound_type(self, left: UnboundType) -> bool:
110110
return True
111111

112-
def visit_type_list(self, t: TypeList) -> bool:
113-
assert False, 'Not supported'
114-
115112
def visit_any(self, left: AnyType) -> bool:
116113
return True
117114

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

566-
def visit_type_list(self, left: TypeList) -> bool:
567-
assert False, 'Should not happen'
568-
569563
def visit_any(self, left: AnyType) -> bool:
570564
return isinstance(self.right, AnyType)
571565

mypy/typeanal.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from mypy.types import (
77
Type, UnboundType, TypeVarType, TupleType, TypedDictType, UnionType, Instance,
88
AnyType, CallableType, NoneTyp, DeletedType, TypeList, TypeVarDef, TypeVisitor,
9+
SyntheticTypeVisitor,
910
StarType, PartialType, EllipsisType, UninhabitedType, TypeType, get_typ_args, set_typ_args,
1011
get_type_vars, union_items
1112
)
@@ -89,7 +90,7 @@ def no_subscript_builtin_alias(name: str, propose_alt: bool = True) -> str:
8990
return msg
9091

9192

92-
class TypeAnalyser(TypeVisitor[Type]):
93+
class TypeAnalyser(SyntheticTypeVisitor[Type]):
9394
"""Semantic analyzer for types (semantic analysis pass 2).
9495
9596
Converts unbound types into bound types.

mypy/types.py

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,7 @@ def __init__(self, items: List[Type], line: int = -1, column: int = -1) -> None:
238238
self.items = items
239239

240240
def accept(self, visitor: 'TypeVisitor[T]') -> T:
241+
assert isinstance(visitor, SyntheticTypeVisitor)
241242
return visitor.visit_type_list(self)
242243

243244
def serialize(self) -> JsonDict:
@@ -975,6 +976,7 @@ def __init__(self, type: Type, line: int = -1, column: int = -1) -> None:
975976
super().__init__(line, column)
976977

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

980982

@@ -1116,6 +1118,7 @@ class EllipsisType(Type):
11161118
"""
11171119

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

11211124
def serialize(self) -> JsonDict:
@@ -1199,9 +1202,6 @@ def _notimplemented_helper(self, name: str) -> NotImplementedError:
11991202
def visit_unbound_type(self, t: UnboundType) -> T:
12001203
pass
12011204

1202-
def visit_type_list(self, t: TypeList) -> T:
1203-
raise self._notimplemented_helper('type_list')
1204-
12051205
@abstractmethod
12061206
def visit_any(self, t: AnyType) -> T:
12071207
pass
@@ -1244,9 +1244,6 @@ def visit_tuple_type(self, t: TupleType) -> T:
12441244
def visit_typeddict_type(self, t: TypedDictType) -> T:
12451245
pass
12461246

1247-
def visit_star_type(self, t: StarType) -> T:
1248-
raise self._notimplemented_helper('star_type')
1249-
12501247
@abstractmethod
12511248
def visit_union_type(self, t: UnionType) -> T:
12521249
pass
@@ -1255,15 +1252,30 @@ def visit_union_type(self, t: UnionType) -> T:
12551252
def visit_partial_type(self, t: PartialType) -> T:
12561253
pass
12571254

1258-
def visit_ellipsis_type(self, t: EllipsisType) -> T:
1259-
raise self._notimplemented_helper('ellipsis_type')
1260-
12611255
@abstractmethod
12621256
def visit_type_type(self, t: TypeType) -> T:
12631257
pass
12641258

12651259

1266-
class TypeTranslator(TypeVisitor[Type]):
1260+
class SyntheticTypeVisitor(TypeVisitor[T]):
1261+
"""A TypeVisitor that also knows how to visit synthetic AST constructs.
1262+
1263+
Not just real types."""
1264+
1265+
@abstractmethod
1266+
def visit_star_type(self, t: StarType) -> T:
1267+
pass
1268+
1269+
@abstractmethod
1270+
def visit_type_list(self, t: TypeList) -> T:
1271+
pass
1272+
1273+
@abstractmethod
1274+
def visit_ellipsis_type(self, t: EllipsisType) -> T:
1275+
pass
1276+
1277+
1278+
class TypeTranslator(SyntheticTypeVisitor[Type]):
12671279
"""Identity type transformation.
12681280
12691281
Subclass this and override some methods to implement a non-trivial
@@ -1351,7 +1363,7 @@ def visit_type_type(self, t: TypeType) -> Type:
13511363
return TypeType(t.item.accept(self), line=t.line, column=t.column)
13521364

13531365

1354-
class TypeStrVisitor(TypeVisitor[str]):
1366+
class TypeStrVisitor(SyntheticTypeVisitor[str]):
13551367
"""Visitor for pretty-printing types into strings.
13561368
13571369
This is mostly for debugging/testing.
@@ -1510,7 +1522,7 @@ def keywords_str(self, a: Iterable[Tuple[str, Type]]) -> str:
15101522
])
15111523

15121524

1513-
class TypeQuery(Generic[T], TypeVisitor[T]):
1525+
class TypeQuery(SyntheticTypeVisitor[T]):
15141526
"""Visitor for performing queries of types.
15151527
15161528
strategy is used to combine results for a series of types

0 commit comments

Comments
 (0)