omnilib / aiomultiprocess

Take a modern Python codebase to the next level of performance.
https://aiomultiprocess.omnilib.dev
MIT License
1.77k stars 101 forks source link

use Iterable[T] instead of Sequence[T] in Pool.map iterable #212

Open fabridamicelli opened 3 months ago

fabridamicelli commented 3 months ago

First off: Thank you very much for this package! :)

Here's a little thing about type hints I found. In essence the Sequece type is too restrictive. The function map can deal as is with any iterable.

Description

The type of the arg iterable should be a Iterable instead of Sequence. Here's pyright hint:

pyright-error

And here's after the fix

pyright-noerror

fabridamicelli commented 3 months ago

Here's an actually running example code:

import asyncio

from aiomultiprocess import Pool

async def f(i: int) -> int:
    await asyncio.sleep(0.1)
    return i

async def main():
    async with Pool() as p:
        vals_seq = [0, 1, 2]
        vals_iter = (i for i in range(3))

        print(await p.map(f, vals_seq))
        print(await p.map(f, vals_iter))

if __name__ == "__main__":
    asyncio.run(main())

pyright-noerror

which outputs:

out