join.py 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691
  1. """Calculation of the least upper bound types (joins)."""
  2. from __future__ import annotations
  3. from typing import overload
  4. import mypy.typeops
  5. from mypy.maptype import map_instance_to_supertype
  6. from mypy.nodes import CONTRAVARIANT, COVARIANT, INVARIANT
  7. from mypy.state import state
  8. from mypy.subtypes import (
  9. SubtypeContext,
  10. find_member,
  11. is_equivalent,
  12. is_proper_subtype,
  13. is_protocol_implementation,
  14. is_subtype,
  15. )
  16. from mypy.types import (
  17. AnyType,
  18. CallableType,
  19. DeletedType,
  20. ErasedType,
  21. FunctionLike,
  22. Instance,
  23. LiteralType,
  24. NoneType,
  25. Overloaded,
  26. Parameters,
  27. ParamSpecType,
  28. PartialType,
  29. PlaceholderType,
  30. ProperType,
  31. TupleType,
  32. Type,
  33. TypeAliasType,
  34. TypedDictType,
  35. TypeOfAny,
  36. TypeType,
  37. TypeVarTupleType,
  38. TypeVarType,
  39. TypeVisitor,
  40. UnboundType,
  41. UninhabitedType,
  42. UnionType,
  43. UnpackType,
  44. get_proper_type,
  45. get_proper_types,
  46. )
  47. class InstanceJoiner:
  48. def __init__(self) -> None:
  49. self.seen_instances: list[tuple[Instance, Instance]] = []
  50. def join_instances(self, t: Instance, s: Instance) -> ProperType:
  51. if (t, s) in self.seen_instances or (s, t) in self.seen_instances:
  52. return object_from_instance(t)
  53. self.seen_instances.append((t, s))
  54. # Calculate the join of two instance types
  55. if t.type == s.type:
  56. # Simplest case: join two types with the same base type (but
  57. # potentially different arguments).
  58. # Combine type arguments.
  59. args: list[Type] = []
  60. # N.B: We use zip instead of indexing because the lengths might have
  61. # mismatches during daemon reprocessing.
  62. for ta, sa, type_var in zip(t.args, s.args, t.type.defn.type_vars):
  63. ta_proper = get_proper_type(ta)
  64. sa_proper = get_proper_type(sa)
  65. new_type: Type | None = None
  66. if isinstance(ta_proper, AnyType):
  67. new_type = AnyType(TypeOfAny.from_another_any, ta_proper)
  68. elif isinstance(sa_proper, AnyType):
  69. new_type = AnyType(TypeOfAny.from_another_any, sa_proper)
  70. elif isinstance(type_var, TypeVarType):
  71. if type_var.variance == COVARIANT:
  72. new_type = join_types(ta, sa, self)
  73. if len(type_var.values) != 0 and new_type not in type_var.values:
  74. self.seen_instances.pop()
  75. return object_from_instance(t)
  76. if not is_subtype(new_type, type_var.upper_bound):
  77. self.seen_instances.pop()
  78. return object_from_instance(t)
  79. # TODO: contravariant case should use meet but pass seen instances as
  80. # an argument to keep track of recursive checks.
  81. elif type_var.variance in (INVARIANT, CONTRAVARIANT):
  82. if not is_equivalent(ta, sa):
  83. self.seen_instances.pop()
  84. return object_from_instance(t)
  85. # If the types are different but equivalent, then an Any is involved
  86. # so using a join in the contravariant case is also OK.
  87. new_type = join_types(ta, sa, self)
  88. else:
  89. # ParamSpec type variables behave the same, independent of variance
  90. if not is_equivalent(ta, sa):
  91. return get_proper_type(type_var.upper_bound)
  92. new_type = join_types(ta, sa, self)
  93. assert new_type is not None
  94. args.append(new_type)
  95. result: ProperType = Instance(t.type, args)
  96. elif t.type.bases and is_proper_subtype(
  97. t, s, subtype_context=SubtypeContext(ignore_type_params=True)
  98. ):
  99. result = self.join_instances_via_supertype(t, s)
  100. else:
  101. # Now t is not a subtype of s, and t != s. Now s could be a subtype
  102. # of t; alternatively, we need to find a common supertype. This works
  103. # in of the both cases.
  104. result = self.join_instances_via_supertype(s, t)
  105. self.seen_instances.pop()
  106. return result
  107. def join_instances_via_supertype(self, t: Instance, s: Instance) -> ProperType:
  108. # Give preference to joins via duck typing relationship, so that
  109. # join(int, float) == float, for example.
  110. for p in t.type._promote:
  111. if is_subtype(p, s):
  112. return join_types(p, s, self)
  113. for p in s.type._promote:
  114. if is_subtype(p, t):
  115. return join_types(t, p, self)
  116. # Compute the "best" supertype of t when joined with s.
  117. # The definition of "best" may evolve; for now it is the one with
  118. # the longest MRO. Ties are broken by using the earlier base.
  119. best: ProperType | None = None
  120. for base in t.type.bases:
  121. mapped = map_instance_to_supertype(t, base.type)
  122. res = self.join_instances(mapped, s)
  123. if best is None or is_better(res, best):
  124. best = res
  125. assert best is not None
  126. for promote in t.type._promote:
  127. if isinstance(promote, Instance):
  128. res = self.join_instances(promote, s)
  129. if is_better(res, best):
  130. best = res
  131. return best
  132. def join_simple(declaration: Type | None, s: Type, t: Type) -> ProperType:
  133. """Return a simple least upper bound given the declared type.
  134. This function should be only used by binder, and should not recurse.
  135. For all other uses, use `join_types()`.
  136. """
  137. declaration = get_proper_type(declaration)
  138. s = get_proper_type(s)
  139. t = get_proper_type(t)
  140. if (s.can_be_true, s.can_be_false) != (t.can_be_true, t.can_be_false):
  141. # if types are restricted in different ways, use the more general versions
  142. s = mypy.typeops.true_or_false(s)
  143. t = mypy.typeops.true_or_false(t)
  144. if isinstance(s, AnyType):
  145. return s
  146. if isinstance(s, ErasedType):
  147. return t
  148. if is_proper_subtype(s, t, ignore_promotions=True):
  149. return t
  150. if is_proper_subtype(t, s, ignore_promotions=True):
  151. return s
  152. if isinstance(declaration, UnionType):
  153. return mypy.typeops.make_simplified_union([s, t])
  154. if isinstance(s, NoneType) and not isinstance(t, NoneType):
  155. s, t = t, s
  156. if isinstance(s, UninhabitedType) and not isinstance(t, UninhabitedType):
  157. s, t = t, s
  158. # Meets/joins require callable type normalization.
  159. s, t = normalize_callables(s, t)
  160. if isinstance(s, UnionType) and not isinstance(t, UnionType):
  161. s, t = t, s
  162. value = t.accept(TypeJoinVisitor(s))
  163. if declaration is None or is_subtype(value, declaration):
  164. return value
  165. return declaration
  166. def trivial_join(s: Type, t: Type) -> Type:
  167. """Return one of types (expanded) if it is a supertype of other, otherwise top type."""
  168. if is_subtype(s, t):
  169. return t
  170. elif is_subtype(t, s):
  171. return s
  172. else:
  173. return object_or_any_from_type(get_proper_type(t))
  174. @overload
  175. def join_types(
  176. s: ProperType, t: ProperType, instance_joiner: InstanceJoiner | None = None
  177. ) -> ProperType:
  178. ...
  179. @overload
  180. def join_types(s: Type, t: Type, instance_joiner: InstanceJoiner | None = None) -> Type:
  181. ...
  182. def join_types(s: Type, t: Type, instance_joiner: InstanceJoiner | None = None) -> Type:
  183. """Return the least upper bound of s and t.
  184. For example, the join of 'int' and 'object' is 'object'.
  185. """
  186. if mypy.typeops.is_recursive_pair(s, t):
  187. # This case can trigger an infinite recursion, general support for this will be
  188. # tricky so we use a trivial join (like for protocols).
  189. return trivial_join(s, t)
  190. s = get_proper_type(s)
  191. t = get_proper_type(t)
  192. if (s.can_be_true, s.can_be_false) != (t.can_be_true, t.can_be_false):
  193. # if types are restricted in different ways, use the more general versions
  194. s = mypy.typeops.true_or_false(s)
  195. t = mypy.typeops.true_or_false(t)
  196. if isinstance(s, UnionType) and not isinstance(t, UnionType):
  197. s, t = t, s
  198. if isinstance(s, AnyType):
  199. return s
  200. if isinstance(s, ErasedType):
  201. return t
  202. if isinstance(s, NoneType) and not isinstance(t, NoneType):
  203. s, t = t, s
  204. if isinstance(s, UninhabitedType) and not isinstance(t, UninhabitedType):
  205. s, t = t, s
  206. # We shouldn't run into PlaceholderTypes here, but in practice we can encounter them
  207. # here in the presence of undefined names
  208. if isinstance(t, PlaceholderType) and not isinstance(s, PlaceholderType):
  209. # mypyc does not allow switching the values like above.
  210. return s.accept(TypeJoinVisitor(t))
  211. elif isinstance(t, PlaceholderType):
  212. return AnyType(TypeOfAny.from_error)
  213. # Meets/joins require callable type normalization.
  214. s, t = normalize_callables(s, t)
  215. # Use a visitor to handle non-trivial cases.
  216. return t.accept(TypeJoinVisitor(s, instance_joiner))
  217. class TypeJoinVisitor(TypeVisitor[ProperType]):
  218. """Implementation of the least upper bound algorithm.
  219. Attributes:
  220. s: The other (left) type operand.
  221. """
  222. def __init__(self, s: ProperType, instance_joiner: InstanceJoiner | None = None) -> None:
  223. self.s = s
  224. self.instance_joiner = instance_joiner
  225. def visit_unbound_type(self, t: UnboundType) -> ProperType:
  226. return AnyType(TypeOfAny.special_form)
  227. def visit_union_type(self, t: UnionType) -> ProperType:
  228. if is_proper_subtype(self.s, t):
  229. return t
  230. else:
  231. return mypy.typeops.make_simplified_union([self.s, t])
  232. def visit_any(self, t: AnyType) -> ProperType:
  233. return t
  234. def visit_none_type(self, t: NoneType) -> ProperType:
  235. if state.strict_optional:
  236. if isinstance(self.s, (NoneType, UninhabitedType)):
  237. return t
  238. elif isinstance(self.s, UnboundType):
  239. return AnyType(TypeOfAny.special_form)
  240. else:
  241. return mypy.typeops.make_simplified_union([self.s, t])
  242. else:
  243. return self.s
  244. def visit_uninhabited_type(self, t: UninhabitedType) -> ProperType:
  245. return self.s
  246. def visit_deleted_type(self, t: DeletedType) -> ProperType:
  247. return self.s
  248. def visit_erased_type(self, t: ErasedType) -> ProperType:
  249. return self.s
  250. def visit_type_var(self, t: TypeVarType) -> ProperType:
  251. if isinstance(self.s, TypeVarType) and self.s.id == t.id:
  252. return self.s
  253. else:
  254. return self.default(self.s)
  255. def visit_param_spec(self, t: ParamSpecType) -> ProperType:
  256. if self.s == t:
  257. return t
  258. return self.default(self.s)
  259. def visit_type_var_tuple(self, t: TypeVarTupleType) -> ProperType:
  260. if self.s == t:
  261. return t
  262. return self.default(self.s)
  263. def visit_unpack_type(self, t: UnpackType) -> UnpackType:
  264. raise NotImplementedError
  265. def visit_parameters(self, t: Parameters) -> ProperType:
  266. if self.s == t:
  267. return t
  268. else:
  269. return self.default(self.s)
  270. def visit_instance(self, t: Instance) -> ProperType:
  271. if isinstance(self.s, Instance):
  272. if self.instance_joiner is None:
  273. self.instance_joiner = InstanceJoiner()
  274. nominal = self.instance_joiner.join_instances(t, self.s)
  275. structural: Instance | None = None
  276. if t.type.is_protocol and is_protocol_implementation(self.s, t):
  277. structural = t
  278. elif self.s.type.is_protocol and is_protocol_implementation(t, self.s):
  279. structural = self.s
  280. # Structural join is preferred in the case where we have found both
  281. # structural and nominal and they have same MRO length (see two comments
  282. # in join_instances_via_supertype). Otherwise, just return the nominal join.
  283. if not structural or is_better(nominal, structural):
  284. return nominal
  285. return structural
  286. elif isinstance(self.s, FunctionLike):
  287. if t.type.is_protocol:
  288. call = unpack_callback_protocol(t)
  289. if call:
  290. return join_types(call, self.s)
  291. return join_types(t, self.s.fallback)
  292. elif isinstance(self.s, TypeType):
  293. return join_types(t, self.s)
  294. elif isinstance(self.s, TypedDictType):
  295. return join_types(t, self.s)
  296. elif isinstance(self.s, TupleType):
  297. return join_types(t, self.s)
  298. elif isinstance(self.s, LiteralType):
  299. return join_types(t, self.s)
  300. else:
  301. return self.default(self.s)
  302. def visit_callable_type(self, t: CallableType) -> ProperType:
  303. if isinstance(self.s, CallableType) and is_similar_callables(t, self.s):
  304. if is_equivalent(t, self.s):
  305. return combine_similar_callables(t, self.s)
  306. result = join_similar_callables(t, self.s)
  307. # We set the from_type_type flag to suppress error when a collection of
  308. # concrete class objects gets inferred as their common abstract superclass.
  309. if not (
  310. (t.is_type_obj() and t.type_object().is_abstract)
  311. or (self.s.is_type_obj() and self.s.type_object().is_abstract)
  312. ):
  313. result.from_type_type = True
  314. if any(
  315. isinstance(tp, (NoneType, UninhabitedType))
  316. for tp in get_proper_types(result.arg_types)
  317. ):
  318. # We don't want to return unusable Callable, attempt fallback instead.
  319. return join_types(t.fallback, self.s)
  320. return result
  321. elif isinstance(self.s, Overloaded):
  322. # Switch the order of arguments to that we'll get to visit_overloaded.
  323. return join_types(t, self.s)
  324. elif isinstance(self.s, Instance) and self.s.type.is_protocol:
  325. call = unpack_callback_protocol(self.s)
  326. if call:
  327. return join_types(t, call)
  328. return join_types(t.fallback, self.s)
  329. def visit_overloaded(self, t: Overloaded) -> ProperType:
  330. # This is more complex than most other cases. Here are some
  331. # examples that illustrate how this works.
  332. #
  333. # First let's define a concise notation:
  334. # - Cn are callable types (for n in 1, 2, ...)
  335. # - Ov(C1, C2, ...) is an overloaded type with items C1, C2, ...
  336. # - Callable[[T, ...], S] is written as [T, ...] -> S.
  337. #
  338. # We want some basic properties to hold (assume Cn are all
  339. # unrelated via Any-similarity):
  340. #
  341. # join(Ov(C1, C2), C1) == C1
  342. # join(Ov(C1, C2), Ov(C1, C2)) == Ov(C1, C2)
  343. # join(Ov(C1, C2), Ov(C1, C3)) == C1
  344. # join(Ov(C2, C2), C3) == join of fallback types
  345. #
  346. # The presence of Any types makes things more interesting. The join is the
  347. # most general type we can get with respect to Any:
  348. #
  349. # join(Ov([int] -> int, [str] -> str), [Any] -> str) == Any -> str
  350. #
  351. # We could use a simplification step that removes redundancies, but that's not
  352. # implemented right now. Consider this example, where we get a redundancy:
  353. #
  354. # join(Ov([int, Any] -> Any, [str, Any] -> Any), [Any, int] -> Any) ==
  355. # Ov([Any, int] -> Any, [Any, int] -> Any)
  356. #
  357. # TODO: Consider more cases of callable subtyping.
  358. result: list[CallableType] = []
  359. s = self.s
  360. if isinstance(s, FunctionLike):
  361. # The interesting case where both types are function types.
  362. for t_item in t.items:
  363. for s_item in s.items:
  364. if is_similar_callables(t_item, s_item):
  365. if is_equivalent(t_item, s_item):
  366. result.append(combine_similar_callables(t_item, s_item))
  367. elif is_subtype(t_item, s_item):
  368. result.append(s_item)
  369. if result:
  370. # TODO: Simplify redundancies from the result.
  371. if len(result) == 1:
  372. return result[0]
  373. else:
  374. return Overloaded(result)
  375. return join_types(t.fallback, s.fallback)
  376. elif isinstance(s, Instance) and s.type.is_protocol:
  377. call = unpack_callback_protocol(s)
  378. if call:
  379. return join_types(t, call)
  380. return join_types(t.fallback, s)
  381. def visit_tuple_type(self, t: TupleType) -> ProperType:
  382. # When given two fixed-length tuples:
  383. # * If they have the same length, join their subtypes item-wise:
  384. # Tuple[int, bool] + Tuple[bool, bool] becomes Tuple[int, bool]
  385. # * If lengths do not match, return a variadic tuple:
  386. # Tuple[bool, int] + Tuple[bool] becomes Tuple[int, ...]
  387. #
  388. # Otherwise, `t` is a fixed-length tuple but `self.s` is NOT:
  389. # * Joining with a variadic tuple returns variadic tuple:
  390. # Tuple[int, bool] + Tuple[bool, ...] becomes Tuple[int, ...]
  391. # * Joining with any Sequence also returns a Sequence:
  392. # Tuple[int, bool] + List[bool] becomes Sequence[int]
  393. if isinstance(self.s, TupleType) and self.s.length() == t.length():
  394. if self.instance_joiner is None:
  395. self.instance_joiner = InstanceJoiner()
  396. fallback = self.instance_joiner.join_instances(
  397. mypy.typeops.tuple_fallback(self.s), mypy.typeops.tuple_fallback(t)
  398. )
  399. assert isinstance(fallback, Instance)
  400. if self.s.length() == t.length():
  401. items: list[Type] = []
  402. for i in range(t.length()):
  403. items.append(join_types(t.items[i], self.s.items[i]))
  404. return TupleType(items, fallback)
  405. else:
  406. return fallback
  407. else:
  408. return join_types(self.s, mypy.typeops.tuple_fallback(t))
  409. def visit_typeddict_type(self, t: TypedDictType) -> ProperType:
  410. if isinstance(self.s, TypedDictType):
  411. items = {
  412. item_name: s_item_type
  413. for (item_name, s_item_type, t_item_type) in self.s.zip(t)
  414. if (
  415. is_equivalent(s_item_type, t_item_type)
  416. and (item_name in t.required_keys) == (item_name in self.s.required_keys)
  417. )
  418. }
  419. fallback = self.s.create_anonymous_fallback()
  420. # We need to filter by items.keys() since some required keys present in both t and
  421. # self.s might be missing from the join if the types are incompatible.
  422. required_keys = set(items.keys()) & t.required_keys & self.s.required_keys
  423. return TypedDictType(items, required_keys, fallback)
  424. elif isinstance(self.s, Instance):
  425. return join_types(self.s, t.fallback)
  426. else:
  427. return self.default(self.s)
  428. def visit_literal_type(self, t: LiteralType) -> ProperType:
  429. if isinstance(self.s, LiteralType):
  430. if t == self.s:
  431. return t
  432. if self.s.fallback.type.is_enum and t.fallback.type.is_enum:
  433. return mypy.typeops.make_simplified_union([self.s, t])
  434. return join_types(self.s.fallback, t.fallback)
  435. else:
  436. return join_types(self.s, t.fallback)
  437. def visit_partial_type(self, t: PartialType) -> ProperType:
  438. # We only have partial information so we can't decide the join result. We should
  439. # never get here.
  440. assert False, "Internal error"
  441. def visit_type_type(self, t: TypeType) -> ProperType:
  442. if isinstance(self.s, TypeType):
  443. return TypeType.make_normalized(join_types(t.item, self.s.item), line=t.line)
  444. elif isinstance(self.s, Instance) and self.s.type.fullname == "builtins.type":
  445. return self.s
  446. else:
  447. return self.default(self.s)
  448. def visit_type_alias_type(self, t: TypeAliasType) -> ProperType:
  449. assert False, f"This should be never called, got {t}"
  450. def default(self, typ: Type) -> ProperType:
  451. typ = get_proper_type(typ)
  452. if isinstance(typ, Instance):
  453. return object_from_instance(typ)
  454. elif isinstance(typ, UnboundType):
  455. return AnyType(TypeOfAny.special_form)
  456. elif isinstance(typ, TupleType):
  457. return self.default(mypy.typeops.tuple_fallback(typ))
  458. elif isinstance(typ, TypedDictType):
  459. return self.default(typ.fallback)
  460. elif isinstance(typ, FunctionLike):
  461. return self.default(typ.fallback)
  462. elif isinstance(typ, TypeVarType):
  463. return self.default(typ.upper_bound)
  464. elif isinstance(typ, ParamSpecType):
  465. return self.default(typ.upper_bound)
  466. else:
  467. return AnyType(TypeOfAny.special_form)
  468. def is_better(t: Type, s: Type) -> bool:
  469. # Given two possible results from join_instances_via_supertype(),
  470. # indicate whether t is the better one.
  471. t = get_proper_type(t)
  472. s = get_proper_type(s)
  473. if isinstance(t, Instance):
  474. if not isinstance(s, Instance):
  475. return True
  476. # Use len(mro) as a proxy for the better choice.
  477. if len(t.type.mro) > len(s.type.mro):
  478. return True
  479. return False
  480. def normalize_callables(s: ProperType, t: ProperType) -> tuple[ProperType, ProperType]:
  481. if isinstance(s, (CallableType, Overloaded)):
  482. s = s.with_unpacked_kwargs()
  483. if isinstance(t, (CallableType, Overloaded)):
  484. t = t.with_unpacked_kwargs()
  485. return s, t
  486. def is_similar_callables(t: CallableType, s: CallableType) -> bool:
  487. """Return True if t and s have identical numbers of
  488. arguments, default arguments and varargs.
  489. """
  490. return (
  491. len(t.arg_types) == len(s.arg_types)
  492. and t.min_args == s.min_args
  493. and t.is_var_arg == s.is_var_arg
  494. )
  495. def join_similar_callables(t: CallableType, s: CallableType) -> CallableType:
  496. from mypy.meet import meet_types
  497. arg_types: list[Type] = []
  498. for i in range(len(t.arg_types)):
  499. arg_types.append(meet_types(t.arg_types[i], s.arg_types[i]))
  500. # TODO in combine_similar_callables also applies here (names and kinds; user metaclasses)
  501. # The fallback type can be either 'function', 'type', or some user-provided metaclass.
  502. # The result should always use 'function' as a fallback if either operands are using it.
  503. if t.fallback.type.fullname == "builtins.function":
  504. fallback = t.fallback
  505. else:
  506. fallback = s.fallback
  507. return t.copy_modified(
  508. arg_types=arg_types,
  509. arg_names=combine_arg_names(t, s),
  510. ret_type=join_types(t.ret_type, s.ret_type),
  511. fallback=fallback,
  512. name=None,
  513. )
  514. def combine_similar_callables(t: CallableType, s: CallableType) -> CallableType:
  515. arg_types: list[Type] = []
  516. for i in range(len(t.arg_types)):
  517. arg_types.append(join_types(t.arg_types[i], s.arg_types[i]))
  518. # TODO kinds and argument names
  519. # TODO what should happen if one fallback is 'type' and the other is a user-provided metaclass?
  520. # The fallback type can be either 'function', 'type', or some user-provided metaclass.
  521. # The result should always use 'function' as a fallback if either operands are using it.
  522. if t.fallback.type.fullname == "builtins.function":
  523. fallback = t.fallback
  524. else:
  525. fallback = s.fallback
  526. return t.copy_modified(
  527. arg_types=arg_types,
  528. arg_names=combine_arg_names(t, s),
  529. ret_type=join_types(t.ret_type, s.ret_type),
  530. fallback=fallback,
  531. name=None,
  532. )
  533. def combine_arg_names(t: CallableType, s: CallableType) -> list[str | None]:
  534. """Produces a list of argument names compatible with both callables.
  535. For example, suppose 't' and 's' have the following signatures:
  536. - t: (a: int, b: str, X: str) -> None
  537. - s: (a: int, b: str, Y: str) -> None
  538. This function would return ["a", "b", None]. This information
  539. is then used above to compute the join of t and s, which results
  540. in a signature of (a: int, b: str, str) -> None.
  541. Note that the third argument's name is omitted and 't' and 's'
  542. are both valid subtypes of this inferred signature.
  543. Precondition: is_similar_types(t, s) is true.
  544. """
  545. num_args = len(t.arg_types)
  546. new_names = []
  547. for i in range(num_args):
  548. t_name = t.arg_names[i]
  549. s_name = s.arg_names[i]
  550. if t_name == s_name or t.arg_kinds[i].is_named() or s.arg_kinds[i].is_named():
  551. new_names.append(t_name)
  552. else:
  553. new_names.append(None)
  554. return new_names
  555. def object_from_instance(instance: Instance) -> Instance:
  556. """Construct the type 'builtins.object' from an instance type."""
  557. # Use the fact that 'object' is always the last class in the mro.
  558. res = Instance(instance.type.mro[-1], [])
  559. return res
  560. def object_or_any_from_type(typ: ProperType) -> ProperType:
  561. # Similar to object_from_instance() but tries hard for all types.
  562. # TODO: find a better way to get object, or make this more reliable.
  563. if isinstance(typ, Instance):
  564. return object_from_instance(typ)
  565. elif isinstance(typ, (CallableType, TypedDictType, LiteralType)):
  566. return object_from_instance(typ.fallback)
  567. elif isinstance(typ, TupleType):
  568. return object_from_instance(typ.partial_fallback)
  569. elif isinstance(typ, TypeType):
  570. return object_or_any_from_type(typ.item)
  571. elif isinstance(typ, TypeVarType) and isinstance(typ.upper_bound, ProperType):
  572. return object_or_any_from_type(typ.upper_bound)
  573. elif isinstance(typ, UnionType):
  574. for item in typ.items:
  575. if isinstance(item, ProperType):
  576. candidate = object_or_any_from_type(item)
  577. if isinstance(candidate, Instance):
  578. return candidate
  579. return AnyType(TypeOfAny.implementation_artifact)
  580. def join_type_list(types: list[Type]) -> Type:
  581. if not types:
  582. # This is a little arbitrary but reasonable. Any empty tuple should be compatible
  583. # with all variable length tuples, and this makes it possible.
  584. return UninhabitedType()
  585. joined = types[0]
  586. for t in types[1:]:
  587. joined = join_types(joined, t)
  588. return joined
  589. def unpack_callback_protocol(t: Instance) -> ProperType | None:
  590. assert t.type.is_protocol
  591. if t.type.protocol_members == ["__call__"]:
  592. return get_proper_type(find_member("__call__", t, t, is_operator=True))
  593. return None