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

RUF015 auto fix changes behavior #6148

Closed
jc-louis opened this issue Jul 28, 2023 · 1 comment · Fixed by #6150
Closed

RUF015 auto fix changes behavior #6148

jc-louis opened this issue Jul 28, 2023 · 1 comment · Fixed by #6150
Labels
bug Something isn't working

Comments

@jc-louis
Copy link

Using ruff v0.0.280 with --fix the rule RUF015 changes code behavior:

# before RUF015 auto fix
def fct(a: list, b: list):
    return a + [ele for ele in b if ele][:1]


print(fct([1], [0]))  # [1]


# after RUF015 auto fix
def fct(a: list, b: list):
    return [*a, next(ele for ele in b if ele)]


print(fct([1], [0]))  # raises StopIteration
@charliermarsh charliermarsh added the bug Something isn't working label Jul 28, 2023
@charliermarsh
Copy link
Member

We should probably remove the slice cases here.

charliermarsh added a commit that referenced this issue Jul 28, 2023
## Summary

Right now, `RUF015` will try to rewrite `x[:1]` as `[next(x)]`. This
isn't equivalent if `x`, for example, is empty, where slicing like
`x[:1]` is forgiving, but `next` raises `StopIteration`. For me this is
a little too much of a deviation to be comfortable with, and most of the
value in this rule is the `x[0]` to `next(x)` conversion anyway.

Closes #6148.
LaBatata101 pushed a commit to LaBatata101/ruff that referenced this issue Jul 28, 2023
## Summary

Right now, `RUF015` will try to rewrite `x[:1]` as `[next(x)]`. This
isn't equivalent if `x`, for example, is empty, where slicing like
`x[:1]` is forgiving, but `next` raises `StopIteration`. For me this is
a little too much of a deviation to be comfortable with, and most of the
value in this rule is the `x[0]` to `next(x)` conversion anyway.

Closes astral-sh#6148.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants