-
Notifications
You must be signed in to change notification settings - Fork 1.3k
[syntax-errors] detect single starred expression assignment x = *y
#17624
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
Conversation
@ntBre could you review? |
|
|
||
| | ||
1 | _ = *[42] | ||
| ^^^^^ Syntax Error: can't use starred expression here |
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.
Let's use the same convention for the message as other similar syntax errors: "Starred expression cannot be used here" (cc @ntBre)
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.
Oh, that is actually what we used for the existing InvalidStarExpression
variant, so that's my bad. I think the closest to the other variants would be something like "cannot use a starred expression here," but the current value is identical to CPython.
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 don't think we necessarily need to have the same message as CPython.
I think the closest to the other variants would be something like "cannot use a starred expression here,"
Oh interesting. IIRC, the parser has messages like "Yield expressions cannot be used here", etc. which is where my recommendation comes from.
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.
IIRC, the parser has messages like "Yield expressions cannot be used here"
There's also the exact message you mentioned above for star expressions:
ruff/crates/ruff_python_parser/src/error.rs
Lines 229 to 231 in bc0a5aa
ParseErrorType::InvalidStarredExpressionUsage => { | |
f.write_str("Starred expression cannot be used here") | |
} |
Should I change the syntax error to this message then?
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.
Hi, bumping this once again.
It is not clear to me whether the message needs to be changed or not.
(Although changing makes sense to me, happy to do that).
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'd recommend changing the message to "Starred expression cannot be used here" to be consistent with what the parser emits and other similar messages emitted by the Ruff parser. It'd be useful if that change could be done in a separate PR but if that's too much to ask, it's fine to include it in this PR itself.
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.
Opened #17772 for the message change.
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, this is looking good. I think we can revert at least one of the snapshot updates and add a couple more interesting test_ok
cases, but otherwise this looks good.
Let's follow up with a small PR updating the error message for this variant, as Dhruv suggested. I think it's okay to land this first since it's just reusing the current message.
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 think we can just leave this file alone. It doesn't look like the snapshots changed at all, so I don't think it's worth renaming the file or updating the tests, unless I'm missing something. They don't even trigger the new semantic error, right?
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.
They do trigger the new rule.
257 │+1 | x = *a and b
258 │+ | ^^^^^^^^ Syntax Error: can't use starred expression here
259 │+2 | x = *yield x
260 │+3 | x = *yield from x
261 │+ |
262 │+
263 │+
264 │+ |
265 │+1 | x = *a and b
266 │+2 | x = *yield x
267 │+ | ^^^^^^^^ Syntax Error: can't use starred expression here
268 │+3 | x = *yield from x
269 │+4 | x = *lambda x: x
I had removed the assignments so that they do not trigger it.
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 think that would be changing what the test was suppose to do. It's suppose to test that a starred expression involved in an assignment uses the correct precedence which is bitwise_or
as per the grammar. We should retain this test but update it such that it still maintains what the original intent was. Now that the star expressions are not allowed at the top level, we can update the code to include the star expressions inside a list / tuple. For example:
x = (*a and b,)
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.
Ah, my mistake for not seeing that these would be affected. Thanks Dhruv for clarifying the intent of the tests, that looks like a great suggestion.
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 the explanation! Makes sense. For the non-yield
test cases, I followed the pattern you mentioned, putting the expression inside a list.
For the yield
test cases, I had to create a two-element tuple, because:
- Changing to
(*yield x,)
is not good enough, since this is still a "Starred expression" as a whole as per the AST - Changing to
((*yield x),)
triggers another of the "Starred expression cannot be used here" errors, namely this one:
Lines 62 to 64 in f11d9cb
1 | # Starred expression isn't allowed in a parenthesized expression. 2 | (*x) | ^^ Syntax Error: Starred expression cannot be used here
To avoid this being triggered, I went with a two-element tuple -- (42, *yield from x)
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 following up, that works.
d68b486
to
10e45ae
Compare
Addressed two of the review comments, but Brent, please see the unresolved thread at #17624 (comment). As you suggested, I'll make a follow up PR to edit the messages once this one is in. |
10e45ae
to
44bc77a
Compare
Thanks a lot for the reviews! I have addressed all the comments and pushed. |
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.
Looks good, I've mainly looked at the test cases. I'll leave the final review to @ntBre
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.
Looks great, thank you!
* main: [red-knot] Preliminary `NamedTuple` support (#17738) [red-knot] Add tests for classes that have incompatible `__new__` and `__init__` methods (#17747) Update dependency vite to v6.2.7 (#17746) [red-knot] Update call binding to return all matching overloads (#17618) [`airflow`] apply Replacement::AutoImport to `AIR312` (#17570) [`ruff`] Add fix safety section (`RUF028`) (#17722) [syntax-errors] Detect single starred expression assignment `x = *y` (#17624) py-fuzzer: fix minimization logic when `--only-new-bugs` is passed (#17739) Fix example syntax for pydocstyle ignore_var_parameters option (#17740) [red-knot] Update salsa to prevent panic in custom panic-handler (#17742) [red-knot] Ban direct instantiation of generic protocols as well as non-generic ones (#17741) [red-knot] Lookup of `__new__` (#17733) [red-knot] Check decorator consistency on overloads (#17684) [`flake8-use-pathlib`] Avoid suggesting `Path.iterdir()` for `os.listdir` with file descriptor (`PTH208`) (#17715) [red-knot] Check overloads without an implementation (#17681) Expand Semantic Syntax Coverage (#17725) [red-knot] Check for invalid overload usages (#17609)
Summary
Part of #17412
Starred expressions cannot be used as values in assignment expressions.
Add a new semantic syntax error to catch such instances.
Note that we already have
ParseErrorType::InvalidStarredExpressionUsage
to catch some starred expression errors during parsing, but that does not cover top level assignment expressions.Test Plan
_ = *data
), which are not really valid (per this new rule) and updated themassign_stmt_invalid_value_expr
which had instances of*
expression which would be deemed invalid by this new rule. Converted these to tuples, so that they do not trigger this new rule.