From c6db5ecffb794539cb7cb2dd98371b5f9897827f Mon Sep 17 00:00:00 2001 From: Tushar Sadhwani Date: Mon, 14 Nov 2022 15:08:44 +0530 Subject: [PATCH 1/6] Suppress stop-iteration-return on itertools.cycle --- pylint/checkers/refactoring/refactoring_checker.py | 2 +- tests/functional/s/stop_iteration_inside_generator.py | 8 +++++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/pylint/checkers/refactoring/refactoring_checker.py b/pylint/checkers/refactoring/refactoring_checker.py index 412c8c4389..40144c9952 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 084c42058c..b1646fe163 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: From 7af9441693fd5baef458f93715d5ca9a830130e5 Mon Sep 17 00:00:00 2001 From: Tushar Sadhwani Date: Mon, 14 Nov 2022 17:23:08 +0530 Subject: [PATCH 2/6] Add changelog --- doc/whatsnew/fragments/7766.false_positive | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 doc/whatsnew/fragments/7766.false_positive diff --git a/doc/whatsnew/fragments/7766.false_positive b/doc/whatsnew/fragments/7766.false_positive new file mode 100644 index 0000000000..6cdc67417b --- /dev/null +++ b/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 From 49ef939fea7b16fb51758011c6775b9b3d44e7e5 Mon Sep 17 00:00:00 2001 From: Tushar Sadhwani Date: Mon, 14 Nov 2022 17:23:36 +0530 Subject: [PATCH 3/6] Update error location --- tests/functional/s/stop_iteration_inside_generator.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/functional/s/stop_iteration_inside_generator.txt b/tests/functional/s/stop_iteration_inside_generator.txt index c351012b56..99a706a30b 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:140:10:146:35:invalid_object_passed_to_next:Do not raise StopIteration in generator, use return statement instead:INFERENCE From 9dbc0ed2e7eed64a91efc92ead62f3fa800832b0 Mon Sep 17 00:00:00 2001 From: Tushar Sadhwani Date: Mon, 14 Nov 2022 18:26:54 +0530 Subject: [PATCH 4/6] Fix error location properly --- tests/functional/s/stop_iteration_inside_generator.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/functional/s/stop_iteration_inside_generator.txt b/tests/functional/s/stop_iteration_inside_generator.txt index 99a706a30b..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:146: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 From 346450052b6cf5ec100c5d4f440c4bcc4dd7a560 Mon Sep 17 00:00:00 2001 From: Pierre Sassoulas Date: Mon, 14 Nov 2022 14:02:46 +0100 Subject: [PATCH 5/6] Update doc/whatsnew/fragments/7766.false_positive --- doc/whatsnew/fragments/7766.false_positive | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/whatsnew/fragments/7766.false_positive b/doc/whatsnew/fragments/7766.false_positive index 6cdc67417b..de7c44c5af 100644 --- a/doc/whatsnew/fragments/7766.false_positive +++ b/doc/whatsnew/fragments/7766.false_positive @@ -1,3 +1,3 @@ -Don't warn about ``stop-iteration-return`` when using ``next()`` over ``itertools.cycle`` +Don't warn about ``stop-iteration-return`` when using ``next()`` over ``itertools.cycle``. Closes #7765 From 3a05b090400946e5fa98b22502782af91ab550e8 Mon Sep 17 00:00:00 2001 From: Pierre Sassoulas Date: Mon, 14 Nov 2022 14:16:43 +0100 Subject: [PATCH 6/6] Rename 7766.false_positive to 7765.false_positive --- .../fragments/{7766.false_positive => 7765.false_positive} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename doc/whatsnew/fragments/{7766.false_positive => 7765.false_positive} (100%) diff --git a/doc/whatsnew/fragments/7766.false_positive b/doc/whatsnew/fragments/7765.false_positive similarity index 100% rename from doc/whatsnew/fragments/7766.false_positive rename to doc/whatsnew/fragments/7765.false_positive