Skip to content

Format assignment expressions (FormatExprNamedExpr) #5613

@konstin

Description

@konstin

Assignment expressions are those using the "walrus operator" :=:

if x := f():
    g(x)

In general, they are easy to format, they are a sequence of target, space, :=, space, value with optional parentheses.

The difficulty is that there are a list of exceptional cases in PEP 572 where the parentheses are mandatory based on the surrounding node, e.g. keyword argument values need parentheses. So for implementing this we either need to use the tuple way of checking if there are parentheses in range or each node in the list in PEP 572 needs to have a special case where it checks whether the expression it is about to format is a ExprNamedExpr and in that cases sets the parentheses to always.

Metadata

Metadata

Assignees

No one assigned

    Labels

    formatterRelated to the formatter

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions