Skip to content

B902: False positive when using the attrs library #413

@henzef

Description

@henzef

The adaptions to B902 cause a regression when using the attrs library:

import attrs
from typing import Any


@attrs.frozen
class SpamAndEggs:
    foobar: list[int] = attrs.field()

    @foobar.validator
    def _positive(
        self,
        attribute: "attrs.Attribute[list[int]]",
        value: Any,
    ) -> None:
        if any(i < 0 for i in value):
            raise ValueError()


SpamAndEggs([1, 2, 3])

The error is:

myfile.py:11:9: B902 Invalid first argument 'self' used for class method. Use the canonical first argument name in methods, i.e. cls.

My suggestion would be to remove validator and root_validator from the default list and allow pydantic user to set them via the commandline flag. I would imagine that other libraries also have @validator decorators or that some people write their own

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions