Skip to content

Specialize syntax error for assignment expressions in invalid places #146601

Open

Description

@brianschubert

Feature or enhancement

Proposal:

There are a few places where (unparenthesized) assignment expressions are not accepted but users might think of using them. For example:

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

I find this error pretty confusing. It's not clear why this is a syntax error or what users should do it fix it (i.e. add parentheses).

I propose adding a specialized syntax error to point users in the right direction, similar to what's currently done for assert:

>>> assert x := 1
  File "<python-input-12>", line 1
    assert x := 1
           ^^^^^^
SyntaxError: cannot use named expression without parentheses here

Refs #138716, #146260

Has this already been discussed elsewhere?

This is a minor feature, which does not need previous discussion elsewhere

Links to previous discussion of this feature:

No response

Linked PRs

Activity

  1. brianschubert commented on Mar 29, 2026

    @brianschubert
    ContributorAuthor

    I tried implementing this with this patch:

    diff --git a/Grammar/python.gram b/Grammar/python.gram
    index 3a91d426c36..75ebf0dc7e3 100644
    --- a/Grammar/python.gram
    +++ b/Grammar/python.gram
    @@ -1273,6 +1273,7 @@ invalid_expression:
             RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "f-string: lambda expressions are not allowed without parentheses") }
        | a='lambda' [lambda_params] b=':' &TSTRING_MIDDLE  {
             RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "t-string: lambda expressions are not allowed without parentheses") }
    +   | a=NAME ':=' b=expression { RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "assignment expression must be parenthesized here") }
     
     invalid_if_expression:
        | disjunction 'if' b=disjunction 'else' a='*' {

    This works well, but I worry it might be overly broad. There's a few invalid cases where this overrides the existing error:

    lambda: x:=1
    1 if True else x := 1

    These currently report:

      File "<python-input-0>", line 1
        lambda: x:=1
        ^^^^^^^^^
    SyntaxError: cannot use assignment expressions with lambda
    
      File "<python-input-1>", line 1
        1 if True else x := 1
        ^^^^^^^^^^^^^^^^
    SyntaxError: cannot use assignment expressions with conditional expression

    But with the above patch would report:

    >>> lambda: x:=1
      File "<python-input-0>", line 1
        lambda: x:=1
                ^^^^
    SyntaxError: assignment expression must be parenthesized here
    >>> 1 if True else x := 1
      File "<python-input-1>", line 1
        1 if True else x := 1
                       ^^^^^^
    SyntaxError: assignment expression must be parenthesized here

    I think this is arguably an improvement (it's more likely that the user intended an inner assignment expression than assigning to a non-identifier LHS). But I am little hesitant, since this isn't actually representative of the first pass parser state. (Note that this only applies to sub expressions that start with an assignment expression; cases like lambda: x and y:=1 are unaffected).

    This also wouldn't help in a handful of places that don't take an expression (for example, 1 if x:=1 else 2 and [a for a in x:=1]).

    @pablogsal @lysnikolaou do you think the changes to the existing syntax errors shown above would be acceptable? If so I can open a PR to show what the broader impact might look like.

  2. araooz commented on Nov 14, 2026

    @araooz

    I opened #161963, which covers the keyword-argument case from the original report
    (f(arg=x := foo())).

    @brianschubert I took a narrower approach than your patch: the rule is in invalid_kwarg rather than invalid_expression, so the existing lambda and conditional-expression messages stay as they are, which avoids the question you raised. The lambda case is being handled separately in #161927 (gh-137570), and the other places listed in the docs (with statements, assignment statements, comprehension-if) could be handled in follow-ups.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions