Skip to content

Commit

Permalink
Do not crash if next() is called without arguments (#7831)
Browse files Browse the repository at this point in the history
Co-authored-by: Pierre Sassoulas <[email protected]>
(cherry picked from commit a9c1cda)
  • Loading branch information
clavedeluna authored and Pierre-Sassoulas committed Nov 24, 2022
1 parent ac2da87 commit 9ec1aa0
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 0 deletions.
3 changes: 3 additions & 0 deletions doc/whatsnew/fragments/7828.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fixes a crash in ``stop-iteration-return`` when the ``next`` builtin is called without arguments.

Closes #7828
5 changes: 5 additions & 0 deletions pylint/checkers/refactoring/refactoring_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
10 changes: 10 additions & 0 deletions tests/functional/s/stop_iteration_inside_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 9ec1aa0

Please sign in to comment.