-
Notifications
You must be signed in to change notification settings - Fork 2k
PLR6301 not aware of attrs validation decorator #12568
Copy link
Copy link
Closed
Labels
ruleImplementing or modifying a lint ruleImplementing or modifying a lint rule
Description
Related to #12172.
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 integerHowever, this triggers PLR6301 - https://play.ruff.rs/2aa3e667-d733-4dc9-b1c7-307256edcabe
Method
validate_xcould be a function, class method, or static method
But in this situation, this is not correct. validate_x needs self in order to be valid.
Perhaps this is beyond the scope of PLR6301, but it would nice if Ruff was context-aware of attrs here.
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
ruleImplementing or modifying a lint ruleImplementing or modifying a lint rule