Skip to content

AsyncContextManager is not callable error when used as decorator #6625

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
ljodal opened this issue Dec 17, 2021 · 3 comments · Fixed by #6634
Closed

AsyncContextManager is not callable error when used as decorator #6625

ljodal opened this issue Dec 17, 2021 · 3 comments · Fixed by #6634

Comments

@ljodal
Copy link

ljodal commented Dec 17, 2021

Bug Report

It apprears that mypy is not aware that starting with Python 3.10 async context managers created using contextlib.asynccontextmanager can be used as decorators.

To Reproduce

The following is example code from the Python docs for contextlib.asynccontextmanager. Running mypy against it results in the error included below the code.

import time
from contextlib import asynccontextmanager


@asynccontextmanager
async def timeit():
    now = time.monotonic()
    try:
        yield
    finally:
        print(f"it took {time.monotonic() - now}s to run")


@timeit()
async def main():
    pass
$ mypy example.py
example.py:14: error: "AsyncContextManager[Any]" not callable
Found 1 error in 1 file (checked 1 source file)

Expected Behavior

The example code should type check without problems on Python 3.10+

Actual Behavior

Mypy complains that the context manager is not callable.

Your Environment

  • Mypy version used: 0.920
  • Mypy command-line flags: None
  • Mypy configuration options from mypy.ini (and other config files): None
  • Python version used: 3.10.1
  • Operating system and version: macOS 10.15.7
ljodal referenced this issue in ljodal/mypy Dec 17, 2021
On Python 3.10+ AsyncContextManager implements __call__
so it can be used as a context manager

Fixes #11783
@ljodal
Copy link
Author

ljodal commented Dec 17, 2021

I realised that this should probably be fixed in the python/typeshed repo first, so I submitted a PR there to fix this.

@JelleZijlstra JelleZijlstra transferred this issue from python/mypy Dec 17, 2021
@ljodal
Copy link
Author

ljodal commented Dec 17, 2021

@JelleZijlstra Looks like I was mistaken, it's in place here: https://github.com/python/typeshed/blob/master/stdlib/contextlib.pyi#L60-L61

@ljodal
Copy link
Author

ljodal commented Dec 17, 2021

But I guess it might have something to do with the inheritance hierarchy in the stubs, because in cpython the return type of asynccontextmanager() inherits from AsyncContextDecorator, but it doesn't look like it does in here

https://github.com/python/cpython/blob/main/Lib/contextlib.py#L189-L193

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant