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
Error with Protocols and AsyncContextManager #8276
Comments
Not sure about the error on line 29, but the error from line 18 at least seems to be a legitimate one -- you need to use
|
Sorry, I forgot about it, fixed in my example |
It looks like the type annotations were incorrect and the errors were legitimate. This version passes without errors (I'm not 100% sure if the annotations are correct here, though): import asyncio
from contextlib import asynccontextmanager
from typing import AsyncGenerator, AsyncContextManager
from typing_extensions import Protocol
class LockProto(Protocol):
def with_lock(self) -> AsyncContextManager[str]: ...
class Consumer:
def __init__(self, locker: LockProto) -> None:
self.locker = locker
async def run(self) -> None:
async with self.locker.with_lock() as name:
print(name)
class SockLock:
@asynccontextmanager
async def with_lock(self) -> AsyncGenerator[str, None]:
yield 'some-string'
async def main() -> None:
cons = Consumer(SockLock())
await cons.run()
if __name__ == '__main__':
asyncio.run(main()) Changes I made:
I wonder if some error messages could be improved here? |
Thanks, moving away |
Yeah, |
https://mypy.readthedocs.io/en/stable/kinds_of_types.html is it a good place for such kind of examples? Or at least just to put them in the examples https://mypy.readthedocs.io/en/stable/cheat_sheet_py3.html#miscellaneous |
I'd say that appending to https://mypy.readthedocs.io/en/stable/kinds_of_types.html is a good place for |
I'm taking this issue |
@JukkaL where should I add |
@joybhallaa I would create a new section called "Context Managers". |
Are you reporting a bug, or opening a feature request?
A bug
Please insert below the code you are checking with mypy,
What is the behavior/output you expect?
No errors
What are the versions of mypy and Python you are using?
0.761
What are the mypy flags you are using? (For example --strict-optional)
No flags, just
mypy bug.py
The text was updated successfully, but these errors were encountered: