Skip to content

RUF033 fix removes necessary parentheses from around the default expression #18950

@dscorbett

Description

@dscorbett

Summary

The fix for post-init-default (RUF033) introduces a syntax error when it removes necessary parentheses from around the default expression.

Assignment expression:

$ cat >ruf033_1.py <<'# EOF'
from dataclasses import dataclass
@dataclass
class Foo:
    def __post_init__(self, bar: int = (x := 1)) -> None:
        pass
# EOF

$ ruff check --isolated ruf033_1.py --select RUF033 --unsafe-fixes --diff 2>&1 | grep error:
error: Fix introduced a syntax error. Reverting all changes.

Multiline expression:

$ cat >ruf033_2.py <<'# EOF'
from dataclasses import dataclass
@dataclass
class Foo:
    def __post_init__(self, bar: int = (-
        1
    )) -> None:
        pass
# EOF

$ ruff check --isolated ruf033_2.py --select RUF033 --unsafe-fixes --diff 2>&1 | grep error: 
error: Fix introduced a syntax error. Reverting all changes.

Version

ruff 0.12.0 (87f0feb 2025-06-17)

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingfixesRelated to suggested fixes for violationshelp wantedContributions especially welcome

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions