Skip to content

Commit 9df5e8a

Browse files
Suppress stop-iteration-return on itertools.cycle (#7766)
Co-authored-by: Pierre Sassoulas <[email protected]>
1 parent fa618cb commit 9df5e8a

File tree

4 files changed

+12
-3
lines changed

4 files changed

+12
-3
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Don't warn about ``stop-iteration-return`` when using ``next()`` over ``itertools.cycle``.
2+
3+
Closes #7765

pylint/checkers/refactoring/refactoring_checker.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
nodes.TryExcept, nodes.TryFinally, nodes.While, nodes.For, nodes.If
3636
]
3737

38-
KNOWN_INFINITE_ITERATORS = {"itertools.count"}
38+
KNOWN_INFINITE_ITERATORS = {"itertools.count", "itertools.cycle"}
3939
BUILTIN_EXIT_FUNCS = frozenset(("quit", "exit"))
4040
CALLS_THAT_COULD_BE_REPLACED_BY_WITH = frozenset(
4141
(

tests/functional/s/stop_iteration_inside_generator.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -110,14 +110,20 @@ def gen_next_with_sentinel():
110110
yield next([], 42) # No bad return
111111

112112

113-
from itertools import count
113+
from itertools import count, cycle
114114

115115
# https://github.com/PyCQA/pylint/issues/2158
116116
def generator_using_next():
117117
counter = count()
118118
number = next(counter)
119119
yield number * 2
120120

121+
# https://github.com/PyCQA/pylint/issues/7765
122+
def infinite_iterator_itertools_cycle():
123+
counter = cycle('ABCD')
124+
val = next(counter)
125+
yield val
126+
121127

122128
# pylint: disable=too-few-public-methods
123129
class SomeClassWithNext:

tests/functional/s/stop_iteration_inside_generator.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ stop-iteration-return:44:14:44:21:gen_next_raises_stopiter:Do not raise StopIter
44
stop-iteration-return:66:18:66:25:gen_next_inside_wrong_try_except:Do not raise StopIteration in generator, use return statement instead:INFERENCE
55
stop-iteration-return:80:12:80:31:gen_next_inside_wrong_try_except2:Do not raise StopIteration in generator, use return statement instead:INFERENCE
66
stop-iteration-return:97:18:97:25:gen_dont_crash_on_no_exception:Do not raise StopIteration in generator, use return statement instead:INFERENCE
7-
stop-iteration-return:140:10:140:35:invalid_object_passed_to_next:Do not raise StopIteration in generator, use return statement instead:INFERENCE
7+
stop-iteration-return:146:10:146:35:invalid_object_passed_to_next:Do not raise StopIteration in generator, use return statement instead:INFERENCE

0 commit comments

Comments
 (0)