Skip to content

feat(integrations): add attrs integration with PyTree support#273

Open
XuehaiPan wants to merge 5 commits intometaopt:mainfrom
XuehaiPan:attrs
Open

feat(integrations): add attrs integration with PyTree support#273
XuehaiPan wants to merge 5 commits intometaopt:mainfrom
XuehaiPan:attrs

Conversation

@XuehaiPan
Copy link
Member

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 #15213 if this solves the issue #15213

  • I have raised an issue to propose this change (required for new features and bug fixes)

Types of changes

What types of changes does your code introduce? Put an x in all the boxes that apply:

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds core functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation (update in the documentation)

Checklist

Go over all the following points, and put an x in all the boxes that apply.
If you are unsure about any of these, don't hesitate to ask. We are here to help!

  • I have read the CONTRIBUTION guide. (required)
  • My change requires a change to the documentation.
  • I have updated the tests accordingly. (required for a bug fix or a new feature)
  • I have updated the documentation accordingly.
  • I have reformatted the code using make format. (required)
  • I have checked the code using make lint. (required)
  • I have ensured make test pass. (required)

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.
@XuehaiPan XuehaiPan self-assigned this Mar 19, 2026
@XuehaiPan XuehaiPan added enhancement New feature or request python Something related to the Python source code integration Integration for other packages labels Mar 19, 2026
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.
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

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.attrs with field/define/frozen/make_class/register and AttrsEntry path support.
  • Adds optree.dataclasses.register() / DataclassEntry exports and corresponding tests.
  • Updates docs, optional dependencies, and coverage/test configuration to include the new attrs integration.

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
Copy link

codecov bot commented Mar 19, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (24bf2d4) to head (552ae9e).

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     
Flag Coverage Δ
unittests 100.00% <100.00%> (ø)
unittests-cp310-Linux 100.00% <100.00%> (ø)
unittests-cp310-Windows 100.00% <100.00%> (ø)
unittests-cp310-macOS 100.00% <100.00%> (ø)
unittests-cp311-Linux 100.00% <100.00%> (ø)
unittests-cp311-Windows 100.00% <100.00%> (ø)
unittests-cp311-macOS 100.00% <100.00%> (ø)
unittests-cp312-Linux 100.00% <100.00%> (ø)
unittests-cp312-Windows 100.00% <100.00%> (ø)
unittests-cp312-macOS 100.00% <100.00%> (ø)
unittests-cp313-Linux 100.00% <100.00%> (ø)
unittests-cp313-Windows 100.00% <100.00%> (ø)
unittests-cp313-macOS 100.00% <100.00%> (ø)
unittests-cp313t-Linux 100.00% <100.00%> (ø)
unittests-cp313t-Windows 100.00% <100.00%> (ø)
unittests-cp313t-macOS 100.00% <100.00%> (ø)
unittests-cp314-Linux 100.00% <100.00%> (ø)
unittests-cp314-Windows 100.00% <100.00%> (ø)
unittests-cp314-macOS 100.00% <100.00%> (ø)
unittests-cp314t-Linux 100.00% <100.00%> (ø)
unittests-cp314t-Windows 100.00% <100.00%> (ø)
unittests-cp314t-macOS 100.00% <100.00%> (ø)
unittests-cp39-Linux 100.00% <100.00%> (ø)
unittests-cp39-Windows 100.00% <100.00%> (ø)
unittests-cp39-macOS 100.00% <100.00%> (ø)
unittests-pp311-Linux 100.00% <100.00%> (ø)
unittests-pp311-Windows 100.00% <100.00%> (ø)
unittests-pp311-macOS 100.00% <100.00%> (ø)
unittests-pydebug 100.00% <100.00%> (ø)
unittests-pydebug-cp310d-Linux 100.00% <100.00%> (ø)
unittests-pydebug-cp310d-Windows 100.00% <100.00%> (ø)
unittests-pydebug-cp310d-macOS 100.00% <100.00%> (ø)
unittests-pydebug-cp311d-Linux 100.00% <100.00%> (ø)
unittests-pydebug-cp311d-Windows 100.00% <100.00%> (ø)
unittests-pydebug-cp311d-macOS 100.00% <100.00%> (ø)
unittests-pydebug-cp312d-Linux 100.00% <100.00%> (ø)
unittests-pydebug-cp312d-Windows 100.00% <100.00%> (ø)
unittests-pydebug-cp312d-macOS 100.00% <100.00%> (ø)
unittests-pydebug-cp313d-Linux 100.00% <100.00%> (ø)
unittests-pydebug-cp313d-Windows 100.00% <100.00%> (ø)
unittests-pydebug-cp313d-macOS 100.00% <100.00%> (ø)
unittests-pydebug-cp313td-Linux 100.00% <100.00%> (ø)
unittests-pydebug-cp313td-Windows 100.00% <100.00%> (ø)
unittests-pydebug-cp313td-macOS 100.00% <100.00%> (ø)
unittests-pydebug-cp314d-Linux 100.00% <100.00%> (ø)
unittests-pydebug-cp314d-Windows 100.00% <100.00%> (ø)
unittests-pydebug-cp314d-macOS 100.00% <100.00%> (ø)
unittests-pydebug-cp314td-Linux 100.00% <100.00%> (ø)
unittests-pydebug-cp314td-Windows 100.00% <100.00%> (ø)
unittests-pydebug-cp314td-macOS 100.00% <100.00%> (ø)
unittests-pydebug-cp39d-Linux 100.00% <100.00%> (ø)
unittests-pydebug-cp39d-Windows 100.00% <100.00%> (ø)
unittests-pydebug-cp39d-macOS 100.00% <100.00%> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

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.
@XuehaiPan XuehaiPan requested a review from Copilot March 19, 2026 13:39
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

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 attrs integration module (field, define, frozen, make_class, register) plus AttrsEntry for accessor paths.
  • Add comprehensive integration tests for attrs and extend dataclasses tests for register()/public API.
  • Wire up optional dependencies + docs to include attrs integration.

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.
@XuehaiPan XuehaiPan added this to the 0.20.0 milestone Mar 19, 2026
Add ``.. versionadded:: 0.20.0`` to the attrs integration module
docstring, all public functions in ``optree.integrations.attrs``,
and ``optree.dataclasses.register_node()``.
@XuehaiPan XuehaiPan requested a review from Copilot March 19, 2026 14:03
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

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.attrs module with field/define/frozen/make_class/register_node and AttrsEntry path support.
  • Refactored optree.dataclasses to expose DataclassEntry and a new register_node() API, with corresponding tests.
  • Updated tests, optional dependencies, coverage config, and docs to include the new attrs integration.

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.

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

Labels

enhancement New feature or request integration Integration for other packages python Something related to the Python source code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants