|
2 | 2 |
|
3 | 3 | from mypy.types import ( |
4 | 4 | Type, AnyType, UnboundType, TypeVisitor, ErrorType, Void, NoneTyp, |
5 | | - Instance, TypeVar, Callable, TupleType, UnionType, Overloaded, ErasedType, TypeList |
| 5 | + Instance, TypeVar, Callable, TupleType, UnionType, Overloaded, ErasedType, TypeList, |
| 6 | + is_named_instance |
6 | 7 | ) |
7 | 8 | from mypy import sametypes |
8 | 9 | from mypy.nodes import TypeInfo |
9 | 10 | from mypy.expandtype import expand_type |
| 11 | +from mypy.maptype import map_instance_to_supertype |
10 | 12 |
|
11 | 13 |
|
12 | 14 | def is_immutable(t: Instance) -> bool: |
@@ -190,112 +192,6 @@ def is_callable_subtype(left: Callable, right: Callable) -> bool: |
190 | 192 | return True |
191 | 193 |
|
192 | 194 |
|
193 | | -def map_instance_to_supertype(instance: Instance, |
194 | | - supertype: TypeInfo) -> Instance: |
195 | | - """Map an Instance type, including the type arguments, to compatible |
196 | | - Instance of a specific supertype. |
197 | | -
|
198 | | - Assume that supertype is a supertype of instance.type. |
199 | | - """ |
200 | | - if instance.type == supertype: |
201 | | - return instance |
202 | | - |
203 | | - # Strip type variables away if the supertype has none. |
204 | | - if not supertype.type_vars: |
205 | | - return Instance(supertype, []) |
206 | | - |
207 | | - return map_instance_to_supertypes(instance, supertype)[0] |
208 | | - |
209 | | - |
210 | | -def map_instance_to_direct_supertype(instance: Instance, |
211 | | - supertype: TypeInfo) -> Instance: |
212 | | - typ = instance.type |
213 | | - |
214 | | - for base in typ.bases: |
215 | | - if base.type == supertype: |
216 | | - map = type_var_map(typ, instance.args) |
217 | | - return cast(Instance, expand_type(base, map)) |
218 | | - |
219 | | - # Relationship with the supertype not specified explicitly. Use AnyType |
220 | | - # type arguments implicitly. |
221 | | - # TODO Should this be an error instead? |
222 | | - return Instance(supertype, [AnyType()] * len(supertype.type_vars)) |
223 | | - |
224 | | - |
225 | | -def type_var_map(typ: TypeInfo, args: List[Type]) -> Dict[int, Type]: |
226 | | - if not args: |
227 | | - return None |
228 | | - else: |
229 | | - tvars = {} # type: Dict[int, Type] |
230 | | - for i in range(len(args)): |
231 | | - tvars[i + 1] = args[i] |
232 | | - return tvars |
233 | | - |
234 | | - |
235 | | -def map_instance_to_supertypes(instance: Instance, |
236 | | - supertype: TypeInfo) -> List[Instance]: |
237 | | - # FIX: Currently we should only have one supertype per interface, so no |
238 | | - # need to return an array |
239 | | - result = [] # type: List[Instance] |
240 | | - for path in class_derivation_paths(instance.type, supertype): |
241 | | - types = [instance] |
242 | | - for sup in path: |
243 | | - a = [] # type: List[Instance] |
244 | | - for t in types: |
245 | | - a.extend(map_instance_to_direct_supertypes(t, sup)) |
246 | | - types = a |
247 | | - result.extend(types) |
248 | | - return result |
249 | | - |
250 | | - |
251 | | -def class_derivation_paths(typ: TypeInfo, |
252 | | - supertype: TypeInfo) -> List[List[TypeInfo]]: |
253 | | - """Return an array of non-empty paths of direct base classes from |
254 | | - type to supertype. Return [] if no such path could be found. |
255 | | -
|
256 | | - InterfaceImplementationPaths(A, B) == [[B]] if A inherits B |
257 | | - InterfaceImplementationPaths(A, C) == [[B, C]] if A inherits B and |
258 | | - B inherits C |
259 | | - """ |
260 | | - # FIX: Currently we might only ever have a single path, so this could be |
261 | | - # simplified |
262 | | - result = [] # type: List[List[TypeInfo]] |
263 | | - |
264 | | - for base in typ.bases: |
265 | | - if base.type == supertype: |
266 | | - result.append([base.type]) |
267 | | - else: |
268 | | - # Try constructing a longer path via the base class. |
269 | | - for path in class_derivation_paths(base.type, supertype): |
270 | | - result.append([base.type] + path) |
271 | | - |
272 | | - return result |
273 | | - |
274 | | - |
275 | | -def map_instance_to_direct_supertypes(instance: Instance, |
276 | | - supertype: TypeInfo) -> List[Instance]: |
277 | | - # FIX: There should only be one supertypes, always. |
278 | | - typ = instance.type |
279 | | - result = [] # type: List[Instance] |
280 | | - |
281 | | - for b in typ.bases: |
282 | | - if b.type == supertype: |
283 | | - map = type_var_map(typ, instance.args) |
284 | | - result.append(cast(Instance, expand_type(b, map))) |
285 | | - |
286 | | - if result: |
287 | | - return result |
288 | | - else: |
289 | | - # Relationship with the supertype not specified explicitly. Use dynamic |
290 | | - # type arguments implicitly. |
291 | | - return [Instance(supertype, [AnyType()] * len(supertype.type_vars))] |
292 | | - |
293 | | - |
294 | | -def is_named_instance(t: Type, fullname: str) -> bool: |
295 | | - return (isinstance(t, Instance) and |
296 | | - cast(Instance, t).type.fullname() == fullname) |
297 | | - |
298 | | - |
299 | 195 | def restrict_subtype_away(t: Type, s: Type) -> Type: |
300 | 196 | """Return a supertype of (t intersect not s) |
301 | 197 |
|
|
0 commit comments