Repository navigation
docs: document how to type custom validator functions with attrs.Attribute - #1666
Mehmetbaruk wants to merge 3 commits into
Conversation
chrikrah
left a comment
There was a problem hiding this comment.
@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 |
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 writingattribute: attrs.Attribute[int]raisesTypeErrorat runtime on Python 3.13 and older (becauseAttributeis generic in.pyitype stubs rather than at runtime) and shows the two ways to handle it: addingfrom __future__ import annotationsat the top of the file, or quoting the type as"attrs.Attribute[int]".Includes
changelog.d/1666.change.mdand plain code blocks; verified locally withruff,codespell,towncrier build --version main --draft, andpytest tests/test_validators.py.Pull Request Checklist
mainbranch..pyi).typing_tests/baseline.pyor, if necessary,typing_tests/mypy.py.attr/__init__.pyi, they've also been re-imported inattrs/__init__.pyi.docs/api.rstby hand.@attr.s()and@attrs.define()have to be added by hand too.versionadded,versionchanged, ordeprecateddirectives..rstand.mdfiles is written using semantic newlines.changelog.d.