Skip to content

False positive when overriding an async method #5125

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

Closed
JelleZijlstra opened this issue May 30, 2018 · 3 comments
Closed

False positive when overriding an async method #5125

JelleZijlstra opened this issue May 30, 2018 · 3 comments
Labels
topic-inheritance Inheritance and incompatible overrides topic-usability

Comments

@JelleZijlstra
Copy link
Member

from typing import Any, Callable, Awaitable

AnyFunction = Callable[..., Awaitable[Any]]

def retry() -> Callable[[AnyFunction], AnyFunction]:
    def decorator(fn: AnyFunction) -> AnyFunction:
        return fn
    return decorator

class B:
    async def run(self) -> bool:
        return True

class C(B):
    @retry()
    async def run(self) -> bool:
        return True

with master produces

$ /home/jelle/mypy/venv/bin/python -m mypy untitled.py 
untitled.py:16: error: Signature of "run" incompatible with supertype "B"

This is a regression since 0.600.

@JelleZijlstra JelleZijlstra added the bug mypy got something wrong label May 30, 2018
@ilevkivskyi
Copy link
Member

OK, this is an actual type error, you should either update the AnyFunction to Callable[..., Coroutine[None, None, Any]] or use a type variable with current type as a bound. I would recommend the second one, I always use it myself and it gives tighter types, with AnyFunction = TypeVar('AnyFunction', bound=Callable[..., Awaitable[Any]]) the code passes.

This is a bit verbose, but I think @gvanrossum proposed that we should actually infer the most precise types for async defs. Possible solution is to define an alias like NativeCoro = Coroutine[None, None, T].

@ilevkivskyi ilevkivskyi added topic-usability and removed bug mypy got something wrong labels May 31, 2018
@JelleZijlstra
Copy link
Member Author

Thanks for clarifying! So the issue is that in the base class run is now inferred as (roughly) Callable[..., Coroutine], and in the child class it is the more general type Callable[..., Awaitable].

I feel like this should perhaps be allowed (just like we allow overriding a function with a different Callable), but it doesn't seem like a big deal.

@JelleZijlstra JelleZijlstra added the topic-inheritance Inheritance and incompatible overrides label Mar 19, 2022
@hauntsaninja
Copy link
Collaborator

I think mypy is behaving as expected

@hauntsaninja hauntsaninja closed this as not planned Won't fix, can't repro, duplicate, stale Aug 28, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic-inheritance Inheritance and incompatible overrides topic-usability
Projects
None yet
Development

No branches or pull requests

3 participants