@@ -130,13 +130,19 @@ checkers should report an error or warning if an implementation is missing.
130
130
Overload definitions within stub files, protocols, and abstract base classes
131
131
are exempt from this check.
132
132
133
- If an overload is decorated with ``@staticmethod `` or ``@classmethod ``,
134
- all overloads must be similarly decorated. The implementation,
135
- if present, must be decorated in the same manner. Type checkers should report
136
- an error if these conditions are not met.
137
-
138
- If one or more overloads are decorated with ``@final `` or ``@override `` but the
139
- implementation is not, an error should be reported.
133
+ If one overload signature is decorated with ``@staticmethod `` or
134
+ ``@classmethod ``, all overload signatures must be similarly decorated. The
135
+ implementation, if present, must also have a consistent decorator. Type
136
+ checkers should report an error if these conditions are not met.
137
+
138
+ If a ``@final `` or ``@override `` decorator is supplied for a function with
139
+ overloads, the decorator should be applied only to the overload implementation
140
+ if it is present. If an overload implementation isn't present (for example, in
141
+ a stub file), the ``@final `` or ``@override `` decorator should be applied only
142
+ to the first overload. Type checkers should enforce these rules and generate
143
+ an error when they are violated. If a ``@final `` or ``@override `` decorator
144
+ follows these rules, a type checker should treat the decorator as if it is
145
+ present on all overloads.
140
146
141
147
Overloads are allowed to use a mixture of ``async def `` and ``def `` statements
142
148
within the same overload definition. Type checkers should convert
@@ -202,6 +208,21 @@ for conditions that are mandated by their usage in the runtime. For example,
202
208
the ``__get__ `` method of a descriptor is often defined using overloads
203
209
that would partially overlap if the above rule is enforced.
204
210
211
+ Type checkers may ignore the possibility of multiple inheritance or
212
+ intersections involving structural types for purposes of computing overlap.
213
+ In the following example, classes ``A `` and ``B `` could theoretically overlap
214
+ because there could be a common type ``C `` that derives from both ``A `` and
215
+ ``B ``, but type checkers may choose not to flag this as an overlapping
216
+ overload::
217
+
218
+ class A: ...
219
+ class B: ...
220
+
221
+ @overload
222
+ def func(x: A) -> int: ...
223
+ @overload
224
+ def func(x: B) -> str: ...
225
+
205
226
If all possible sets of arguments accepted by an overload are also always
206
227
accepted by an earlier overload, the two overloads are said to "fully overlap".
207
228
In this case, the latter overload will never be used. This condition
@@ -217,21 +238,14 @@ checkers::
217
238
def func(x: int) -> int: ...
218
239
219
240
220
- [Eric's note for reviewers: We've identified a number of subtle issues and
221
- cases where current type checkers do not honor the above rules, especially
222
- for partially-overlapping overloads. At this point, I'm tempted to delete
223
- this section entirely. We could always add it back to the spec later
224
- if and when we find that there's a need for it and we achieve consensus on
225
- the details.]
226
-
227
-
228
241
Overload call evaluation
229
242
^^^^^^^^^^^^^^^^^^^^^^^^
230
243
231
244
When a type checker evaluates the call of an overloaded function, it
232
245
attempts to "match" the supplied arguments with one or more overloads.
233
246
This section describes the algorithm that type checkers should use
234
- for overload matching.
247
+ for overload matching. This algorithm should be applied even in the
248
+ presence of :ref: <overlapping overloads>.
235
249
236
250
Only the overloads (the ``@overload ``-decorated signatures) should be
237
251
considered for matching purposes. The implementation, if provided,
@@ -310,10 +324,10 @@ eliminate all of the subsequent remaining overloads.
310
324
For example, if the argument type is ``list[Any] `` and there are three remaining
311
325
overloads with corresponding parameter types of ``list[int] ``, ``list[Any] ``
312
326
and ``Any ``. We can eliminate the third of the remaining overloads because
313
- all manifestations of ``list[Any] `` are assignable to ``list[Any] ``, the parameter
314
- in the second overload. We cannot eliminate the second overload because there
315
- are possible manifestations of ``list[Any] `` (for example, `` list[str] ``) that
316
- are not assignable to ``list[int] ``.
327
+ all materializations of ``list[Any] `` are assignable to ``list[Any] ``, the
328
+ parameter in the second overload. We cannot eliminate the second overload
329
+ because there are possible materializations of ``list[Any] `` (for example,
330
+ `` list[str] ``) that are not assignable to ``list[int] ``.
317
331
318
332
Once this filtering process is applied for all arguments, examine the return
319
333
types of the remaining overloads. If these return types include type variables,
0 commit comments