-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
base: main
Are you sure you want to change the base?
Conversation
|
@ntBre could you take a look at this PR |
There was a problem hiding this 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.
// 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), | ||
}; |
There was a problem hiding this comment.
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.
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 |
The PR partially solves issue #16457
Specifically, it solves the following problem:
Now starred expressions are corrected handled.