aiofiles icon indicating copy to clipboard operation
aiofiles copied to clipboard

Support for type hints

Open zealws opened this issue 7 years ago • 12 comments

It'd be really nice to be able to use mypy against files that include aiofiles, but that's not possible now since aiofiles isn't typed.

A simple example shows that mypy --strict doesn't allow use of aiofiles.open:

$ mypy --strict main.py 
main.py:13: error: Call to untyped function "open" in typed context

Here's the code:

#!/usr/bin/env python
import aiofiles
import asyncio

def main() -> None:
    el = asyncio.get_event_loop()
    el.run_until_complete(async_main())

async def async_main() -> None:
    async with aiofiles.open('test_aiofiles', 'wb') as f:
        await f.write(b'Hello, world!\n')

if __name__ == '__main__':
    main()

zealws avatar Sep 07 '18 18:09 zealws

Feel like submitting a pull request? You'd need to use the old typing style (e.g. in comments) but it shouldn't be too difficult.

Tinche avatar Sep 10 '18 13:09 Tinche

@Tinche Python has supported function annotations since https://www.python.org/dev/peps/pep-3107/

graingert avatar Oct 10 '18 12:10 graingert

As long as the tests pass on our current platforms, I'll merge it in :)

Tinche avatar Oct 10 '18 15:10 Tinche

Mitigations addressed in the Debian bug report:

https://bugs.debian.org/994932

davesteele avatar Sep 24 '21 16:09 davesteele

Hm looks like someone wrote stubs for us?

pip install types-aiofiles

Would this be enough to close this issue?

Tinche avatar Sep 24 '21 19:09 Tinche

It would be more convenient if you merged them in and added a py.typed

graingert avatar Sep 24 '21 20:09 graingert

Yes, that is enough.

davesteele avatar Sep 28 '21 12:09 davesteele

types-aiofiles is a nice first step but rather incomplete

vbraun avatar Apr 18 '22 12:04 vbraun

bump

cpvandehey avatar Apr 10 '23 23:04 cpvandehey