8000 Add better types for asyncio.gather by Gobot1234 · Pull Request #9678 · python/typeshed · GitHub
[go: up one dir, main page]

Skip to content

Add better types for asyncio.gather #9678

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

Merged
merged 14 commits into from
Oct 4, 2023
Merged

Conversation

Gobot1234
Copy link
Contributor

Before:
image

After:
image

@github-actions

This comment has been minimized.

@Gobot1234
Copy link
Contributor Author

Looking at #9105 I don't think the case presented where you have vargs of different types actually happens that much whereas unpacking as shown in the screenshots above is much more common and helps catch errors that would have gone amiss.

@AlexWaygood
Copy link
Member

This looks promising.

The homeassistant hit is here: https://github.com/home-assistant/core/blob/04e05af44cbea2156d904d0d34e4e0d41c9725c1/homeassistant/components/amcrest/camera.py#L404-L418. They just miss out on the precise overloads, since they're passing in 6 coroutines. Previously the return type of the call to asyncio.gather was inferred as list[Any]; with this change, it is inferred as list[object]. That's probably not too terrible; and we could probably get rid of the hit by just adding an overload for if you pass in six coroutines.

Some of the test cases in https://github.com/python/typeshed/blob/main/test_cases/stdlib/asyncio/check_gather.py are failing, and will need to be updated.

Lastly, as well as adding the new overload, we should probably also update the existing fallback overloads here to be more precise as well:

@overload
def gather(*coros_or_futures: _FutureLike[Any], return_exceptions: bool = False) -> Future[list[Any]]: ... # type: ignore[misc]

@overload
def gather( # type: ignore[misc]
*coros_or_futures: _FutureLike[Any], loop: AbstractEventLoop | None = None, return_exceptions: bool = False
) -> Future[list[Any]]: ...

They should probably be changed to:

@overload
def gather(*coros_or_futures: _FutureLike[_T], return_exceptions: bool) -> Future[list[_T | BaseException]]: ...  # type: ignore[misc]

@Gobot1234
Copy link
Contributor Author

home-assistant/core@04e05af/homeassistant/components/amcrest/camera.py#L404-L418 would be inferred as list[str | bool] but I guess that can be solved as you said by adding another overload.

@github-actions

This comment has been minimized.

@Gobot1234
Copy link
Contributor Author

I don't think this is gonna work (with pyright at least) cause adding a special case for the 0 arg call (which is required to avoid the return of await asyncio.gather() being list[BaseException], which thinking about it isn't technically wrong because it will always be empty) is preferred by asyncio.gather(*iterable) making the return for that list[BaseException].

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@Gobot1234
Copy link
Contributor Author

All of the failures look like mypy bugs and the failure in pyright is IMO acceptable I just don't know what the test needs to be changed to to pass

@Gobot1234
Copy link
Contributor Author

Hmm the paasta things seem like they do now break which is annoying. I'm not entirely sure what's wrong there though.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@Gobot1234
Copy link
Contributor Author
Gobot1234 commented Oct 2, 2023

This seems to me like it should now work, I'm slightly confused by the behaviour I'm seeing by mypy and pyright here, unpacking a list seems to only give one element?

@github-actions

This comment has been minimized.

8000
Copy link
Collaborator
@srittau srittau left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Copy link
Member
@AlexWaygood AlexWaygood left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks great, thanks for persevering with this! Pushed one more test just to be safe.

@AlexWaygood AlexWaygood merged commit 25eb99c into python:main Oct 4, 2023
@github-actions
Copy link
Contributor
github-actions bot commented Oct 4, 2023

According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉

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

Successfully merging this pull request may close these issues.

3 participants
0