Skip to content

Commit

Permalink
Introduce error category [unsafe-overload] (#16061)
Browse files Browse the repository at this point in the history
fixes #16060

Co-authored-by: Alex Waygood <[email protected]>
  • Loading branch information
randolf-scholz and AlexWaygood authored Sep 7, 2023
1 parent ed9b899 commit 175c5a5
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 1 deletion.
7 changes: 7 additions & 0 deletions mypy/errorcodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,3 +261,10 @@ def __hash__(self) -> int:

# This is a catch-all for remaining uncategorized errors.
MISC: Final = ErrorCode("misc", "Miscellaneous other checks", "General")

UNSAFE_OVERLOAD: Final[ErrorCode] = ErrorCode(
"unsafe-overload",
"Warn if multiple @overload variants overlap in unsafe ways",
"General",
sub_code_of=MISC,
)
1 change: 1 addition & 0 deletions mypy/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -1604,6 +1604,7 @@ def overloaded_signatures_overlap(self, index1: int, index2: int, context: Conte
"Overloaded function signatures {} and {} overlap with "
"incompatible return types".format(index1, index2),
context,
code=codes.UNSAFE_OVERLOAD,
)

def overloaded_signature_will_never_match(
Expand Down
2 changes: 1 addition & 1 deletion mypy/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -3019,7 +3019,7 @@ def get_proper_type(typ: Type | None) -> ProperType | None:


@overload
def get_proper_types(types: list[Type] | tuple[Type, ...]) -> list[ProperType]: # type: ignore[misc]
def get_proper_types(types: list[Type] | tuple[Type, ...]) -> list[ProperType]: # type: ignore[unsafe-overload]
...


Expand Down
14 changes: 14 additions & 0 deletions test-data/unit/check-errorcodes.test
Original file line number Diff line number Diff line change
Expand Up @@ -1072,3 +1072,17 @@ A.f = h # type: ignore[assignment] # E: Unused "type: ignore" comment, use nar
[case testUnusedIgnoreEnableCode]
# flags: --enable-error-code=unused-ignore
x = 1 # type: ignore # E: Unused "type: ignore" comment [unused-ignore]

[case testErrorCodeUnsafeOverloadError]
from typing import overload, Union

@overload
def unsafe_func(x: int) -> int: ... # E: Overloaded function signatures 1 and 2 overlap with incompatible return types [unsafe-overload]
@overload
def unsafe_func(x: object) -> str: ...
def unsafe_func(x: object) -> Union[int, str]:
if isinstance(x, int):
return 42
else:
return "some string"
[builtins fixtures/isinstancelist.pyi]

0 comments on commit 175c5a5

Please sign in to comment.