Skip to content

False positive when ignoring the yield type of an async context manager #11020

@Apakottur

Description

@Apakottur

Describe the bug
Consider the following code:

# test.py
import asyncio
from contextlib import asynccontextmanager
from typing import AsyncIterator

@asynccontextmanager
async def manager() -> AsyncIterator[int]:
    yield 1

async def main() -> None:
    async with (
        manager(),
    ):
        print("Hello!")

asyncio.run(main())

Running pyright test.py gives:

./test.py
  ./test.py:13:16 - error: Object of type "tuple[_AsyncGeneratorContextManager[int, None]]" cannot be used with "async with" because it does not correctly implement __aenter__
    Attribute "__aenter__" is unknown (reportGeneralTypeIssues)
  ./test.py:13:16 - error: Object of type "tuple[_AsyncGeneratorContextManager[int, None]]" cannot be used with "with" because it does not correctly implement __aexit__
    Attribute "__aexit__" is unknown (reportGeneralTypeIssues)
2 errors, 0 warnings, 0 informations

The code is valid and prints "Hello!" as expected, but Pyright seems to have an issue with the context manager.
Am I missing something or is this a bug?

I'm using pyright==1.1.406.

Metadata

Metadata

Assignees

No one assigned

    Labels

    addressed in next versionIssue is fixed and will appear in next published versionbugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions