Skip to content

unresolved-attribute for @<field>.validator in attrs class #267

@my1e5

Description

@my1e5

Summary

Reporting this here to understand if this is something that needs sorting on the attrs library side.

attrs allows you to define a validator for an attribute using a decorator (@<field>.validator)

# /// script
# requires-python = ">=3.13"
# dependencies = [
#     "attrs",
# ]
# ///

from typing import Any

from attrs import define, field


@define(kw_only=True)
class Foo:
    name: str = field(default="")
    age: int = field(default=0)

    @age.validator
    def check_age(self, _: Any, value: int) -> None:
        if value < 0:
            raise ValueError("Age must be a positive integer.")


if __name__ == "__main__":
    foo = Foo(age=3)
    foo.age = -2  # This will raise a ValueError

ty

$ uvx --with attrs==25.3.0 ty check foo.py 
Installed 2 packages in 51ms
error: lint:unresolved-attribute: Type `Literal[0]` has no attribute `validator`
  --> foo.py:18:6
   |
16 |     age: int = field(default=0)
17 |
18 |     @age.validator
   |      ^^^^^^^^^^^^^
19 |     def check_age(self, _: Any, value: int) -> None:
20 |         if value < 0:
   |
info: `lint:unresolved-attribute` is enabled by default

Found 1 diagnostic

mypy

$ uvx --with attrs mypy --strict foo.py
Success: no issues found in 1 source file

Version

ty 0.0.0-alpha.7 (905a3e1 2025-05-07)

Metadata

Metadata

Assignees

No one assigned

    Labels

    attribute accessInstance attributes, class attributes, etc.dataclassesIssues relating to dataclasses and dataclass_transformlibraryDedicated support for popular third-party libraries

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions