Skip to content

Commit

Permalink
Let overload item have a wider return type than implementation
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
JukkaL committed Mar 23, 2022
1 parent fd8a15e commit 59ed20e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
3 changes: 2 additions & 1 deletion mypy/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -600,7 +600,8 @@ def check_overlapping_overloads(self, defn: OverloadedFuncDef) -> None:
self.msg.overloaded_signatures_arg_specific(i + 1, defn.impl)

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

# Here's the scoop about generators and coroutines.
Expand Down
12 changes: 12 additions & 0 deletions test-data/unit/check-overloading.test
Original file line number Diff line number Diff line change
Expand Up @@ -6328,3 +6328,15 @@ if True:
def f2(x): ...
if True:
def f2(x): ... # E: Name "f2" already defined on line 17

[case testOverloadItemHasMoreGeneralReturnType]
from typing import overload

@overload
def f() -> object: ...

@overload
def f(x: int) -> object: ...

def f(x: int = 0) -> int:
return x

0 comments on commit 59ed20e

Please sign in to comment.