Skip to content

Auto fix for RUF015 is not safe #7019

@jaap3

Description

@jaap3

Using ruff 0.0.286 the following:

bar = (n for n in ())
try:
    foo = list(bar)[0]
except IndexError:
    pass
else:
    print(foo)

is rewritten to:

bar = (n for n in ())
try:
    foo = next(iter(bar))
except IndexError:
    pass
else:
    print(foo)

The rewritten code is now prone to raising an uncaught StopIteration error.

Same thing goes for the rewrite of:

import contextlib

bar = (n for n in ())

with contextlib.suppress(IndexError):
    foo = list(bar)[0]
    print(foo)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions