Skip to content

Add no-overload-impl error code #11944

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

Merged
merged 3 commits into from Jan 15, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
22 changes: 22 additions & 0 deletions docs/source/error_code_list.rst
Original file line number Diff line number Diff line change
Expand Up @@ -657,6 +657,28 @@ consistently when using the call-based syntax. Example:
# Error: First argument to namedtuple() should be "Point2D", not "Point"
Point2D = NamedTuple("Point", [("x", int), ("y", int)])

Check that overloaded functions have an implementation [no-overload-impl]
-------------------------------------------------------------------------

Overloaded functions outside of stub files must be followed by a non overloaded
implementation.

.. code-block:: python

from typing import overload

@overload
def func(value: int) -> int:
...

@overload
def func(value: str) -> str:
...

# presence of required function below is checked
def func(value):
pass # actual implementation

Report syntax errors [syntax]
-----------------------------

Expand Down
5 changes: 5 additions & 0 deletions mypy/errorcodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,11 @@ def __str__(self) -> str:
NAME_MATCH: Final = ErrorCode(
"name-match", "Check that type definition has consistent naming", "General"
)
NO_OVERLOAD_IMPL: Final = ErrorCode(
"no-overload-impl",
"Check that overloaded functions outside stub files have an implementation",
"General",
)


# Syntax errors are often blocking.
Expand Down
2 changes: 1 addition & 1 deletion mypy/semanal.py
Original file line number Diff line number Diff line change
Expand Up @@ -878,7 +878,7 @@ def handle_missing_overload_implementation(self, defn: OverloadedFuncDef) -> Non
else:
self.fail(
"An overloaded function outside a stub file must have an implementation",
defn)
defn, code=codes.NO_OVERLOAD_IMPL)

def process_final_in_overload(self, defn: OverloadedFuncDef) -> None:
"""Detect the @final status of an overloaded function (and perform checks)."""
Expand Down
11 changes: 11 additions & 0 deletions test-data/unit/check-errorcodes.test
Original file line number Diff line number Diff line change
Expand Up @@ -898,6 +898,17 @@ if lst:
pass
[builtins fixtures/list.pyi]

[case testNoOverloadImplementation]
from typing import overload

@overload # E: An overloaded function outside a stub file must have an implementation [no-overload-impl]
def f(arg: int) -> int:
...

@overload
def f(arg: str) -> str:
...

[case testSliceInDict39]
# flags: --python-version 3.9 --show-column-numbers
from typing import Dict
Expand Down