sourcery
sourcery copied to clipboard
use-itertools-product changes functionality involving infinite generators
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
Thanks for raising! Yeah this is a tricky one to restrict without cutting out a bunch of the useful cases. Will have a think.
Fixed with 0.12.9