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 ca23a5c663..87b8316092 100644 --- a/pylint/checkers/refactoring/refactoring_checker.py +++ b/pylint/checkers/refactoring/refactoring_checker.py @@ -1134,6 +1134,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 29db3d7aed..f543f40ee1 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