diff options
author | Tushar Sadhwani <86737547+tushar-deepsource@users.noreply.github.com> | 2022-11-14 20:59:46 +0530 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-11-14 16:29:46 +0100 |
commit | 6de674ced02985a2fa23a8e46c11a5328ea117be (patch) | |
tree | b9d345fdceb1a5b61a9c2cedceb9cc9483460741 | |
parent | fdd8f1892799c6e8c4aa070d3031c215a77bd1f1 (diff) | |
download | pylint-git-6de674ced02985a2fa23a8e46c11a5328ea117be.tar.gz |
Suppress `stop-iteration-return` on `itertools.cycle` (#7766)
Co-authored-by: Pierre Sassoulas <pierre.sassoulas@gmail.com>
4 files changed, 12 insertions, 3 deletions
diff --git a/doc/whatsnew/fragments/7765.false_positive b/doc/whatsnew/fragments/7765.false_positive new file mode 100644 index 000000000..de7c44c5a --- /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 412c8c438..40144c995 100644 --- a/pylint/checkers/refactoring/refactoring_checker.py +++ b/pylint/checkers/refactoring/refactoring_checker.py @@ -35,7 +35,7 @@ NodesWithNestedBlocks = Union[ 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 084c42058..b1646fe16 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 c351012b5..f20351c5d 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 |