diff --git a/changelog.d/1666.change.md b/changelog.d/1666.change.md new file mode 100644 index 000000000..3edec292c --- /dev/null +++ b/changelog.d/1666.change.md @@ -0,0 +1 @@ +Documented how to type custom validator functions using `attrs.Attribute` on Python versions before 3.14. diff --git a/docs/types.md b/docs/types.md index b1e001f8f..7a37659d5 100644 --- a/docs/types.md +++ b/docs/types.md @@ -58,6 +58,54 @@ As of Python 3.14, this is no longer necessary because it introduced [*deferred In both cases, if you need to resolve these to real types, you can call {func}`attrs.resolve_types`, which will update the attributes in place. +## Typing validators + +When typing custom validator functions, you may want to annotate the *attribute* parameter as `attrs.Attribute[int]` or `attrs.Attribute[T]`. +Because {class}`attrs.Attribute` is declared generic in the type stubs (`.pyi`) rather than at runtime, subscripting it directly in an eagerly evaluated annotation raises `TypeError` on Python versions before 3.14. + +To avoid that runtime error while keeping strict static typing, either defer annotation evaluation with `from __future__ import annotations` (or Python 3.14+ deferred annotations), or quote the annotation as a string literal: + +```python +from __future__ import annotations + +from typing import Any + +import attrs + + +def positive( + instance: Any, attribute: attrs.Attribute[int], value: int +) -> None: + if value <= 0: + raise ValueError(f"{attribute.name} must be positive") + + +@attrs.define +class Item: + count: int = attrs.field(validator=positive) +``` + +Without `from __future__ import annotations` on Python 3.13 and earlier, quote the subscripted type instead: + +```python +from typing import Any + +import attrs + + +def positive( + instance: Any, attribute: "attrs.Attribute[int]", value: int +) -> None: + if value <= 0: + raise ValueError(f"{attribute.name} must be positive") + + +@attrs.define +class Item: + count: int = attrs.field(validator=positive) +``` + + ## Class variables and constants Defining an attribute with a type annotation and assigned value looks like a class variable, but it isn't.