Skip to content

Remove unnecessary walrus operator parentheses #5781

@MichaReiser

Description

@MichaReiser

Our formatter currently always adds parentheses around the walrus (a := 5) operator in for/if/... cases

impl NeedsParentheses for ExprNamedExpr {
fn needs_parentheses(
&self,
_parent: AnyNodeRef,
_context: &PyFormatContext,
) -> OptionalParentheses {
// Unlike tuples, named expression parentheses are not part of the range even when
// mandatory. See [PEP 572](https://peps.python.org/pep-0572/) for details.
OptionalParentheses::Always
}

The goal of this issue is to implement black's parentheses behavior (inside NeedsParentheses) and only preserve parentheses that are necessary.

https://github.com/psf/black/blob/b4dca26c7d93f930bbd5a7b552807370b60d4298/src/black/linegen.py#L1387-L1401

Metadata

Metadata

Assignees

No one assigned

    Labels

    formatterRelated to the formatterhelp wantedContributions especially welcome

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions