Skip to content

[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

Merged
merged 4 commits into from
Apr 30, 2025

Conversation

abhijeetbodas2001
Copy link
Contributor

@abhijeetbodas2001 abhijeetbodas2001 commented Apr 25, 2025

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

  • Added new inline tests for the new rule
  • Found some examples marked as "valid" in existing tests (_ = *data), which are not really valid (per this new rule) and updated them
  • There was an existing inline test - assign_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.

@abhijeetbodas2001
Copy link
Contributor Author

@ntBre could you review?

Copy link
Contributor

github-actions bot commented Apr 25, 2025

ruff-ecosystem results

Linter (stable)

ℹ️ ecosystem check encountered linter errors. (no lint changes; 1 project error)

bokeh/bokeh (error)

ruff check --no-cache --exit-zero --ignore RUF9 --no-fix --output-format concise --no-preview --select ALL

Failed to clone bokeh/bokeh: error: 3300 bytes of body are still expected
fetch-pack: unexpected disconnect while reading sideband packet
fatal: early EOF
fatal: fetch-pack: invalid index-pack output

Linter (preview)

✅ ecosystem check detected no linter changes.

Formatter (stable)

✅ ecosystem check detected no format changes.

Formatter (preview)

✅ ecosystem check detected no format changes.

@MichaReiser MichaReiser requested a review from ntBre April 25, 2025 06:41
@ntBre ntBre self-assigned this Apr 25, 2025

|
1 | _ = *[42]
| ^^^^^ Syntax Error: can't use starred expression here
Copy link
Member

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)

Copy link
Contributor

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.

Copy link
Member

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.

Copy link
Contributor Author

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:

ParseErrorType::InvalidStarredExpressionUsage => {
f.write_str("Starred expression cannot be used here")
}

Should I change the syntax error to this message then?

Copy link
Contributor Author

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).

Copy link
Member

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.

Copy link
Contributor Author

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.

@MichaReiser MichaReiser added the preview Related to preview mode features label Apr 28, 2025
@MichaReiser MichaReiser removed their request for review April 28, 2025 06:45
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, 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.

Copy link
Contributor

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?

Copy link
Contributor Author

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.

Copy link
Member

@dhruvmanila dhruvmanila Apr 29, 2025

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,)

Copy link
Contributor

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.

Copy link
Contributor Author

@abhijeetbodas2001 abhijeetbodas2001 Apr 30, 2025

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:

To avoid this being triggered, I went with a two-element tuple -- (42, *yield from x)

Copy link
Member

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.

@abhijeetbodas2001
Copy link
Contributor Author

abhijeetbodas2001 commented Apr 29, 2025

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.

@abhijeetbodas2001
Copy link
Contributor Author

Thanks a lot for the reviews! I have addressed all the comments and pushed.

Copy link
Member

@dhruvmanila dhruvmanila left a 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

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.

Looks great, thank you!

@ntBre ntBre merged commit 0eeb02c into astral-sh:main Apr 30, 2025
34 checks passed
dcreager added a commit that referenced this pull request May 1, 2025
* 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)
@abhijeetbodas2001 abhijeetbodas2001 deleted the syntax-error branch May 1, 2025 05:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
preview Related to preview mode features
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants