def visit_type_var(self, t: TypeVar) -> Type: 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 AnyType(t.line, AnyRepr(tok)) else: return AnyType() else: return t
"""Type parser""" from typing import List, Tuple, Union, cast, Optional from mypy.types import (Type, UnboundType, TupleType, TypeList, CallableType, StarType, EllipsisType) from mypy.lex import Token, Name, StrLit, lex from mypy import nodes none = Token('') # Empty token class TypeParseError(Exception): def __init__(self, token: Token, index: int, message: Optional[str] = None) -> None: super().__init__() self.token = token self.index = index self.message = message def parse_type(tok: List[Token], index: int) -> Tuple[Type, int]: """Parse a type. Return (type, index after type). """ p = TypeParser(tok, index) return p.parse_type(), p.index()
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):