astral-sh / ruff

An extremely fast Python linter and code formatter, written in Rust.
https://docs.astral.sh/ruff
MIT License
29.95k stars 979 forks source link

Rule Suggestion: explict-dataclasses #8981

Open sbdchd opened 8 months ago

sbdchd commented 8 months ago

Basically I want dataclasses to default to frozen=True and slots=True with the option to opt out.

The rule would require passing the frozen and slots warts (if the python version is new enough to have slots)

Not sure about the naming of the rule

Or maybe the rule should be more general, like dataclass-require-params and the params are part of a config?

from dataclasses import dataclass

# error, missing frozen and slot params
@dataclass
class Data: ...

# ok, unless on a python version with slots
@dataclass(frozen=True)
class Data: ...

# ok
@dataclass(frozen=True, slots=True)
class Data: ...

# ok
@dataclass(frozen=False, slots=False)
class Data: ...

# ok
@dataclass(frozen=True, slots=False)
class Data: ...
Avasam commented 8 months ago

I've been thinking about wanting a way to enforce using slots on dataclasses. slots are the default with the attr package! idk about frozen though.