Skip to content

Commit 8c0ca90

Browse files
committed
Refactoring: remove replacetvars.py
Remove the unused function replace_type_vars, and move the function replace_func_type_vars to erasetype to share implementation with erase_typevars.
1 parent e0b0101 commit 8c0ca90

File tree

5 files changed

+24
-79
lines changed

5 files changed

+24
-79
lines changed

mypy/checkexpr.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
import mypy.checker
2323
from mypy import types
2424
from mypy.sametypes import is_same_type
25-
from mypy.replacetvars import replace_func_type_vars
25+
from mypy.erasetype import replace_func_type_vars
2626
from mypy.messages import MessageBuilder
2727
from mypy import messages
2828
from mypy.infer import infer_type_arguments, infer_function_type_arguments

mypy/erasetype.py

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Optional, Container
1+
from typing import Optional, Container, Callable
22

33
from mypy.types import (
44
Type, TypeVisitor, UnboundType, ErrorType, AnyType, Void, NoneTyp, TypeVarId,
@@ -109,16 +109,26 @@ def erase_typevars(t: Type, ids_to_erase: Optional[Container[TypeVarId]] = None)
109109
"""Replace all type variables in a type with any,
110110
or just the ones in the provided collection.
111111
"""
112-
return t.accept(TypeVarEraser(ids_to_erase))
112+
def erase_id(id: TypeVarId) -> bool:
113+
if ids_to_erase is None:
114+
return True
115+
return id in ids_to_erase
116+
return t.accept(TypeVarEraser(erase_id, AnyType()))
117+
118+
119+
def replace_func_type_vars(t: Type, target_type: Type) -> Type:
120+
"""Replace function type variables in a type with the target type."""
121+
return t.accept(TypeVarEraser(lambda id: id.is_func_var(), target_type))
113122

114123

115124
class TypeVarEraser(TypeTranslator):
116125
"""Implementation of type erasure"""
117126

118-
def __init__(self, ids_to_erase: Optional[Container[TypeVarId]]) -> None:
119-
self.ids_to_erase = ids_to_erase
127+
def __init__(self, erase_id: Callable[[TypeVarId], bool], replacement: Type) -> None:
128+
self.erase_id = erase_id
129+
self.replacement = replacement
120130

121131
def visit_type_var(self, t: TypeVarType) -> Type:
122-
if self.ids_to_erase is not None and t.id not in self.ids_to_erase:
123-
return t
124-
return AnyType()
132+
if self.erase_id(t.id):
133+
return self.replacement
134+
return t

mypy/replacetvars.py

Lines changed: 0 additions & 45 deletions
This file was deleted.

mypy/test/testtypes.py

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
Instance, NoneTyp, ErrorType, Overloaded, TypeType,
1515
)
1616
from mypy.nodes import ARG_POS, ARG_OPT, ARG_STAR, CONTRAVARIANT, INVARIANT, COVARIANT
17-
from mypy.replacetvars import replace_type_vars
1817
from mypy.subtypes import is_subtype, is_more_precise, is_proper_subtype
1918
from mypy.typefixture import TypeFixture, InterfaceTypeFixture
2019

@@ -132,25 +131,6 @@ def assert_expand(self, orig, map_items, result):
132131
# Remove erased tags (asterisks).
133132
assert_equal(str(exp).replace('*', ''), str(result))
134133

135-
# replace_type_vars
136-
137-
def test_trivial_replace(self):
138-
for t in (self.fx.a, self.fx.o, self.fx.void, self.fx.nonet,
139-
self.tuple(self.fx.a),
140-
self.callable([], self.fx.a, self.fx.a), self.fx.anyt,
141-
self.fx.err):
142-
self.assert_replace(t, t)
143-
144-
def test_replace_type_var(self):
145-
self.assert_replace(self.fx.t, self.fx.anyt)
146-
147-
def test_replace_generic_instance(self):
148-
self.assert_replace(self.fx.ga, self.fx.ga)
149-
self.assert_replace(self.fx.gt, self.fx.gdyn)
150-
151-
def assert_replace(self, orig, result):
152-
assert_equal(str(replace_type_vars(orig)), str(result))
153-
154134
# erase_type
155135

156136
def test_trivial_erase(self):

mypy/types.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,12 @@ def __ne__(self, other: object) -> bool:
7070
def __hash__(self) -> int:
7171
return hash(self.raw_id)
7272

73+
def is_class_var(self) -> bool:
74+
return self.raw_id > 0
75+
76+
def is_func_var(self) -> bool:
77+
return self.raw_id < 0
78+
7379

7480
class TypeVarDef(mypy.nodes.Context):
7581
"""Definition of a single type variable."""
@@ -411,12 +417,6 @@ def __init__(self, binder: TypeVarDef, line: int = -1) -> None:
411417
def accept(self, visitor: 'TypeVisitor[T]') -> T:
412418
return visitor.visit_type_var(self)
413419

414-
def is_class_var(self) -> bool:
415-
return self.id.raw_id > 0
416-
417-
def is_func_var(self) -> bool:
418-
return self.id.raw_id < 0
419-
420420
def erase_to_union_or_bound(self) -> Type:
421421
if self.values:
422422
return UnionType.make_simplified_union(self.values)

0 commit comments

Comments
 (0)