Skip to content

gh-146601: Improve error message for unparenthesized walrus in keyword arguments - #161963

Open
araooz wants to merge 1 commit into
python:mainfrom
araooz:gh-146601-walrus-kwarg-error
Open

araooz wants to merge 1 commit into
python:mainfrom
araooz:gh-146601-walrus-kwarg-error

Conversation

@araooz

@araooz araooz commented Nov 14, 2026 •

Copy link
Copy Markdown

Before:

>>> f(a=x := 1)
  File "<python-input-0>", line 1
    f(a=x := 1)
          ^^
SyntaxError: invalid syntax

After:

>>> f(a=x := 1)
  File "<python-input-0>", line 1
    f(a=x := 1)
        ^^^^^^
SyntaxError: cannot use named expression without parentheses here

This adds an alternative to invalid_kwarg that matches NAME '=' NAME ':=' expressionand reuses the message already used for assert x := 1. Because invalid_kwarg is used for all keyword arguments, this also covers keywords after positional, *args or **kwargs arguments, and class keywords such as class C(metaclass=x := type).

Unlike the patch suggested in the issue, the rule lives in invalid_kwarg instead of invalid_expression, so the existing messages for other cases (e.g. lambda and conditional expressions) are unchanged. Non-name targets like f(a=x.y := 1) keep the current message, since adding parentheses would not fix them.

Two existing tests in test_named_expressions (invalid_09 and invalid_10) asserted the generic "invalid syntax" message for exactly these inputs; they now expect the new message. New cases were added to test_syntax.

Related: #161927 handles the lambda case (gh-137570) with the same message.

…keyword arguments

Add an invalid_kwarg alternative so that `f(a=x := 1)` reports
"cannot use named expression without parentheses here" instead of
the generic "invalid syntax", reusing the message already used for
`assert x := 1`.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@python-cla-bot

python-cla-bot Bot commented Nov 14, 2026 •

Copy link
Copy Markdown

All commit authors signed the Contributor License Agreement.

CLA signed

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

Labels

awaiting review type-feature A feature request or enhancement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant