Skip to content

Mypy tries checking for __enter__ and __exit__ for "async with" under certain conditions #9261

@agronholm

Description

@agronholm

Note: if you are reporting a wrong signature of a function or a class in
the standard library, then the typeshed tracker is better suited
for this report: https://github.com/python/typeshed/issues

Please provide more information to help us understand the issue:

  • Are you reporting a bug, or opening a feature request?

Reporting a bug.

  • Please insert below the code you are checking with mypy,
    or a mock-up repro if the source is private. We would appreciate
    if you try to simplify your case to a minimal repro.
from typing import Generic, AsyncContextManager, TypeVar

T_Item = TypeVar('T_Item', str, int)

class Foo(Generic[T_Item]):
    async def foo(self, manager: AsyncContextManager):
        async with manager:
            pass

Strangely, removing the typevar constraints makes the problem go away.

  • What is the actual behavior/output?
bug.py:7: error: "AsyncContextManager[Any]" has no attribute "__enter__"; maybe "__aenter__"?
bug.py:7: error: "AsyncContextManager[Any]" has no attribute "__exit__"; maybe "__aexit__"?
  • What is the behavior/output you expect?

No errors.

  • What are the versions of mypy and Python you are using?
    Do you see the same issue after installing mypy from Git master?

Mypy 0.782.
Yes, this also happens with master (7938f5d).

  • What are the mypy flags you are using? (For example --strict-optional)

No flags used.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy