MCPcopy Index your code
hub / github.com/python/mypy / EraseTypeVisitor

Class EraseTypeVisitor

mypy/erasetype.py:57–142  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

55
56
57class EraseTypeVisitor(TypeVisitor[ProperType]):
58 def visit_unbound_type(self, t: UnboundType) -> ProperType:
59 # TODO: replace with an assert after UnboundType can't leak from semantic analysis.
60 return AnyType(TypeOfAny.from_error)
61
62 def visit_any(self, t: AnyType) -> ProperType:
63 return t
64
65 def visit_none_type(self, t: NoneType) -> ProperType:
66 return t
67
68 def visit_uninhabited_type(self, t: UninhabitedType) -> ProperType:
69 return t
70
71 def visit_erased_type(self, t: ErasedType) -> ProperType:
72 return t
73
74 def visit_partial_type(self, t: PartialType) -> ProperType:
75 # Should not get here.
76 raise RuntimeError("Cannot erase partial types")
77
78 def visit_deleted_type(self, t: DeletedType) -> ProperType:
79 return t
80
81 def visit_instance(self, t: Instance) -> ProperType:
82 args = erased_vars(t.type.defn.type_vars, TypeOfAny.special_form)
83 return Instance(t.type, args, t.line)
84
85 def visit_type_var(self, t: TypeVarType) -> ProperType:
86 return AnyType(TypeOfAny.special_form)
87
88 def visit_param_spec(self, t: ParamSpecType) -> ProperType:
89 return AnyType(TypeOfAny.special_form)
90
91 def visit_parameters(self, t: Parameters) -> ProperType:
92 raise RuntimeError("Parameters should have been bound to a class")
93
94 def visit_type_var_tuple(self, t: TypeVarTupleType) -> ProperType:
95 # Likely, we can never get here because of aggressive erasure of types that
96 # can contain this, but better still return a valid replacement.
97 return t.tuple_fallback.copy_modified(args=[AnyType(TypeOfAny.special_form)])
98
99 def visit_unpack_type(self, t: UnpackType) -> ProperType:
100 return AnyType(TypeOfAny.special_form)
101
102 def visit_callable_type(self, t: CallableType) -> ProperType:
103 # We must preserve the fallback type for overload resolution to work.
104 any_type = AnyType(TypeOfAny.special_form)
105 return CallableType(
106 arg_types=[any_type, any_type],
107 arg_kinds=[ARG_STAR, ARG_STAR2],
108 arg_names=[None, None],
109 ret_type=any_type,
110 fallback=t.fallback,
111 is_ellipsis_args=True,
112 implicit=True,
113 )
114

Callers 1

erase_typeFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…