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

Method union_overload_result

mypy/checkexpr.py:3116–3216  ·  view source on GitHub ↗

Accepts a list of overload signatures and attempts to match calls by destructuring the first union. Return a list of ( , ) if call succeeds for every item of the desctructured union. Returns None if there is no match.

(
        self,
        plausible_targets: list[CallableType],
        args: list[Expression],
        arg_types: list[Type],
        arg_kinds: list[ArgKind],
        arg_names: Sequence[str | None] | None,
        callable_name: str | None,
        object_type: Type | None,
        none_type_var_overlap: bool,
        context: Context,
        level: int = 0,
    )

Source from the content-addressed store, hash-verified

3114 return False
3115
3116 def union_overload_result(
3117 self,
3118 plausible_targets: list[CallableType],
3119 args: list[Expression],
3120 arg_types: list[Type],
3121 arg_kinds: list[ArgKind],
3122 arg_names: Sequence[str | None] | None,
3123 callable_name: str | None,
3124 object_type: Type | None,
3125 none_type_var_overlap: bool,
3126 context: Context,
3127 level: int = 0,
3128 ) -> list[tuple[Type, Type]] | None:
3129 """Accepts a list of overload signatures and attempts to match calls by destructuring
3130 the first union.
3131
3132 Return a list of (<return type>, <inferred variant type>) if call succeeds for every
3133 item of the desctructured union. Returns None if there is no match.
3134 """
3135 # Step 1: If we are already too deep, then stop immediately. Otherwise mypy might
3136 # hang for long time because of a weird overload call. The caller will get
3137 # the exception and generate an appropriate note message, if needed.
3138 if level >= MAX_UNIONS:
3139 raise TooManyUnions
3140
3141 # Step 2: Find position of the first union in arguments. Return the normal inferred
3142 # type if no more unions left.
3143 for idx, typ in enumerate(arg_types):
3144 if self.real_union(typ):
3145 break
3146 else:
3147 # No unions in args, just fall back to normal inference
3148 with self.type_overrides_set(args, arg_types):
3149 res = self.infer_overload_return_type(
3150 plausible_targets,
3151 args,
3152 arg_types,
3153 arg_kinds,
3154 arg_names,
3155 callable_name,
3156 object_type,
3157 context,
3158 )
3159 if res is not None:
3160 return [res]
3161 return None
3162
3163 # Step 3: Try a direct match before splitting to avoid unnecessary union splits
3164 # and save performance.
3165 if not none_type_var_overlap:
3166 with self.type_overrides_set(args, arg_types):
3167 direct = self.infer_overload_return_type(
3168 plausible_targets,
3169 args,
3170 arg_types,
3171 arg_kinds,
3172 arg_names,
3173 callable_name,

Callers 1

check_overload_callMethod · 0.95

Calls 12

real_unionMethod · 0.95
type_overrides_setMethod · 0.95
get_proper_typeFunction · 0.90
enumerateFunction · 0.85
isinstanceFunction · 0.85
setClass · 0.85
relevant_itemsMethod · 0.80
extendMethod · 0.80
appendMethod · 0.80
copyMethod · 0.45
addMethod · 0.45

Tested by

no test coverage detected