Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Let overload item have a wider return type than implementation #12435

Merged
merged 3 commits into from
Mar 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
21 changes: 21 additions & 0 deletions test-data/unit/check-overloading.test
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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