Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Suppress stop-iteration-return on itertools.cycle #7766

Merged
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions doc/whatsnew/fragments/7766.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