Skip to content

Document when on_setattr hooks run - #1663

Open
iamdatvt94 wants to merge 1 commit into
python-attrs:mainfrom
iamdatvt94:docs/on-setattr-initialization
Open

iamdatvt94 wants to merge 1 commit into
python-attrs:mainfrom
iamdatvt94:docs/on-setattr-initialization

Conversation

@iamdatvt94

Copy link
Copy Markdown

Summary

Closes #672.

Clarify why a custom on_setattr hook runs on assignment but is skipped when the generated __init__ initializes fields. The example shows the same value passed through both paths, and the guide points readers to converters and validators for initialization-time behavior.

Testing

  • tox run -e docs-doctests,docs-build on Windows with Python 3.14.
  • git diff --check.

Pull Request Checklist

  • I acknowledge this project's AI policy.
  • This PR is from a topic branch.
  • The documentation has been updated and includes a runnable example.

OpenAI Codex assisted with the documentation change and verification. I reviewed the diff before submission.

@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.

@iamdatvt94 The paragraph is accurate for the generated __init__, and the doctest pins it, so I'm approving; one gap below, non-blocking.

# head 225715e (base 2a1fb86), git archive, Python 3.14, attr.__file__ under the tree
$ sphinx-build -n -T -W -b doctest -d /tmp/dt docs docs/_build/html
477 tests, 0 failures in tests   # 471 at 2a1fb86
# first expected `3` edited to `6`
1 failure in tests, init.md line 536
# your Counter, plus __attrs_post_init__ doing self.count = self.count + 1
>>> Counter(3).count
8

non-blocking: the new text sits right under the execution order list that ends with __attrs_post_init__, and an assignment made there does go through the hook (3 + 1, doubled). A reader could take "skips on_setattr hooks" to cover the whole construction. I'd add a clause along the lines of "an assignment you make in __attrs_post_init__ is an ordinary assignment and runs them".

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

2 participants