Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce error category [unsafe-overload] #16061

Merged
merged 6 commits into from
Sep 7, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions mypy/errorcodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ def __hash__(self) -> int:
"General",
default_enabled=False,
)

randolf-scholz marked this conversation as resolved.
Show resolved Hide resolved
USED_BEFORE_DEF: Final[ErrorCode] = ErrorCode(
"used-before-def", "Warn about variables that are used before they are defined", "General"
)
Expand All @@ -261,3 +262,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,
)
5 changes: 3 additions & 2 deletions mypy/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -1601,9 +1601,10 @@ def overload_inconsistently_applies_decorator(self, decorator: str, context: Con

def overloaded_signatures_overlap(self, index1: int, index2: int, context: Context) -> None:
self.fail(
"Overloaded function signatures {} and {} overlap with "
"incompatible return types".format(index1, index2),
f"Overloaded function signatures {index1} and {index2} overlap with incompatible return types",
# "See https://mypy.readthedocs.io/en/stable/more_types.html#type-checking-the-variants.",
randolf-scholz marked this conversation as resolved.
Show resolved Hide resolved
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]
Loading