Skip to content

Commit d33fe55

Browse files
Brian Phillipstushar-deepsource
authored andcommitted
Add no-overload-impl error code (python#11944)
1 parent e5d08a4 commit d33fe55

File tree

4 files changed

+39
-1
lines changed

4 files changed

+39
-1
lines changed

docs/source/error_code_list.rst

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -657,6 +657,28 @@ consistently when using the call-based syntax. Example:
657657
# Error: First argument to namedtuple() should be "Point2D", not "Point"
658658
Point2D = NamedTuple("Point", [("x", int), ("y", int)])
659659
660+
Check that overloaded functions have an implementation [no-overload-impl]
661+
-------------------------------------------------------------------------
662+
663+
Overloaded functions outside of stub files must be followed by a non overloaded
664+
implementation.
665+
666+
.. code-block:: python
667+
668+
from typing import overload
669+
670+
@overload
671+
def func(value: int) -> int:
672+
...
673+
674+
@overload
675+
def func(value: str) -> str:
676+
...
677+
678+
# presence of required function below is checked
679+
def func(value):
680+
pass # actual implementation
681+
660682
Report syntax errors [syntax]
661683
-----------------------------
662684

mypy/errorcodes.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,11 @@ def __str__(self) -> str:
133133
NAME_MATCH: Final = ErrorCode(
134134
"name-match", "Check that type definition has consistent naming", "General"
135135
)
136+
NO_OVERLOAD_IMPL: Final = ErrorCode(
137+
"no-overload-impl",
138+
"Check that overloaded functions outside stub files have an implementation",
139+
"General",
140+
)
136141

137142

138143
# Syntax errors are often blocking.

mypy/semanal.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -880,7 +880,7 @@ def handle_missing_overload_implementation(self, defn: OverloadedFuncDef) -> Non
880880
else:
881881
self.fail(
882882
"An overloaded function outside a stub file must have an implementation",
883-
defn)
883+
defn, code=codes.NO_OVERLOAD_IMPL)
884884

885885
def process_final_in_overload(self, defn: OverloadedFuncDef) -> None:
886886
"""Detect the @final status of an overloaded function (and perform checks)."""

test-data/unit/check-errorcodes.test

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -898,6 +898,17 @@ if lst:
898898
pass
899899
[builtins fixtures/list.pyi]
900900

901+
[case testNoOverloadImplementation]
902+
from typing import overload
903+
904+
@overload # E: An overloaded function outside a stub file must have an implementation [no-overload-impl]
905+
def f(arg: int) -> int:
906+
...
907+
908+
@overload
909+
def f(arg: str) -> str:
910+
...
911+
901912
[case testSliceInDict39]
902913
# flags: --python-version 3.9 --show-column-numbers
903914
from typing import Dict

0 commit comments

Comments
 (0)