Skip to content

Fix IndexError on empty Generator subscripts - #11382

Closed
iam-kira wants to merge 1 commit into
pylint-dev:mainfrom
iam-kira:fix/11357-empty-tuple-subscript-crash
Closed

iam-kira wants to merge 1 commit into
pylint-dev:mainfrom
iam-kira:fix/11357-empty-tuple-subscript-crash

Conversation

@iam-kira

@iam-kira iam-kira commented Sep 4, 2026 •

Copy link
Copy Markdown
Contributor

Type of Changes

Type
✓ 🐛 Bug fix

Description

Closes #11357

Generator[()] gives a Tuple slice with no elements. The guard checks
all(... for el in node.slice.elts[1:]), which is vacuously true for an empty
list, so the suggestion built immediately after indexes node.slice.elts[0] and
raises IndexError.

Single-element subscripts like Generator[int] produce a Name slice rather than a
Tuple, so the empty tuple is the only reachable case — a non-empty check is the
complete fix.

Testing

  • Added ca.Generator[()] / ca.AsyncGenerator[()] to the existing
    unnecessary_default_type_args functional test; it fails with IndexError when
    the fix is reverted
  • The checker still flags Generator[int, None, None] correctly

`Generator[()]` gives a Tuple slice with no elements. The guard in
visit_subscript checks `all(... for el in node.slice.elts[1:])`, which is
vacuously true for an empty list, so the suggestion built immediately after
indexed `node.slice.elts[0]` and raised IndexError.

Require a non-empty slice. Single-element subscripts like `Generator[int]`
were already excluded, since those produce a Name slice rather than a Tuple,
so the empty tuple was the only reachable case.

Closes pylint-dev#11357
@iam-kira
iam-kira requested a review from cdce8p as a code owner September 4, 2026 06:23
@Pierre-Sassoulas

Copy link
Copy Markdown
Member

Thank you for working on pylint, this is already fixed in #11375

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

IndexError with missing Generator arguments

2 participants