sourcery-ai / sourcery

Instant AI code reviews
https://sourcery.ai
MIT License
1.53k stars 66 forks source link

Wrongly replacement of a manual counter in an async loop #234

Closed movabo closed 2 years ago

movabo commented 2 years ago

Issue description or question

With async for loops and manual counters, sourcery wrongly recommends replacing them with enumerates.

Minimal reproducible example:

import asyncio

async def test():
  for i in range(10):
    yield f"-{i}-"
    await asyncio.sleep(1)

async def main():
  print('running')
  i = 0
  async for x in test():
    i += 1
    print(x, i)
  print('finished')

asyncio.run(main())

Suggestion by sourcery:

Function main refactored with the following changes:

- i = 0
- async for x in test():
-     i += 1
+ async for i, x in enumerate(test(), start=1):

However, enumerate does not support async methods.

Sourcery Version

0.11.2

Code editor or IDE name and version

Pycharm Build #PY-221.5080.212, built on April 12, 2022

OS name and version

Linux 5.14.17-051417-generic x86_64/Ubuntu 21.10

brendanator commented 2 years ago

Thanks for reporting this @movabo!

I have a fix and it will be included in the next release

Hellebore commented 2 years ago

Fixed in 0.11.4