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

Method visit_overloaded

mypy/join.py:417–468  ·  view source on GitHub ↗
(self, t: Overloaded)

Source from the content-addressed store, hash-verified

415 return join_types(t.fallback, self.s)
416
417 def visit_overloaded(self, t: Overloaded) -> ProperType:
418 # This is more complex than most other cases. Here are some
419 # examples that illustrate how this works.
420 #
421 # First let's define a concise notation:
422 # - Cn are callable types (for n in 1, 2, ...)
423 # - Ov(C1, C2, ...) is an overloaded type with items C1, C2, ...
424 # - Callable[[T, ...], S] is written as [T, ...] -> S.
425 #
426 # We want some basic properties to hold (assume Cn are all
427 # unrelated via Any-similarity):
428 #
429 # join(Ov(C1, C2), C1) == C1
430 # join(Ov(C1, C2), Ov(C1, C2)) == Ov(C1, C2)
431 # join(Ov(C1, C2), Ov(C1, C3)) == C1
432 # join(Ov(C2, C2), C3) == join of fallback types
433 #
434 # The presence of Any types makes things more interesting. The join is the
435 # most general type we can get with respect to Any:
436 #
437 # join(Ov([int] -> int, [str] -> str), [Any] -> str) == Any -> str
438 #
439 # We could use a simplification step that removes redundancies, but that's not
440 # implemented right now. Consider this example, where we get a redundancy:
441 #
442 # join(Ov([int, Any] -> Any, [str, Any] -> Any), [Any, int] -> Any) ==
443 # Ov([Any, int] -> Any, [Any, int] -> Any)
444 #
445 # TODO: Consider more cases of callable subtyping.
446 result: list[CallableType] = []
447 s = self.s
448 if isinstance(s, FunctionLike):
449 # The interesting case where both types are function types.
450 for t_item in t.items:
451 for s_item in s.items:
452 if is_similar_callables(t_item, s_item):
453 if is_equivalent(t_item, s_item):
454 result.append(combine_similar_callables(t_item, s_item))
455 elif is_subtype(t_item, s_item):
456 result.append(s_item)
457 if result:
458 # TODO: Simplify redundancies from the result.
459 if len(result) == 1:
460 return result[0]
461 else:
462 return Overloaded(result)
463 return join_types(t.fallback, s.fallback)
464 elif isinstance(s, Instance) and s.type.is_protocol:
465 call = unpack_callback_protocol(s)
466 if call:
467 return join_types(t, call)
468 return join_types(t.fallback, s)
469
470 def join_tuples(self, s: TupleType, t: TupleType) -> list[Type] | None:
471 """Join two tuple types while handling variadic entries.

Callers

nothing calls this directly

Calls 10

is_equivalentFunction · 0.90
is_subtypeFunction · 0.90
OverloadedClass · 0.90
isinstanceFunction · 0.85
is_similar_callablesFunction · 0.85
lenFunction · 0.85
join_typesFunction · 0.85
unpack_callback_protocolFunction · 0.85
appendMethod · 0.80

Tested by

no test coverage detected