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

type inferance from await asyncio.gather() with 6 elements no longer works #17030

Open
jacob-keller opened this issue Mar 14, 2024 · 2 comments
Labels
bug mypy got something wrong

Comments

@jacob-keller
Copy link

Bug Report

mypy doesn't seem to properly infer types when unpacking the results of an asyncio.gather() that has at least 6 elements.

This leads to the unpacked variables getting assigned the object type, and then future uses of the variable cause typing issues.

To Reproduce

import asyncio
import datetime

async def get_str_value() -> str:
    await asyncio.sleep(1)

    return 'value'

async def get_datetime_value() -> datetime.datetime:
    await asyncio.sleep(2)

    return datetime.datetime.now()

async def run() -> None:

    (str1_value,
     str2_value,
     str3_value,
     datetime1_value,
     str4_value,
     str5_value,
     datetime2_value) = reveal_type(await asyncio.gather(get_str_value(),
                                                         get_str_value(),
                                                         get_str_value(),
                                                         get_datetime_value(),
                                                         get_str_value(),
                                                         get_str_value(),
                                                         get_datetime_value()))

    reveal_type(str1_value)
    reveal_type(datetime1_value)

    print(f"str={str1_value}, date={datetime1_value}")

asyncio.run(run())

Expected Behavior

mypy should be able to infer the appropriate type of the values from the gathered list. From searching, it looks like this might be a typeshed issue, or it might be something in mypy itself. It is unclear what can be done here.

Actual Behavior

mypy infers the return of the await as a list, and then assigns the object type to each of the variables. This results in cascading type errors as other code assumes those variables would be the appropriate type.

If you only have 5 values, then this correctly unpacks the tuple into the specific variables. But as soon as you gather 6 items it breaks.

asyncio-gather-test.py:21: note: Revealed type is "tuple[builtins.str, builtins.str, builtins.str, datetime.datetime, builtins.str, builtins.str]"
asyncio-gather-test.py:29: note: Revealed type is "builtins.str"
asyncio-gather-test.py:30: note: Revealed type is "datetime.datetime"

Your Environment

  • Mypy version used: 1.9, I also tried a development build
  • Mypy command-line flags: mypy
  • Mypy configuration options from mypy.ini (and other config files): none
  • Python version used: 3.12.2

I'm really not sure what a good workaround in my own code would be here. For now I've taken to using asserts to check the types, which works ok, but is verbose.

@jacob-keller jacob-keller added the bug mypy got something wrong label Mar 14, 2024
@erictraut
Copy link

erictraut commented Mar 15, 2024

Mypy's behavior is correct here. The typeshed definition for asyncio.gather provides overloads for one to six arguments. You're passing seven arguments, so the behavior is dictated by a fallback overload:

    def gather(*coros_or_futures: _FutureLike[_T], return_exceptions: Literal[False] = False) -> Future[list[_T]]: ...

Because mypy uses joins rather than unions, the resulting return type is Future[list[object]]. (By contrast, pyright evaluates the return type as Future[list[str | datetime]]).

To support seven or more arguments, the typeshed definition would need to be extended to include more overloads.

@jacob-keller
Copy link
Author

Right that makes sense. I suspect this used to work with an older version of typeshed at some point. I'll investigate there if it's worth fixing or if I can add my own overload to my project or something

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong
Projects
None yet
Development

No branches or pull requests

2 participants