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

Added error code for overlapping function signatures #17597

Merged
merged 15 commits into from
Sep 18, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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
20 changes: 20 additions & 0 deletions docs/source/error_code_list.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1149,6 +1149,26 @@ types you expect.

See :ref:`overloading <function-overloading>` for more explanation.


.. _code-overloaded-function-matching:

Check error code of overload function signature match
katconnors marked this conversation as resolved.
Show resolved Hide resolved
katconnors marked this conversation as resolved.
Show resolved Hide resolved
--------------------------------------------------------------------------

.. code-block:: python

from typing import overload, Union

@overload
def process(response1: float,response2: float) -> float:
...
@overload
def process(response1: int,response2: int) -> int: # E: Overloaded function signature 2 will never be matched: signature 1's parameter type(s) are the same or broader [overloaded-function-matching]
...

def process(response1,response2)-> Union[float,int]:
return response1 + response2
katconnors marked this conversation as resolved.
Show resolved Hide resolved

.. _code-annotation-unchecked:

Notify about an annotation in an unchecked function [annotation-unchecked]
Expand Down
1 change: 1 addition & 0 deletions mypy/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -768,6 +768,7 @@ def check_overlapping_overloads(self, defn: OverloadedFuncDef) -> None:
continue

if overload_can_never_match(sig1, sig2):

katconnors marked this conversation as resolved.
Show resolved Hide resolved
self.msg.overloaded_signature_will_never_match(i + 1, i + j + 2, item2.func)
elif not is_descriptor_get:
# Note: we force mypy to check overload signatures in strict-optional mode
Expand Down
6 changes: 6 additions & 0 deletions mypy/errorcodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,12 @@ def __hash__(self) -> int:
default_enabled=False,
)

OVERLOADED_FUNCTION_MATCHING: Final[ErrorCode] = ErrorCode(
"overloaded-function-matching",
"Warn user about signature matching for overloaded functions.",
"General",
katconnors marked this conversation as resolved.
Show resolved Hide resolved
)

# Syntax errors are often blocking.
SYNTAX: Final[ErrorCode] = ErrorCode("syntax", "Report syntax errors", "General")

Expand Down
5 changes: 5 additions & 0 deletions mypy/message_registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -362,3 +362,8 @@ def with_additional_msg(self, info: str) -> ErrorMessage:
TYPE_ALIAS_WITH_AWAIT_EXPRESSION: Final = ErrorMessage(
"Await expression cannot be used within a type alias", codes.SYNTAX
)

# Overloads
OVERLOADED_FUNCTION_SIGNATURE: Final = ErrorMessage(
katconnors marked this conversation as resolved.
Show resolved Hide resolved
"Overloaded function signature {} will never be matched", codes.OVERLOADED_FUNCTION_MATCHING
)
1 change: 1 addition & 0 deletions mypy/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -1653,6 +1653,7 @@ def overloaded_signature_will_never_match(
index1=index1, index2=index2
),
context,
code=codes.OVERLOADED_FUNCTION_MATCHING,
)

def overloaded_signatures_typevar_specific(self, index: int, context: Context) -> None:
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 @@ -1196,3 +1196,17 @@ def f(x: str) -> TypeIs[int]: # E: Narrowed type "int" is not a subtype of inpu
pass

[builtins fixtures/tuple.pyi]


[case testOverloadedFunctionSignature]
from typing import overload, Union

@overload
def process(response1: float,response2: float) -> float:
...
@overload
def process(response1: int,response2: int) -> int: # E: Overloaded function signature 2 will never be matched: signature 1's parameter type(s) are the same or broader [overloaded-function-matching]
...

def process(response1,response2)-> Union[float,int]:
return response1 + response2
Loading