forked from python/mypy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
replacetvars.py
48 lines (36 loc) · 1.45 KB
/
replacetvars.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
from lex import Token
from mtypes import Type, Any, NoneTyp, TypeTranslator, TypeVar
from typerepr import AnyRepr
Type replace_type_vars(Type typ, bool func_tvars=True):
"""Replace type variable references in a type with the any type. If
func_tvars is false, only replace instance type variables.
"""
return typ.accept(ReplaceTypeVarsVisitor(func_tvars))
class ReplaceTypeVarsVisitor(TypeTranslator):
# Only override type variable handling; otherwise perform an indentity
# transformation.
bool func_tvars
void __init__(self, bool func_tvars):
self.func_tvars = func_tvars
Type visit_type_var(self, TypeVar t):
if t.id > 0 or self.func_tvars:
if t.repr is not None:
# Give a representation for the dynamic type.
tok = Token('any')
tok.pre = t.repr.name.pre
return Any(t.line, AnyRepr(tok))
else:
return Any()
else:
return t
Type replace_func_type_vars(Type typ, Type target_type):
"""Replace function type variables in a type with the target type."""
return typ.accept(ReplaceFuncTypeVarsVisitor(target_type))
class ReplaceFuncTypeVarsVisitor(TypeTranslator):
void __init__(self, Type target_type):
self.target_type = target_type
Type visit_type_var(self, TypeVar t):
if t.id < 0:
return self.target_type
else:
return t