-
Notifications
You must be signed in to change notification settings - Fork 216
Open
Labels
attribute accessInstance attributes, class attributes, etc.Instance attributes, class attributes, etc.dataclassesIssues relating to dataclasses and dataclass_transformIssues relating to dataclasses and dataclass_transformlibraryDedicated support for popular third-party librariesDedicated support for popular third-party libraries
Milestone
Description
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 ValueErrorty
$ 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)
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
attribute accessInstance attributes, class attributes, etc.Instance attributes, class attributes, etc.dataclassesIssues relating to dataclasses and dataclass_transformIssues relating to dataclasses and dataclass_transformlibraryDedicated support for popular third-party librariesDedicated support for popular third-party libraries