0
  • Environment
    Python v3.9.13
    Pyright 1.1.357
    attrs 23.1.0

The code at official document of attrs library make the pyright type checker casue reportAttributeAccessIssue

https://www.attrs.org/en/stable/init.html#validators

  • Code
from attrs import define, field


@define
class C:
    x: int = field()

    @x.validator
    def _check_x(self, attribute, value):
        if value > 42:
            raise ValueError("x must be smaller or equal to 42")
  • Result of pyright
qwer.py:8:8 - error: Cannot access member "validator" for type "int"
    Member "validator" is unknown (reportAttributeAccessIssue)
1 error, 0 warnings, 0 informations

Instead of ignoring the issue, is there any way to resolve it?

4
  • 1
    Hello usan, while people are searching for an issue I'd like to wonder why you didn't consider to use pydantic for class validation? It is one the most convenient libraries for validation. Commented Apr 16 at 10:47
  • Also I would consider removing the decorator @x.validator and replacing it with x: int = field(validator=[self. _check_x]) Commented Apr 16 at 10:51
  • 1
    FTR, I asked a similar question, but for PyCharm. It never got an answer.
    – InSync
    Commented Apr 16 at 10:53
  • @JohnnyCheesecutter I just prefer to minimize the use of external libraries, which is why I don't use pydantic
    – usan
    Commented Apr 25 at 7:24

1 Answer 1

1

Pyright's support for attrs is, unfortunately, wildly inadequate. They took a hard stance on only supporting anything that gets standardized via PEPs for the new-ish "dataclass transform" API.

Not the answer you're looking for? Browse other questions tagged or ask your own question.