feat(integrations): add attrs integration with PyTree support#273
feat(integrations): add attrs integration with PyTree support#273XuehaiPan wants to merge 5 commits intometaopt:mainfrom
attrs integration with PyTree support#273Conversation
Add a public `register(cls, /, *, namespace)` function that registers existing `@dataclasses.dataclass`-decorated classes as pytree nodes. Refactor `dataclass()` and `make_dataclass()` to delegate registration logic to `register()`, eliminating ~46 lines of duplicated code. Also export `DataclassEntry` from `optree.dataclasses` for convenience.
Add `optree.integrations.attrs` module providing PyTree integration for attrs-decorated classes. Includes `field`, `define`, `frozen`, `mutable`, `make_class`, and `register` functions that wrap attrs APIs with pytree node registration. Also adds decorator support for `register()` in both `optree.dataclasses` and `optree.integrations.attrs` modules, following the `register_pytree_node_class` pattern.
There was a problem hiding this comment.
Pull request overview
Adds first-class PyTree integration for attrs classes and extends the existing dataclasses integration with an explicit register() API.
Changes:
- Introduces
optree.integrations.attrswithfield/define/frozen/make_class/registerandAttrsEntrypath support. - Adds
optree.dataclasses.register()/DataclassEntryexports and corresponding tests. - Updates docs, optional dependencies, and coverage/test configuration to include the new
attrsintegration.
Reviewed changes
Copilot reviewed 13 out of 13 changed files in this pull request and generated 4 comments.
Show a summary per file
| File | Description |
|---|---|
| tests/test_dataclasses.py | Expands public API assertions and adds dataclasses.register() behavior tests. |
| tests/requirements.txt | Adds attrs to the test environment dependencies. |
| tests/integrations/test_imports.py | Extends integration import tests to include attrs. |
| tests/integrations/test_attrs.py | New test suite covering attrs integration behavior and edge cases. |
| tests/.coveragerc | Marks attrs as an optional module for coverage configuration. |
| pyproject.toml | Adds attrs as an optional dependency and includes it in docs extras. |
| optree/integrations/attrs.py | New attrs integration module implementing PyTree registration/flattening and re-exports. |
| optree/integrations/init.py | Adds attrs to integrations submodule discovery. |
| optree/dataclasses.py | Adds register() implementation and exports DataclassEntry + updates logic to use register(). |
| docs/source/spelling_wordlist.txt | Adds attrs to spelling whitelist. |
| docs/source/integrations.rst | Documents the new attrs integration API. |
| docs/source/index.rst | Minor wording update in project overview/licensing text. |
| docs/requirements.txt | Adds attrs to docs build dependencies. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
You can also share your feedback on Copilot code review. Take the survey.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #273 +/- ##
==========================================
Coverage 100.00% 100.00%
==========================================
Files 14 15 +1
Lines 1416 1553 +137
Branches 175 204 +29
==========================================
+ Hits 1416 1553 +137 Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. |
Fix comment typo in dataclasses.py (`dataclasses` -> `dataclass`), use `import attrs` in test_imports.py for consistency, and add `.. data:: mutable` directive in docs. Add unit tests for DataclassEntry and AttrsEntry covering fields, init_fields, integer entry dispatch, name, and repr. Remove unnecessary pragma: no cover.
There was a problem hiding this comment.
Pull request overview
Adds a new optree.integrations.attrs module to enable PyTree flatten/unflatten support for attrs-decorated classes, along with tests and documentation updates. It also extends the optree.dataclasses integration with a new register() API and exposes DataclassEntry publicly.
Changes:
- Add
attrsintegration module (field,define,frozen,make_class,register) plusAttrsEntryfor accessor paths. - Add comprehensive integration tests for
attrsand extend dataclasses tests forregister()/public API. - Wire up optional dependencies + docs to include
attrsintegration.
Reviewed changes
Copilot reviewed 14 out of 14 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| optree/integrations/attrs.py | Implements PyTree registration and wrapper APIs for attrs classes. |
| optree/integrations/init.py | Exposes attrs as an integrations submodule and updates SUBMODULES. |
| optree/dataclasses.py | Adds register() for existing dataclasses; exports DataclassEntry; refactors decorator to reuse register(). |
| optree/accessors.py | Enables coverage for DataclassEntry.fields() by removing no cover. |
| tests/integrations/test_attrs.py | New test suite covering attrs wrappers, registration, accessors, and error cases. |
| tests/integrations/test_imports.py | Updates integration import expectations to include attrs. |
| tests/test_dataclasses.py | Updates public API expectations and adds tests for dataclasses.register(). |
| pyproject.toml | Adds attrs optional extra and includes it in docs extras. |
| tests/requirements.txt | Adds attrs to test requirements. |
| docs/requirements.txt | Adds attrs to docs requirements. |
| tests/.coveragerc | Marks attrs as an optional module for coverage. |
| docs/source/integrations.rst | Documents the new attrs integration APIs. |
| docs/source/spelling_wordlist.txt | Adds attrs to spelling whitelist. |
| docs/source/index.rst | Minor wording/formatting change in license blurb. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
You can also share your feedback on Copilot code review. Take the survey.
Rename the public function from `register()` to `register_node()` in both `optree.dataclasses` and `optree.integrations.attrs` modules. Update all tests, docs, and docstring references accordingly.
Add ``.. versionadded:: 0.20.0`` to the attrs integration module docstring, all public functions in ``optree.integrations.attrs``, and ``optree.dataclasses.register_node()``.
There was a problem hiding this comment.
Pull request overview
This PR adds first-class PyTree integration for attrs classes and extends the existing dataclasses integration to support registering already-defined dataclasses as PyTree nodes.
Changes:
- Added
optree.integrations.attrsmodule withfield/define/frozen/make_class/register_nodeandAttrsEntrypath support. - Refactored
optree.dataclassesto exposeDataclassEntryand a newregister_node()API, with corresponding tests. - Updated tests, optional dependencies, coverage config, and docs to include the new
attrsintegration.
Reviewed changes
Copilot reviewed 14 out of 14 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| tests/test_dataclasses.py | Adds tests for dataclasses.register_node, accessors, and updated public API surface. |
| tests/requirements.txt | Adds attrs to the test dependency set. |
| tests/integrations/test_imports.py | Extends integration import tests to cover attrs. |
| tests/integrations/test_attrs.py | New test suite for the attrs integration API and behavior. |
| tests/.coveragerc | Marks attrs as an optional module for coverage configuration. |
| pyproject.toml | Adds attrs optional dependency group and includes it in docs extras. |
| optree/integrations/attrs.py | New attrs integration implementation with PyTree registration + path entries. |
| optree/integrations/init.py | Adds attrs to the known integrations submodules set. |
| optree/dataclasses.py | Exposes DataclassEntry, adds register_node, and routes decorator registration through it. |
| optree/accessors.py | Removes pragma: no cover from DataclassEntry.fields (now exercised by tests). |
| docs/source/spelling_wordlist.txt | Adds attrs to spelling whitelist. |
| docs/source/integrations.rst | Documents the new attrs integration API. |
| docs/source/index.rst | Minor wording/formatting update in license attribution section. |
| docs/requirements.txt | Adds attrs to docs build dependencies. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
You can also share your feedback on Copilot code review. Take the survey.
Description
Describe your changes in detail.
Motivation and Context
Why is this change required? What problem does it solve?
If it fixes an open issue, please link to the issue here.
You can use the syntax
close #15213if this solves the issue #15213Types of changes
What types of changes does your code introduce? Put an
xin all the boxes that apply:Checklist
Go over all the following points, and put an
xin all the boxes that apply.If you are unsure about any of these, don't hesitate to ask. We are here to help!
make format. (required)make lint. (required)make testpass. (required)