Skip to content

AttributeError when async with block has mixed return paths and non-return branch is taken #1115

@Chainfire

Description

@Chainfire

Conditions:

  • inside a an async context manager block
  • a branch must exist that explicitly returns
  • another branch must exist that does not explicitly return
  • the AttributeError occurs when the branch that does not explicitly return is executed
from types import TracebackType

import asyncio
from typing import Optional, Type, Literal


class DummyContext:
    async def __aenter__(self) -> None:
        pass

    async def __aexit__(
        self,
        t: Optional[Type[BaseException]],
        v: Optional[BaseException],
        tb: Optional[TracebackType],
    ) -> Literal[False]:
        return False


async def early_exit_bug() -> None:
    async with DummyContext():
        if False:
            return


asyncio.run(early_exit_bug())

Raises:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "test_mypyc_async_with_bug.py", line XXX, in <module>
    asyncio.run(early_exit_bug())
  File "/usr/lib/python3.12/asyncio/runners.py", line 195, in run
    return runner.run(main)
           ^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.12/asyncio/runners.py", line 118, in run
    return self._loop.run_until_complete(task)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.12/asyncio/base_events.py", line 691, in run_until_complete
    return future.result()
           ^^^^^^^^^^^^^^^
  File "test_mypyc_async_with_bug.py", line -1, in early_exit_bug
AttributeError: attribute '__mypyc_temp__4' of 'early_exit_bug_gen' undefined

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions