Skip to content

Commit 175c5a5

Browse files
Introduce error category [unsafe-overload] (#16061)
fixes #16060 Co-authored-by: Alex Waygood <[email protected]>
1 parent ed9b899 commit 175c5a5

File tree

4 files changed

+23
-1
lines changed

4 files changed

+23
-1
lines changed

mypy/errorcodes.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,3 +261,10 @@ def __hash__(self) -> int:
261261

262262
# This is a catch-all for remaining uncategorized errors.
263263
MISC: Final = ErrorCode("misc", "Miscellaneous other checks", "General")
264+
265+
UNSAFE_OVERLOAD: Final[ErrorCode] = ErrorCode(
266+
"unsafe-overload",
267+
"Warn if multiple @overload variants overlap in unsafe ways",
268+
"General",
269+
sub_code_of=MISC,
270+
)

mypy/messages.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1604,6 +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,
16071608
)
16081609

16091610
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[misc]
3022+
def get_proper_types(types: list[Type] | tuple[Type, ...]) -> list[ProperType]: # type: ignore[unsafe-overload]
30233023
...
30243024

30253025

test-data/unit/check-errorcodes.test

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1072,3 +1072,17 @@ A.f = h # type: ignore[assignment] # E: Unused "type: ignore" comment, use nar
10721072
[case testUnusedIgnoreEnableCode]
10731073
# flags: --enable-error-code=unused-ignore
10741074
x = 1 # type: ignore # E: Unused "type: ignore" comment [unused-ignore]
1075+
1076+
[case testErrorCodeUnsafeOverloadError]
1077+
from typing import overload, Union
1078+
1079+
@overload
1080+
def unsafe_func(x: int) -> int: ... # E: Overloaded function signatures 1 and 2 overlap with incompatible return types [unsafe-overload]
1081+
@overload
1082+
def unsafe_func(x: object) -> str: ...
1083+
def unsafe_func(x: object) -> Union[int, str]:
1084+
if isinstance(x, int):
1085+
return 42
1086+
else:
1087+
return "some string"
1088+
[builtins fixtures/isinstancelist.pyi]

0 commit comments

Comments
 (0)