Debian Codegraph @

mypy/maptype.py

raw
1 from __future__ import annotations
2
3 from mypy.expandtype import expand_type_by_instance
4 from mypy.nodes import TypeInfo
5 from mypy.types import AnyType, Instance, TupleType, TypeOfAny, has_type_vars
6
7
8 def map_instance_to_supertype(instance: Instance, superclass: TypeInfo) -> Instance:
9 """Produce a supertype of `instance` that is an Instance
10 of `superclass`, mapping type arguments up the chain of bases.
11
12 If `superclass` is not a nominal superclass of `instance.type`,
13 then all type arguments are mapped to 'Any'.
14 """
15 if instance.type == superclass:
16 # Fast path: `instance` already belongs to `superclass`.
17 return instance
18
19 if not superclass.type_vars:
20 # Fast path: `superclass` has no type variables to map to.
21 return Instance(superclass, [])
22
23 return map_instance_to_supertypes(instance, superclass)[0]
24
25
26 def map_instance_to_supertypes(instance: Instance, supertype: TypeInfo) -> list[Instance]:
27 # FIX: Currently we should only have one supertype per interface, so no
28 # need to return an array
29 result: list[Instance] = []
30 for path in class_derivation_paths(instance.type, supertype):
31 types = [instance]
32 for sup in path:
33 a: list[Instance] = []
34 for t in types:
35 a.extend(map_instance_to_direct_supertypes(t, sup))
36 types = a
37 result.extend(types)
38 if result:
39 return result
40 else:
41 # Nothing. Presumably due to an error. Construct a dummy using Any.
42 any_type = AnyType(TypeOfAny.from_error)
43 return [Instance(supertype, [any_type] * len(supertype.type_vars))]
44
45
46 def class_derivation_paths(typ: TypeInfo, supertype: TypeInfo) -> list[list[TypeInfo]]:
47 """Return an array of non-empty paths of direct base classes from
48 type to supertype. Return [] if no such path could be found.
49
50 InterfaceImplementationPaths(A, B) == [[B]] if A inherits B
51 InterfaceImplementationPaths(A, C) == [[B, C]] if A inherits B and
52 B inherits C
53 """
54 # FIX: Currently we might only ever have a single path, so this could be
55 # simplified
56 result: list[list[TypeInfo]] = []
57
58 for base in typ.bases:
59 btype = base.type
60 if btype == supertype:
61 result.append([btype])
62 else:
63 # Try constructing a longer path via the base class.
64 for path in class_derivation_paths(btype, supertype):
65 result.append([btype] + path)
66
67 return result
68
69
70 def map_instance_to_direct_supertypes(instance: Instance, supertype: TypeInfo) -> list[Instance]:
71 # FIX: There should only be one supertypes, always.
72 typ = instance.type
73 result: list[Instance] = []
74
75 for b in typ.bases:
76 if b.type == supertype:
77
78 if supertype.fullname == "builtins.tuple" and instance.type.tuple_type:
79 if has_type_vars(instance.type.tuple_type):
80 # We special case mapping generic tuple types to tuple base, because for
81 # such tuples fallback can't be calculated before applying type arguments.
82 alias = instance.type.special_alias
83 assert alias is not None
84 if not alias._is_recursive:
85 # Unfortunately we can't support this for generic recursive tuples.
86 # If we skip this special casing we will fall back to tuple[Any, ...].
87 tuple_type = expand_type_by_instance(instance.type.tuple_type, instance)
88 if isinstance(tuple_type, TupleType):
89 # Make the import here to avoid cyclic imports.
90 import mypy.typeops
91
92 result.append(mypy.typeops.tuple_fallback(tuple_type))
93 continue
94 elif isinstance(tuple_type, Instance):
95 # This can happen after normalizing variadic tuples.
96 result.append(tuple_type)
97 continue
98
99 t = expand_type_by_instance(b, instance)
100 assert isinstance(t, Instance)
101 result.append(t)
102
103 if result:
104 return result
105 else:
106 # Relationship with the supertype not specified explicitly. Use dynamic
107 # type arguments implicitly.
108 any_type = AnyType(TypeOfAny.unannotated)
109 return [Instance(supertype, [any_type] * len(supertype.type_vars))]