Skip to content

Commit 59ed20e

Browse files
committed
Let overload item have a wider return type than implementation
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 fd8a15e commit 59ed20e

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-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

+12
Original file line numberDiff line numberDiff line change
@@ -6328,3 +6328,15 @@ 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

0 commit comments

Comments
 (0)