Skip to content

Conversation

@dbXD320
Copy link
Contributor

@dbXD320 dbXD320 commented Sep 7, 2025

This PR fixes a corner-case in assignment expressions (walrus operator :=) where using parentheses around a target name incorrectly compiles, e.g., ((x) := 1).

@dbXD320
Copy link
Contributor Author

dbXD320 commented Sep 7, 2025

@pablogsal @lysnikolaou Hi, I’ve added the grammar change for raising a SyntaxError when a walrus target is parenthesized.
I think the test could go in Lib/test/test_syntax.py, but I’m not entirely sure about the exact location or structure. Could you confirm or suggest a better place?

@pablogsal
Copy link
Member

@pablogsal @lysnikolaou Hi, I’ve added the grammar change for raising a SyntaxError when a walrus target is parenthesized.

I think the test could go in Lib/test/test_syntax.py, but I’m not entirely sure about the exact location or structure. Could you confirm or suggest a better place?

It should go in test_syntax. Find a zone where we test similar errors and add a new one respecting the structure of the other examples.

If you are not sure just put it somewhere you thing is good and we can guide you from there :)

@picnixz picnixz linked an issue Sep 7, 2025 that may be closed by this pull request
@dbXD320
Copy link
Contributor Author

dbXD320 commented Sep 8, 2025

@pablogsal I’ve added the test in test_syntax.py. Please review it and let me know if any adjustments are needed.

Copy link
Member

@lysnikolaou lysnikolaou left a comment

Choose a reason for hiding this comment

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

Thanks a lot @dbXD320! The PR looks good! 🚀

Left just one comment for where I think the test should go.

Comment on lines 2855 to 2859
def test_parenthesized_named_expression_target(self):
self._check_error(
"((a) := 1)",
"cannot parenthesize target name in assignment expression")

Copy link
Member

Choose a reason for hiding this comment

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

I think it'd be best if this went in the docstring at the top of the file instead of a separate test. It can probably go at the very bottom of that docstring with a short description of the error being tested, something along the lines of Invalid parenthesization inside assignment expressions.

Copy link
Member

Choose a reason for hiding this comment

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

@dbXD320 You can add a new section like this:

Invalid expressions in type scopes:

Copy link
Member

@StanFromIreland StanFromIreland left a comment

Choose a reason for hiding this comment

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

LGTM

thanks for applying my patch!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Misleading error for ((a) := 1)

5 participants