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

Require all overloads be either sync or async #16167

Closed
wants to merge 1 commit into from
Closed

Conversation

sobolevn
Copy link
Member

Overloads are a typing-only contruct, so real python implementation is either sync or async. Having some items as async and some as sync does not make much sense and is an error probably.

Closes #16166

@sobolevn
Copy link
Member Author

sobolevn commented Sep 23, 2023

Please, see #14996
Sometimes you expect sync overloads when funtion returns AsyncIterable or AsyncGenerator

@hauntsaninja can you please explain this API, please? This seems like a bug to me :)

@github-actions
Copy link
Contributor

Diff from mypy_primer, showing the effect of this PR on open source code:

discord.py (https://github.com/Rapptz/discord.py)
+ discord/client.py:1149: error: All overloaded items but be either sync or async  [misc]

psycopg (https://github.com/psycopg/psycopg)
+ psycopg/psycopg/_typeinfo.py:56: error: All overloaded items but be either sync or async  [misc]

jinja (https://github.com/pallets/jinja)
+ src/jinja2/filters.py:1475: error: All overloaded items but be either sync or async  [misc]

Expression (https://github.com/cognitedata/Expression)
+ expression/collections/asyncseq.py:75: error: All overloaded items but be either sync or async  [misc]

@hauntsaninja
Copy link
Collaborator

hauntsaninja commented Sep 23, 2023

Yeah, see the documentation I added here https://mypy.readthedocs.io/en/latest/more_types.html#asynchronous-iterators in particular the last example with launch:

# The type of the overloads is independent of the implementation.
# In particular, their type is not affected by whether or not the
# implementation contains a `yield`.
# Use of `def`` makes it clear the type is Callable[..., AsyncIterator[int]],
# whereas with `async def` it would be Callable[..., Coroutine[Any, Any, AsyncIterator[int]]]
@overload
def launch(*, count: int = ...) -> AsyncIterator[int]: ...
@overload
def launch(*, time: float = ...) -> AsyncIterator[int]: ...

async def launch(*, count: int = 0, time: float = 0) -> AsyncIterator[int]:
    # The implementation of launch is an async generator and contains a yield
    yield 0

As I mention, the root cause is that the presence of yield inside a function body has an effect on the type of the function

@sobolevn sobolevn closed this Jun 24, 2024
@hauntsaninja hauntsaninja deleted the issue-16166 branch June 24, 2024 08:52
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.

@overloads are not checked to be the same as body: sync or async
2 participants