Skip to content

Commit 1c83668

Browse files
committed
Let overload item have a wider return type than implementation (#12435)
A wider return type can be useful if a decorator used for the overload implementation gets a more precise return type as part of a typeshed update. Closes #12434.
1 parent 67088e5 commit 1c83668

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

mypy/checker.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -600,7 +600,8 @@ def check_overlapping_overloads(self, defn: OverloadedFuncDef) -> None:
600600
self.msg.overloaded_signatures_arg_specific(i + 1, defn.impl)
601601

602602
# Is the overload alternative's return type a subtype of the implementation's?
603-
if not is_subtype_no_promote(sig1.ret_type, impl.ret_type):
603+
if not (is_subtype_no_promote(sig1.ret_type, impl.ret_type) or
604+
is_subtype_no_promote(impl.ret_type, sig1.ret_type)):
604605
self.msg.overloaded_signatures_ret_specific(i + 1, defn.impl)
605606

606607
# Here's the scoop about generators and coroutines.

test-data/unit/check-overloading.test

+21
Original file line numberDiff line numberDiff line change
@@ -6328,3 +6328,24 @@ if True:
63286328
def f2(x): ...
63296329
if True:
63306330
def f2(x): ... # E: Name "f2" already defined on line 17
6331+
6332+
[case testOverloadItemHasMoreGeneralReturnType]
6333+
from typing import overload
6334+
6335+
@overload
6336+
def f() -> object: ...
6337+
6338+
@overload
6339+
def f(x: int) -> object: ...
6340+
6341+
def f(x: int = 0) -> int:
6342+
return x
6343+
6344+
@overload
6345+
def g() -> object: ...
6346+
6347+
@overload
6348+
def g(x: int) -> str: ...
6349+
6350+
def g(x: int = 0) -> int: # E: Overloaded function implementation cannot produce return type of signature 2
6351+
return x

test-data/unit/fine-grained.test

+1
Original file line numberDiff line numberDiff line change
@@ -2084,6 +2084,7 @@ a.py:5: error: "list" expects 1 type argument, but 2 given
20842084
==
20852085

20862086
[case testPreviousErrorInOverloadedFunction]
2087+
# flags: --strict-optional
20872088
import a
20882089
[file a.py]
20892090
from typing import overload

0 commit comments

Comments
 (0)