Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Add hint for AsyncIterator incompatible return type#15883

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
hauntsaninja merged 2 commits intopython:masterfromikonst:async-iterator-hint
Aug 30, 2023
Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletionsmypy/messages.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -1310,6 +1310,22 @@ def return_type_incompatible_with_supertype(
code=codes.OVERRIDE,
)

original = get_proper_type(original)
override = get_proper_type(override)
if (
isinstance(original, Instance)
and isinstance(override, Instance)
and override.type.fullname == "typing.AsyncIterator"
and original.type.fullname == "typing.Coroutine"
and len(original.args) == 3
and original.args[2] == override
):
self.note(f'Consider declaring "{name}" in {target} without "async"', context)
self.note(
"See https://mypy.readthedocs.io/en/stable/more_types.html#asynchronous-iterators",
context,
)

def override_target(self, name: str, name_in_super: str, supertype: str) -> str:
target = f'supertype "{supertype}"'
if name_in_super != name:
Expand Down
16 changes: 16 additions & 0 deletionstest-data/unit/check-async-await.test
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -1021,3 +1021,19 @@ def coro() -> Generator[int, None, None]:
reveal_type(coro) # N: Revealed type is "def () -> typing.AwaitableGenerator[builtins.int, None, None, typing.Generator[builtins.int, None, None]]"
[builtins fixtures/async_await.pyi]
[typing fixtures/typing-async.pyi]

[case asyncIteratorInProtocol]
from typing import AsyncIterator, Protocol

class P(Protocol):
async def launch(self) -> AsyncIterator[int]:
raise BaseException

class Launcher(P):
def launch(self) -> AsyncIterator[int]: # E: Return type "AsyncIterator[int]" of "launch" incompatible with return type "Coroutine[Any, Any, AsyncIterator[int]]" in supertype "P" \
# N: Consider declaring "launch" in supertype "P" without "async" \
Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't it instead say: "consider addingasync todef lauch()"?

Because in my experience, AsyncIterators are way more common withasync def rather than regulardef

Copy link
ContributorAuthor

@ikonstikonstAug 29, 2023
edited
Loading

Choose a reason for hiding this comment

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

Ehh, this is confusing :|

The 'problem' here should really be reported on this line:

  class P(Protocol):!>    async def launch(self) -> AsyncIterator[int]:         raise BaseException

but we don't put notices proactively on declarations that smell funny, or do we?

The error on the derivedLauncher.launch gives us the excuse to make a notice. What we want to say is "listen, we must error onLauncher.launch, butLauncher.launch is probably fine, consider changing the base declaration instead".

# N: See https://mypy.readthedocs.io/en/stable/more_types.html#asynchronous-iterators
raise BaseException

[builtins fixtures/async_await.pyi]
[typing fixtures/typing-async.pyi]

[8]ページ先頭

©2009-2025 Movatter.jp