sourcery-ai / sourcery

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

use-itertools-product changes functionality involving infinite generators #281

Closed ronnodas closed 1 year ago

ronnodas commented 1 year ago

Description

The following code terminates before the refactoring but not after:

from itertools import count, product

def loop():
    for x in count():
        for y in range(5):
            if x == 3:
                return y

print(loop())

This is because itertools.product tries to exhaust the iterators before outputting anything, so does not work with infinite iterators, as in https://github.com/python/cpython/issues/54318.

I'm really not sure what the right thing to do is, since it's a useful refactoring in most cases. Maybe the safe solution is to only trigger if each of the iterables is known to be a finite iterator (eg list, tuple, range, ...)?

Debug Information

IDE Version: PyCharm Professional Edition 2022.2.1

Sourcery Version: Sourcery 0.12.8

Operating system and Version: Fedora Linux 36

Hellebore commented 1 year ago

Thanks for raising! Yeah this is a tricky one to restrict without cutting out a bunch of the useful cases. Will have a think.

Hellebore commented 1 year ago

Fixed with 0.12.9