Skip to content

Commit 68c6e30

Browse files
committed
Document and rename overlap-overload error code
A new error code was introduced in #16061 As per #16068, we didn't previously run doc builds on changes to errorcodes.py, causing tests to fail on master when this was merged. Renaming the code as per: #16061 (comment) All type ignores should be unsafe, so we should save the unsafe adjective for things that are really unsafe. As it stands, there are many cases where overloads overlap somewhat benignly.
1 parent 8b73cc2 commit 68c6e30

File tree

5 files changed

+30
-5
lines changed

5 files changed

+30
-5
lines changed

docs/source/error_code_list.rst

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1114,6 +1114,31 @@ Warn about cases where a bytes object may be converted to a string in an unexpec
11141114
print(f"The alphabet starts with {b!r}") # The alphabet starts with b'abc'
11151115
print(f"The alphabet starts with {b.decode('utf-8')}") # The alphabet starts with abc
11161116
1117+
.. _code-overload-overlap:
1118+
1119+
Check that overloaded functions don't overlap [overload-overlap]
1120+
----------------------------------------------------------------
1121+
1122+
Warn if multiple ``@overload`` variants overlap in unsafe ways.
1123+
1124+
.. code-block:: python
1125+
1126+
from typing import overload
1127+
1128+
class A: ...
1129+
class B(A): ...
1130+
1131+
@overload
1132+
def foo(x: B) -> int: ... # Error: Overloaded function signatures 1 and 2 overlap with incompatible return types [overload-overlap]
1133+
@overload
1134+
def foo(x: A) -> str: ...
1135+
def foo(x): ...
1136+
1137+
def takes_a(a: A) -> str:
1138+
return foo(a)
1139+
1140+
assert isinstance(takes_a(B()), str) # may fail when run
1141+
11171142
.. _code-annotation-unchecked:
11181143

11191144
Notify about an annotation in an unchecked function [annotation-unchecked]

mypy/errorcodes.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -262,8 +262,8 @@ def __hash__(self) -> int:
262262
# This is a catch-all for remaining uncategorized errors.
263263
MISC: Final = ErrorCode("misc", "Miscellaneous other checks", "General")
264264

265-
UNSAFE_OVERLOAD: Final[ErrorCode] = ErrorCode(
266-
"unsafe-overload",
265+
OVERLOAD_OVERLAP: Final[ErrorCode] = ErrorCode(
266+
"overload-overlap",
267267
"Warn if multiple @overload variants overlap in unsafe ways",
268268
"General",
269269
sub_code_of=MISC,

mypy/messages.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1604,7 +1604,7 @@ def overloaded_signatures_overlap(self, index1: int, index2: int, context: Conte
16041604
"Overloaded function signatures {} and {} overlap with "
16051605
"incompatible return types".format(index1, index2),
16061606
context,
1607-
code=codes.UNSAFE_OVERLOAD,
1607+
code=codes.OVERLOAD_OVERLAP,
16081608
)
16091609

16101610
def overloaded_signature_will_never_match(

mypy/types.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3019,7 +3019,7 @@ def get_proper_type(typ: Type | None) -> ProperType | None:
30193019

30203020

30213021
@overload
3022-
def get_proper_types(types: list[Type] | tuple[Type, ...]) -> list[ProperType]: # type: ignore[unsafe-overload]
3022+
def get_proper_types(types: list[Type] | tuple[Type, ...]) -> list[ProperType]: # type: ignore[overload-overlap]
30233023
...
30243024

30253025

test-data/unit/check-errorcodes.test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1077,7 +1077,7 @@ x = 1 # type: ignore # E: Unused "type: ignore" comment [unused-ignore]
10771077
from typing import overload, Union
10781078

10791079
@overload
1080-
def unsafe_func(x: int) -> int: ... # E: Overloaded function signatures 1 and 2 overlap with incompatible return types [unsafe-overload]
1080+
def unsafe_func(x: int) -> int: ... # E: Overloaded function signatures 1 and 2 overlap with incompatible return types [overload-overlap]
10811081
@overload
10821082
def unsafe_func(x: object) -> str: ...
10831083
def unsafe_func(x: object) -> Union[int, str]:

0 commit comments

Comments
 (0)