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

Missing warning for unawaited coroutine when invoking method in superclass #14113

Closed
mschurr opened this issue Nov 16, 2022 · 9 comments
Closed
Labels
feature topic-async async, await, asyncio

Comments

@mschurr
Copy link

mschurr commented Nov 16, 2022

Bug Report

An edge-case where warn-unawaited-coroutine does not work.

To Reproduce

class Parent:
    async def func(self) -> bool:
        return True


class Child(Parent):
    async def func(self) -> bool:
        # no warn-unawaited-coroutine emitted here?
        if not super().func():
            return False
        return True

Expected Behavior

Should warn about unawaited coroutine/incompatible type.

Actual Behavior

No warning.

Your Environment

mypy 0.971

@mschurr mschurr added the bug mypy got something wrong label Nov 16, 2022
@mschurr mschurr changed the title Missing warning for unawaited coroutine Missing warning for unawaited coroutine when invoking method in superclass Nov 16, 2022
@AlexWaygood AlexWaygood added the topic-async async, await, asyncio label Nov 17, 2022
@erictraut
Copy link

I don't think mypy has a feature to detect unawaited coroutines. In general, it's not a bug to return a coroutine without awaiting it. Using a coroutine in a conditional expression (especially when applying a not unary operator) is probably an unintended bug, so maybe that's something mypy could detect. I think that would be a new feature though.

@mschurr
Copy link
Author

mschurr commented Sep 7, 2023

https://mypy.readthedocs.io/en/stable/error_code_list.html#check-that-coroutine-return-value-is-used-unused-coroutine

I was probably thinking of this.

Maybe truthy-function handles this, though, and I should be turning that on.

@mschurr
Copy link
Author

mschurr commented Sep 7, 2023

I've confirmed truthy-function also does not catch this, so maybe this is still a bug?

@JelleZijlstra JelleZijlstra added feature and removed bug mypy got something wrong labels Sep 7, 2023
@JelleZijlstra
Copy link
Member

The super() calls are a red herring, here's a simpler repro:

async def func() -> bool:
    return True

async def func2() -> None:
    if func():
        print("hi")

I think this should be a new feature detecting that func() returns a Coroutine object, which is always true. This is similar to but distinct from truthy-function.

@mschurr-extrahop
Copy link

mschurr-extrahop commented Sep 7, 2023

Worth noting that truthy-bool does catch both these cases. So maybe that's sufficient?

"mypy-test.func" returns "Coroutine[Any, Any, bool]" which does not implement __bool__ or __len__ so it could always be true in boolean context  [truthy-bool]

@JelleZijlstra
Copy link
Member

Thanks, I didn't try that code. I think that's enough.

@ikonst
Copy link
Contributor

ikonst commented Sep 7, 2023

I believe truthy-bool is still not default, and might not ever be, which is why we added truthy-function. Should we add a truthy-coroutine?

@JukkaL
Copy link
Collaborator

JukkaL commented Sep 7, 2023

Should we add a truthy-coroutine?

This sounds reasonable to me. It seems unlikely to produce a false positive, at least if strict optional checking is enabled.

@ikonst
Copy link
Contributor

ikonst commented Sep 7, 2023

Created #16069.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature topic-async async, await, asyncio
Projects
None yet
Development

No branches or pull requests

7 participants