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

Argument 1 to "__anext__" of "AsyncIterator" has incompatible type "AsyncIterator[_T_co]"; expected "AsyncIterator[_T_co]" #12723

Closed
aaugustin opened this issue May 3, 2022 · 4 comments
Labels
bug mypy got something wrong topic-async async, await, asyncio topic-error-reporting How we report errors topic-usability

Comments

@aaugustin
Copy link

aaugustin commented May 3, 2022

To Reproduce

While making changes in websockets to account for changes in mypy 0.941 -> 0.950, I encountered this error:

Argument 1 to "__anext__" of "AsyncIterator" has incompatible type "AsyncIterator[_T_co]"; expected "AsyncIterator[_T_co]"

Regardless of what actually happens, "incompatible A, expected A" is not a helpful error message. This is why I am reporting this bug. See https://github.com/aaugustin/websockets/runs/6272618187?check_suite_focus=true for the test run where this happened.

I'm sorry, I'm not sure I will find the motivation to reduce this to a minimal test case. I'm probably going to pile cast() until mypy passes. If you're interested, you can reproduce by checking out this commit python-websockets/websockets@dedc0dd and running mypy 0.950 on it. If not, just close this.

Expected Behavior

Either no error — apparently mypy gets what it expects? — or an error message that makes sense.

Actual Behavior

A nonsensical error message.

Your Environment

  • Mypy version used: 0.950 installed with pipx (so, up to date and isolated - I reinstalled it and that didn't help)
  • Mypy command-line flags: --strict
  • Mypy configuration options from mypy.ini (and other config files): -
  • Python version used: 3.10.2 from homebrew
  • Operating system and version: macOS 12.3.1
@aaugustin aaugustin added the bug mypy got something wrong label May 3, 2022
@JelleZijlstra
Copy link
Member

Repro case:

from typing import AsyncIterable

async def func(message: AsyncIterable[bytes | str] | bytes | str) -> None:
        if isinstance(message, AsyncIterable):
            # aiter_message = aiter(message) without aiter
            # https://github.com/python/mypy/issues/5738
            aiter_message = type(message).__aiter__(message)  # type: ignore
            try:
                # message_chunk = anext(aiter_message) without anext
                # https://github.com/python/mypy/issues/5738
                message_chunk = await type(aiter_message).__anext__(  # type: ignore
                    aiter_message
                )
            except StopAsyncIteration:
                return

https://mypy-play.net/?mypy=latest&python=3.10&gist=3c85aae085133db6d751711196fd4b76

@AlexWaygood AlexWaygood added topic-usability topic-error-reporting How we report errors topic-async async, await, asyncio labels May 3, 2022
@aaugustin
Copy link
Author

Oh, this is actually a change in the behavior of # type: ignore comments.

This fixes the problem:

diff --git a/src/websockets/legacy/protocol.py b/src/websockets/legacy/protocol.py
index d1d52bf..ada4140 100644
--- a/src/websockets/legacy/protocol.py
+++ b/src/websockets/legacy/protocol.py
@@ -684,8 +684,8 @@ class WebSocketCommonProtocol(asyncio.Protocol):
             try:
                 # message_chunk = anext(aiter_message) without anext
                 # https://github.com/python/mypy/issues/5738
-                message_chunk = await type(aiter_message).__anext__(  # type: ignore
-                    aiter_message
+                message_chunk = await type(aiter_message).__anext__(
+                    aiter_message  # type: ignore
                 )
             except StopAsyncIteration:
                 return

I assume that was an intentional change in how # type: ignore comments work when arguments are split over multiple lines and the error affects only one argument.

@JelleZijlstra
Copy link
Member

Interesting, I don't remember any recent changes that would affect where the error is emitted.

However, you are quite right that at best, the error message is confusing, and probably the whole thing is buggy. Your comment refers to #5738, which might be right.

@aaugustin
Copy link
Author

I think it's fair to close as a duplicate of #5738 -- if there's something interesting in how errors are reported, someone else will find it again :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong topic-async async, await, asyncio topic-error-reporting How we report errors topic-usability
Projects
None yet
Development

No branches or pull requests

3 participants