Skip to content

docs: document how to type custom validator functions with attrs.Attribute - #1666

Open
Mehmetbaruk wants to merge 3 commits into
python-attrs:mainfrom
Mehmetbaruk:docs/typing-validators
Open

Mehmetbaruk wants to merge 3 commits into
python-attrs:mainfrom
Mehmetbaruk:docs/typing-validators

Conversation

@Mehmetbaruk

@Mehmetbaruk Mehmetbaruk commented Nov 24, 2026 •

Copy link
Copy Markdown

Summary

Fixes #524.

Basically, I solved the confusion around typing custom validator functions by adding a short "Typing validators" section to docs/types.md. It explains why writing attribute: attrs.Attribute[int] raises TypeError at runtime on Python 3.13 and older (because Attribute is generic in .pyi type stubs rather than at runtime) and shows the two ways to handle it: adding from __future__ import annotations at the top of the file, or quoting the type as "attrs.Attribute[int]".

Includes changelog.d/1666.change.md and plain code blocks; verified locally with ruff, codespell, towncrier build --version main --draft, and pytest tests/test_validators.py.

Pull Request Checklist

  • I acknowledge this project's AI policy.
  • This pull request is not from my main branch.
  • There's tests for all new and changed code.
  • Changes or additions to public APIs are reflected in our type stubs (files ending in .pyi).
    • ...and used in the stub test file typing_tests/baseline.py or, if necessary, typing_tests/mypy.py.
    • If they've been added to attr/__init__.pyi, they've also been re-imported in attrs/__init__.pyi.
  • The documentation has been updated.
    • New functions/classes have to be added to docs/api.rst by hand.
    • Changes to the signatures of @attr.s() and @attrs.define() have to be added by hand too.
    • Changed/added classes/methods/functions have appropriate versionadded, versionchanged, or deprecated directives.
    • Documentation in .rst and .md files is written using semantic newlines.
  • Changes have news fragments in changelog.d.

@chrikrah chrikrah left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Mehmetbaruk Both recipes hold on 3.10 and 3.13, so the advice is right.

# PR head 970f92a via git archive, attrs imported from its src/
$ uv run --python 3.10 quoted.py   # same on 3.13
Item(count=3) / ValueError: count must be positive
$ uv run --python 3.10 fut.py      # same on 3.13
Item(count=3)
# the unquoted annotation at module level
3.10: TypeError: 'type' object is not subscriptable
3.11 to 3.13: TypeError: type 'Attribute' is not subscriptable

non-blocking: the docs doctests run on 3.14 (.python-version-default), where the quotes change nothing, so this doctest can't guard the claim. A plain code block would promise less.

non-blocking: the quoted error is the 3.10 text; I'd name only the TypeError. And the changelog.d, stub-test and api.rst boxes are ticked on a docs-only diff. Will you add changelog.d/1666.change.md or untick them?

@Mehmetbaruk

Copy link
Copy Markdown
Author

@Mehmetbaruk Both recipes hold on 3.10 and 3.13, so the advice is right.

# PR head 970f92a via git archive, attrs imported from its src/
$ uv run --python 3.10 quoted.py   # same on 3.13
Item(count=3) / ValueError: count must be positive
$ uv run --python 3.10 fut.py      # same on 3.13
Item(count=3)
# the unquoted annotation at module level
3.10: TypeError: 'type' object is not subscriptable
3.11 to 3.13: TypeError: type 'Attribute' is not subscriptable

non-blocking: the docs doctests run on 3.14 (.python-version-default), where the quotes change nothing, so this doctest can't guard the claim. A plain code block would promise less.

non-blocking: the quoted error is the 3.10 text; I'd name only the TypeError. And the changelog.d, stub-test and api.rst boxes are ticked on a docs-only diff. Will you add changelog.d/1666.change.md or untick them?

@chrikrah I got you! Thanks for the review and for testing both recipes on 3.10 and 3.13. Switched the quoted example to a plain code block, shortened the reference to TypeError, added changelog.d/1666.change.md, and unticked the inapplicable stub and api.rst checkboxes in the PR description.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

How should I type my validators?

2 participants