Content-Length: 309751 | pFad | http://github.com/astral-sh/ruff/pull/16550

6F [refurb] Fix starred expressions fix (FURB161) by VascoSch92 · Pull Request #16550 · astral-sh/ruff · GitHub
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

[refurb] Fix starred expressions fix (FURB161) #16550

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

VascoSch92
Copy link
Contributor

The PR partially solves issue #16457

Specifically, it solves the following problem:

$ cat >furb161_1.py <<'# EOF'
print(bin(*[123]).count("1"))
# EOF

$ python furb161_1.py
6

$ ruff --isolated check --target-version py310 --preview --select FURB161 furb161_1.py --diff 2>&1 | grep error:
error: Fix introduced a syntax error. Reverting all changes.

Now starred expressions are corrected handled.

Copy link
Contributor

github-actions bot commented Mar 7, 2025

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

@ntBre ntBre added bug Something isn't working fixes Related to suggested fixes for violations labels Mar 7, 2025
@MichaReiser
Copy link
Member

@ntBre could you take a look at this PR

@MichaReiser MichaReiser requested a review from ntBre March 14, 2025 08:27
Copy link
Contributor

@ntBre ntBre left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for doing this! I wrote a longer comment below explaining why I think we should just return on any starred expression, but I'm interested to hear your thoughts.

Comment on lines 114 to 131
// If is a starred expression extract just the value, e.g., `123` in `bin(*[123])`.
let literal_text = match arg {
Expr::Starred(starred) => {
if let Expr::List(list) = &*starred.value {
if list.elts.len() == 1 {
checker.locator().slice(&list.elts[0])
} else {
// If the list is empty or has more than
// one element, we cannot fix it as is a Python error.
return;
}
} else {
// If it is not the unpacking of a list
return;
}
}
_ => checker.locator().slice(arg),
};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I kind of think we should just return immediately if we see a starred expression instead of trying to do this unpacking. We would also need to do this for tuples. However, this only works for the exact case of a literal list or tuple, and I don't think code like *[123] or *(123,) is very likely in real scenarios (you would just write 123 directly). I was going to say we need to handled starred variables, but that's a syntax error:

>>> (*lst).bit_count()
  File "<python-input-7>", line 1
    (*lst).bit_count()
     ^^^^
SyntaxError: cannot use starred expression here
>>>

In short, I think it's better to return early for any starred expression to save us the work of unpacking expressions that should be very rare in practice.

@VascoSch92
Copy link
Contributor Author

Thanks for doing this! I wrote a longer comment below explaining why I think we should just return on any starred expression, but I'm interested to hear your thoughts.

Thank you for the feedback. I don't have strong opinions on that matter. I agree that returning in the case of starred expressions makes perfect sense.

I have updated the code accordingly. Please let me know if it aligns with your suggestion

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working fixes Related to suggested fixes for violations
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: http://github.com/astral-sh/ruff/pull/16550

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy