Skip to content

Specialize syntax error for assignment expressions in invalid places #146601

@brianschubert

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions