The attrs library, which is similar to dataclasses, uses decorators to allow you to define validators for the attributes of your class. Here is a simple example:
from attrs import define, field
@define
class Foo:
x: int = field()
@x.validator
def validate_x(self, attribute, value):
if value <= 0:
raise ValueError("x must be a positive integer")
foo = Foo(x=-1) # ValueError: x must be a positive integer
Related to https://github.com/astral-sh/ruff/issues/12172.
The
attrs
library, which is similar todataclasses
, uses decorators to allow you to define validators for the attributes of your class. Here is a simple example:However, this triggers
PLR6301
- https://play.ruff.rs/2aa3e667-d733-4dc9-b1c7-307256edcabeBut in this situation, this is not correct.
validate_x
needsself
in order to be valid.Perhaps this is beyond the scope of
PLR6301
, but it would nice if Ruff was context-aware ofattrs
here.