Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Backport maintenance/2.15.x] Do not crash if next()` is called #7842

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ on:
branches:
- main
- "maintenance/**"
pull_request:
paths-ignore:
- doc/data/messages/**
pull_request: ~

env:
CACHE_VERSION: 1
Expand Down
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