From fc146c70029fc94eafc70497747c8c5201780a22 Mon Sep 17 00:00:00 2001 From: clavedeluna Date: Wed, 23 Nov 2022 09:25:35 -0300 Subject: [PATCH] do not cras if next()` is called --- doc/whatsnew/fragments/7828.bugfix | 3 +++ pylint/checkers/refactoring/refactoring_checker.py | 5 +++++ tests/functional/s/stop_iteration_inside_generator.py | 10 ++++++++++ 3 files changed, 18 insertions(+) create mode 100644 doc/whatsnew/fragments/7828.bugfix diff --git a/doc/whatsnew/fragments/7828.bugfix b/doc/whatsnew/fragments/7828.bugfix new file mode 100644 index 0000000000..f47cc3cc95 --- /dev/null +++ b/doc/whatsnew/fragments/7828.bugfix @@ -0,0 +1,3 @@ +Fixes a crash in ``stop-iteration-return`` when the ``next`` builtin is called without arguments. + +Closes #7828 diff --git a/pylint/checkers/refactoring/refactoring_checker.py b/pylint/checkers/refactoring/refactoring_checker.py index ec68099a6b..9cc316649f 100644 --- a/pylint/checkers/refactoring/refactoring_checker.py +++ b/pylint/checkers/refactoring/refactoring_checker.py @@ -1135,6 +1135,11 @@ def _looks_like_infinite_iterator(param: nodes.NodeNG) -> bool: # A next() method, which is now what we want. return + if len(node.args) == 0: + # handle case when builtin.next is called without args. + # see https://github.com/PyCQA/pylint/issues/7828 + return + inferred = utils.safe_infer(node.func) if ( diff --git a/tests/functional/s/stop_iteration_inside_generator.py b/tests/functional/s/stop_iteration_inside_generator.py index a143fd946c..efde61a77a 100644 --- a/tests/functional/s/stop_iteration_inside_generator.py +++ b/tests/functional/s/stop_iteration_inside_generator.py @@ -175,3 +175,13 @@ def next(*things): it = iter(it) while True: yield next(1, 2) + +def data(filename): + """ + Ensure pylint doesn't crash if `next` is incorrectly called without args + See https://github.com/PyCQA/pylint/issues/7828 + """ + with open(filename, encoding="utf8") as file: + next() # attempt to skip header but this is incorrect code + for line in file: + yield line