Skip to content

Commit 67768f4

Browse files
committed
Updated Step 5 of overload algorithm based on PR feedback.
1 parent e835221 commit 67768f4

File tree

1 file changed

+21
-8
lines changed

1 file changed

+21
-8
lines changed

docs/spec/overload.rst

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -263,18 +263,31 @@ If so, eliminate overloads that do not have a variadic parameter.
263263
- If two or more candidate overloads remain, proceed to step 5.
264264

265265

266-
Step 5: For each argument, determine whether all possible
266+
Step 5: For all arguments, determine whether all possible
267267
:term:`materializations <materialize>` of the argument's type are assignable to
268268
the corresponding parameter type for each of the remaining overloads. If so,
269269
eliminate all of the subsequent remaining overloads.
270270

271-
For example, if the argument type is ``list[Any]`` and there are three remaining
272-
overloads with corresponding parameter types of ``list[int]``, ``list[Any]``
273-
and ``Any``. We can eliminate the third of the remaining overloads because
274-
all materializations of ``list[Any]`` are assignable to ``list[Any]``, the
275-
parameter in the second overload. We cannot eliminate the second overload
276-
because there are possible materializations of ``list[Any]`` (for example,
277-
``list[str]``) that are not assignable to ``list[int]``.
271+
Consider the following example::
272+
273+
@overload
274+
def example(x: list[int]) -> int: ...
275+
@overload
276+
def example(x: list[Any]) -> str: ...
277+
@overload
278+
def example(x: Any) -> Any: ...
279+
280+
def test(a: list[Any]):
281+
# All materializations of list[Any] will match either the first or
282+
# second overload, so the third overload can be eliminated.
283+
example(a)
284+
285+
This rule eliminates overloads that will never be chosen even if the
286+
caller eliminates types that include ``Any``.
287+
288+
If the call involves more than one argument, all possible materializations of
289+
every argument type must be assignable to its corresponding parameter type.
290+
If this condition exists, all subsequent remaining overloads should be eliminated.
278291

279292
Once this filtering process is applied for all arguments, examine the return
280293
types of the remaining overloads. If these return types include type variables,

0 commit comments

Comments
 (0)