Skip to content

Commit

Permalink
Suppress stop-iteration-return on itertools.cycle (#7766)
Browse files Browse the repository at this point in the history
Co-authored-by: Pierre Sassoulas <pierre.sassoulas@gmail.com>
  • Loading branch information
tushar-deepsource and Pierre-Sassoulas committed Nov 17, 2022
1 parent fa618cb commit 9df5e8a
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 3 deletions.
3 changes: 3 additions & 0 deletions doc/whatsnew/fragments/7765.false_positive
@@ -0,0 +1,3 @@
Don't warn about ``stop-iteration-return`` when using ``next()`` over ``itertools.cycle``.

Closes #7765
2 changes: 1 addition & 1 deletion pylint/checkers/refactoring/refactoring_checker.py
Expand Up @@ -35,7 +35,7 @@
nodes.TryExcept, nodes.TryFinally, nodes.While, nodes.For, nodes.If
]

KNOWN_INFINITE_ITERATORS = {"itertools.count"}
KNOWN_INFINITE_ITERATORS = {"itertools.count", "itertools.cycle"}
BUILTIN_EXIT_FUNCS = frozenset(("quit", "exit"))
CALLS_THAT_COULD_BE_REPLACED_BY_WITH = frozenset(
(
Expand Down
8 changes: 7 additions & 1 deletion tests/functional/s/stop_iteration_inside_generator.py
Expand Up @@ -110,14 +110,20 @@ def gen_next_with_sentinel():
yield next([], 42) # No bad return


from itertools import count
from itertools import count, cycle

# https://github.com/PyCQA/pylint/issues/2158
def generator_using_next():
counter = count()
number = next(counter)
yield number * 2

# https://github.com/PyCQA/pylint/issues/7765
def infinite_iterator_itertools_cycle():
counter = cycle('ABCD')
val = next(counter)
yield val


# pylint: disable=too-few-public-methods
class SomeClassWithNext:
Expand Down
2 changes: 1 addition & 1 deletion tests/functional/s/stop_iteration_inside_generator.txt
Expand Up @@ -4,4 +4,4 @@ stop-iteration-return:44:14:44:21:gen_next_raises_stopiter:Do not raise StopIter
stop-iteration-return:66:18:66:25:gen_next_inside_wrong_try_except:Do not raise StopIteration in generator, use return statement instead:INFERENCE
stop-iteration-return:80:12:80:31:gen_next_inside_wrong_try_except2:Do not raise StopIteration in generator, use return statement instead:INFERENCE
stop-iteration-return:97:18:97:25:gen_dont_crash_on_no_exception:Do not raise StopIteration in generator, use return statement instead:INFERENCE
stop-iteration-return:140:10:140:35:invalid_object_passed_to_next:Do not raise StopIteration in generator, use return statement instead:INFERENCE
stop-iteration-return:146:10:146:35:invalid_object_passed_to_next:Do not raise StopIteration in generator, use return statement instead:INFERENCE

0 comments on commit 9df5e8a

Please sign in to comment.