-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
typing.overload and contextlib.contextmanager don't work together #11373
Comments
I can confirm that issue with mypy 0.920 and Python 3.9. |
This bug is still present in MyPy 0.941 |
The reason for the error is that, after applying the I think the bug here is the overload return type checking being too strict. There's no inherent unsafety in making the implementation return type a subtype of the overload return type because any code expecting the more generic overload return type will handle the implementation's more specific type correctly. The check should probably be changed to accept either the overload return type being a subtype of the implementation return type (the current check) or the implementation return type being a subtype of the overload return type. |
i think the issue is that mypy doesn't allow overloads with return types that are wider than the implementation's return type, even though it's completely safe: from typing import overload
@overload
def baz(a: int) -> int: ...
@overload
def baz(a: object) -> object: ...
def baz(a: object) -> int: ... # error: Overloaded function implementation cannot produce return type of signature 2 |
@DetachHead I agree. Your scenario is completely valid in overrides, so should also be valid in overloads: from typing import overload
class A:
def foo(a: object) -> object: ...
class B(A):
def foo(a: object) -> int: ... # no error
@overload
def foo(a: int) -> int: ...
@overload
def foo(a: object) -> object: ...
def foo(a: object) -> int: ... # error: Overloaded function implementation cannot produce return type of signature 2 |
Fixed in #12435, will be included in 0.942. |
Bug Report
The following example:
produces the following typing errors:
without the overloadings, mypy recognizes that
foo(1)
returns aContextManager[int]
To Reproduce
Expected Behavior
since the overload is correct for the implementation, mypy should pass in both cases.
Actual Behavior
mypy fails for the first case but passes for the second.
Your Environment
reproduced on current mypy-playground
mypy.ini
(and other config files): noneThe text was updated successfully, but these errors were encountered: