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

Lines changed: 29 additions & 46 deletions

File tree

‎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.

0 commit comments

Comments
 (0)