Python type hint error - AsyncGenerator yield

Issue shows up with both of the following

  • SonarQube 8.9.1 build 44547
  • SonarLint 2.2.0
import contextlib
from typing import AsyncIterable

@contextlib.asynccontextmanager
async def sample() -> AsyncIterable[int]:
    yield 1

Note that AsyncGenerator is also not accepted by SonarQube, its refusing to aknowledge that this is an async function.

The recommendation by SonarQube is to do the following, which is wrong and also not conformant according to mypy

import contextlib
from typing import Generator

@contextlib.asynccontextmanager
async def sample() -> Generator[int, None]:
    yield 1

Is there a workaround?

Hello @hnassrat.

Welcome to the community and thanks for your report. We will review it and answer to you here in this topic. But it can take some time.
Have a good day!

1 Like

Here is a simpler example, the context manager isnt needed

# Sonarlint complains on this one
async def sample() -> AsyncIterator[int]:
    yield 1

# Sonarlint is happy with this one, but it is wrong (mypy complains)
async def sample() -> Iterator[int]:
    yield 1

Note both AsyncIterator and AsyncGenerator both cause the same Sonarlint error S5886, but Generator and Iterator seem to work, but it should not.

1 Like

Hi @hnassrat ,

Thanks a lot for your feedback and for reporting this problem.
I created one ticket for the false positive: https://jira.sonarsource.com/browse/SONARPY-900
and one for the false negative: https://jira.sonarsource.com/browse/SONARPY-901

Is there a workaround?

Until we deliver a fix for this FP, on SonarLint you could add a comment # NOSONAR to deactivate the single issue.
Otherwise, if the rule is generating too much noise in your project, you could also deactivate the rule under “SonarLint Rules” tab in VSCode.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.