From c9953064eefe7a0294b9bb5b90ffc2865963fe00 Mon Sep 17 00:00:00 2001 From: Tushar Sadhwani <86737547+tushar-deepsource@users.noreply.github.com> Date: Mon, 14 Nov 2022 20:59:46 +0530 Subject: [PATCH] Suppress `stop-iteration-return` on `itertools.cycle` (#7766) Co-authored-by: Pierre Sassoulas --- doc/whatsnew/fragments/7765.false_positive | 3 +++ pylint/checkers/refactoring/refactoring_checker.py | 2 +- tests/functional/s/stop_iteration_inside_generator.py | 8 +++++++- tests/functional/s/stop_iteration_inside_generator.txt | 2 +- 4 files changed, 12 insertions(+), 3 deletions(-) create mode 100644 doc/whatsnew/fragments/7765.false_positive diff --git a/doc/whatsnew/fragments/7765.false_positive b/doc/whatsnew/fragments/7765.false_positive new file mode 100644 index 0000000000..de7c44c5af --- /dev/null +++ b/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 diff --git a/pylint/checkers/refactoring/refactoring_checker.py b/pylint/checkers/refactoring/refactoring_checker.py index 2d4d632c4d..ca23a5c663 100644 --- a/pylint/checkers/refactoring/refactoring_checker.py +++ b/pylint/checkers/refactoring/refactoring_checker.py @@ -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( ( diff --git a/tests/functional/s/stop_iteration_inside_generator.py b/tests/functional/s/stop_iteration_inside_generator.py index 945a952bd0..29db3d7aed 100644 --- a/tests/functional/s/stop_iteration_inside_generator.py +++ b/tests/functional/s/stop_iteration_inside_generator.py @@ -110,7 +110,7 @@ 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(): @@ -118,6 +118,12 @@ def generator_using_next(): 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: diff --git a/tests/functional/s/stop_iteration_inside_generator.txt b/tests/functional/s/stop_iteration_inside_generator.txt index c351012b56..f20351c5da 100644 --- a/tests/functional/s/stop_iteration_inside_generator.txt +++ b/tests/functional/s/stop_iteration_inside_generator.txt @@ -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