From 3df21e49211b11faa210731708bc1cc77fcc25a3 Mon Sep 17 00:00:00 2001 From: Mehmet BARUK Date: Wed, 25 Nov 2026 02:11:24 +0300 Subject: [PATCH 1/3] docs: document typing custom validators with attrs.Attribute --- docs/types.md | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/docs/types.md b/docs/types.md index b1e001f8f..dc074f987 100644 --- a/docs/types.md +++ b/docs/types.md @@ -58,6 +58,51 @@ 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: 'type' object is not subscriptable` 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: + +```{doctest} +>>> 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) +>>> Item(3) +Item(count=3) +>>> Item(0) +Traceback (most recent call last): + ... +ValueError: count must be positive +``` + + ## Class variables and constants Defining an attribute with a type annotation and assigned value looks like a class variable, but it isn't. From 970f92a68ac4b26dcc82ac52be71924a40060985 Mon Sep 17 00:00:00 2001 From: Mehmet BARUK Date: Wed, 25 Nov 2026 03:26:53 +0300 Subject: [PATCH 2/3] style: format docs/types.md code block with ruff --- docs/types.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/docs/types.md b/docs/types.md index dc074f987..755cb3cdc 100644 --- a/docs/types.md +++ b/docs/types.md @@ -73,7 +73,9 @@ from typing import Any import attrs -def positive(instance: Any, attribute: attrs.Attribute[int], value: int) -> None: +def positive( + instance: Any, attribute: attrs.Attribute[int], value: int +) -> None: if value <= 0: raise ValueError(f"{attribute.name} must be positive") From 288a8b15d7fa830640bc3b2b5e71571537c084fa Mon Sep 17 00:00:00 2001 From: Mehmet BARUK Date: Sat, 28 Nov 2026 01:27:01 +0300 Subject: [PATCH 3/3] docs: address review feedback on validator typing and add changelog fragment --- changelog.d/1666.change.md | 1 + docs/types.md | 33 +++++++++++++++++---------------- 2 files changed, 18 insertions(+), 16 deletions(-) create mode 100644 changelog.d/1666.change.md 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 755cb3cdc..7a37659d5 100644 --- a/docs/types.md +++ b/docs/types.md @@ -61,7 +61,7 @@ In both cases, if you need to resolve these to real types, you can call {func}`a ## 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: 'type' object is not subscriptable` on Python versions before 3.14. +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: @@ -87,21 +87,22 @@ class Item: Without `from __future__ import annotations` on Python 3.13 and earlier, quote the subscripted type instead: -```{doctest} ->>> 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) ->>> Item(3) -Item(count=3) ->>> Item(0) -Traceback (most recent call last): - ... -ValueError: count must be positive +```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) ```