Skip to content

gh-138294: Fix Misleading error for ((a) := 1)#138627

Open
dbXD320 wants to merge 5 commits intopython:mainfrom
dbXD320:fix-walrus-paren-error
Open

gh-138294: Fix Misleading error for ((a) := 1)#138627
dbXD320 wants to merge 5 commits intopython:mainfrom
dbXD320:fix-walrus-paren-error

Conversation

@dbXD320
Copy link
Copy Markdown
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
Copy Markdown
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?

Comment thread Grammar/python.gram
@pablogsal
Copy link
Copy Markdown
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 :)

Comment thread Misc/NEWS.d/next/Core_and_Builtins/2025-09-08-12-30-00.gh-issue-138294.abcd12.rst Outdated
…e-138294.abcd12.rst

Co-authored-by: Stan Ulbrych <89152624+StanFromIreland@users.noreply.github.com>
@picnixz picnixz linked an issue Sep 7, 2025 that may be closed by this pull request
@dbXD320
Copy link
Copy Markdown
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
Copy Markdown
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 thread Lib/test/test_syntax.py Outdated
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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!

@github-actions
Copy link
Copy Markdown

This PR is stale because it has been open for 30 days with no activity.

@github-actions github-actions Bot added the stale Stale PR or inactive for long period of time. label Apr 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting core review stale Stale PR or inactive for long period of time.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Misleading error for ((a) := 1)

5 participants