Skip to content

PLR6301 not aware of attrs validation decorator  #12568

@my1e5

Description

@my1e5

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 integer

However, this triggers PLR6301 - https://play.ruff.rs/2aa3e667-d733-4dc9-b1c7-307256edcabe

Method validate_x could 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.

Metadata

Metadata

Assignees

Labels

ruleImplementing 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