Skip to content

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)

Resolves #252

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)

@XuehaiPan XuehaiPan self-assigned this Nov 8, 2025
@XuehaiPan XuehaiPan added enhancement New feature or request python Something related to the Python source code labels Nov 8, 2025
@XuehaiPan XuehaiPan requested a review from Copilot November 8, 2025 09:38
@codecov
Copy link

codecov bot commented Nov 8, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (d93dbc6) to head (84f805d).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff            @@
##              main      #253   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           14        14           
  Lines         1403      1416   +13     
  Branches       173       175    +2     
=========================================
+ Hits          1403      1416   +13     
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.

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 updates the pytree node registration API to use dunder-style method names (__tree_flatten__ and __tree_unflatten__) instead of the legacy method names (tree_flatten and tree_unflatten). The change maintains backward compatibility by automatically wrapping legacy methods with dunder-style versions.

  • Implements automatic wrapping of legacy method names to new dunder-style names for backward compatibility
  • Updates the CustomTreeNode protocol to require dunder-style methods
  • Adds comprehensive test coverage for the new behavior, including tests for legacy methods, mixed styles, and missing methods

Reviewed Changes

Copilot reviewed 10 out of 10 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
optree/registry.py Adds backward compatibility logic to wrap legacy tree_flatten/tree_unflatten methods as __tree_flatten__/__tree_unflatten__, includes version notes
optree/typing.py Updates CustomTreeNode protocol to use dunder-style method names and adds TREE_PATH_ENTRY_TYPE class variable
optree/functools.py Updates partial class methods to use dunder-style names
tests/test_registry.py Updates existing tests to use dunder-style names and adds three new comprehensive test functions for legacy method support, missing methods, and wrapping behavior
tests/test_treespec.py Updates test class methods to use dunder-style names
tests/test_dataclasses.py Updates test class methods to use dunder-style names
tests/test_accessors.py Updates test class methods to use dunder-style names
tests/helpers.py Updates helper class methods to use dunder-style names
README.md Updates documentation examples to use dunder-style method names
docs/source/spelling_wordlist.txt Adds "dunder" to the spelling wordlist

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@XuehaiPan XuehaiPan force-pushed the dunder-methods branch 3 times, most recently from b8d5b69 to 28deee1 Compare November 8, 2025 10:59
@XuehaiPan XuehaiPan force-pushed the dunder-methods branch 2 times, most recently from 7d6688d to 1a99134 Compare November 8, 2025 11:18
@XuehaiPan XuehaiPan force-pushed the dunder-methods branch 2 times, most recently from 4c694e6 to e9f5b15 Compare November 8, 2025 13:50
@XuehaiPan XuehaiPan merged commit 2327513 into metaopt:main Nov 8, 2025
241 of 242 checks passed
@XuehaiPan XuehaiPan deleted the dunder-methods branch November 8, 2025 17:34
@XuehaiPan XuehaiPan added this to the 0.18.0 milestone Nov 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request python Something related to the Python source code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature Request] Add support for method pair (__tree_flatten__, __tree_unflatten__) for register_pytree_node_class(cls)

1 participant