diff options
author | Tushar Sadhwani <86737547+tushar-deepsource@users.noreply.github.com> | 2022-11-14 20:59:46 +0530 |
---|---|---|
committer | Pierre Sassoulas <pierre.sassoulas@gmail.com> | 2022-11-17 14:03:15 +0100 |
commit | 9df5e8a46174a7ce621947e9f5b403dc8bdbfa65 (patch) | |
tree | 07774d4d4a60dae082df6e4f781481a350009e04 | |
parent | fa618cb7ce74a423e4be8a5da7dedc3c3fd6d383 (diff) | |
download | pylint-git-9df5e8a46174a7ce621947e9f5b403dc8bdbfa65.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 2d4d632c4..ca23a5c66 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 945a952bd..29db3d7ae 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 |