Skip to content

Commit

Permalink
Let overload item have a wider return type than implementation (#12435)
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 24, 2022
1 parent 67088e5 commit 1c83668
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
3 changes: 2 additions & 1 deletion mypy/checker.py
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
21 changes: 21 additions & 0 deletions test-data/unit/check-overloading.test
Expand Up @@ -6328,3 +6328,24 @@ 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

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

@overload
def g(x: int) -> str: ...

def g(x: int = 0) -> int: # E: Overloaded function implementation cannot produce return type of signature 2
return x
1 change: 1 addition & 0 deletions test-data/unit/fine-grained.test
Expand Up @@ -2084,6 +2084,7 @@ a.py:5: error: "list" expects 1 type argument, but 2 given
==

[case testPreviousErrorInOverloadedFunction]
# flags: --strict-optional
import a
[file a.py]
from typing import overload
Expand Down

0 comments on commit 1c83668

Please sign in to comment.