Skip to content

Rule request: Warn whenever any default value is provided in __post_init__ #13128

@NeilGirdhar

Description

@NeilGirdhar

Default values should not be given in the arguments of __post_init__. They should only be provided as default values for class-level init-only member variables.

from dataclasses import InitVar, dataclass


@dataclass
class C:
    x: InitVar[int] = 1

    def __post_init__(self, x: int = 4) -> None:
        print(x)  # noqa: T201


c = C()  # Prints 1!  4 is ignored.

The above text could be used for the Ruff rule warning message.

Metadata

Metadata

Assignees

No one assigned

    Labels

    acceptedReady for implementationhelp wantedContributions especially welcomeruleImplementing or modifying a lint rule

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions