Skip to content

feat(PyTreeKind): use pybind11::native_enum to create enum class PyTreeKind #214

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
May 8, 2025

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)

@XuehaiPan XuehaiPan added enhancement New feature or request cxx Something related to the CXX source code labels May 5, 2025
@XuehaiPan XuehaiPan self-assigned this May 5, 2025
@XuehaiPan XuehaiPan changed the title feat(PyTreeKind): use pybind11::native_enum to create enum class PyTreeKind feat(PyTreeKind): use pybind11::native_enum to create enum class PyTreeKind May 5, 2025
@XuehaiPan XuehaiPan force-pushed the native-enum branch 2 times, most recently from 93f1b6d to a8faff8 Compare May 5, 2025 11:32
@XuehaiPan XuehaiPan added the dependency Pull requests that update a dependency file label May 5, 2025
Copy link

codecov bot commented May 5, 2025

Codecov Report

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

Additional details and impacted files
@@            Coverage Diff            @@
##              main      #214   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           14        14           
  Lines         1302      1302           
  Branches       155       155           
=========================================
  Hits          1302      1302           
Flag Coverage Δ
unittests 100.00% <ø> (ø)
unittests-cp310-Linux 100.00% <ø> (ø)
unittests-cp310-Windows 100.00% <ø> (ø)
unittests-cp310-macOS 100.00% <ø> (ø)
unittests-cp311-Linux 100.00% <ø> (ø)
unittests-cp311-Windows 100.00% <ø> (ø)
unittests-cp311-macOS 100.00% <ø> (ø)
unittests-cp312-Linux 100.00% <ø> (ø)
unittests-cp312-Windows 100.00% <ø> (ø)
unittests-cp312-macOS 100.00% <ø> (ø)
unittests-cp313-Linux 100.00% <ø> (ø)
unittests-cp313-Windows 100.00% <ø> (ø)
unittests-cp313-macOS 100.00% <ø> (ø)
unittests-cp313t-Linux 100.00% <ø> (ø)
unittests-cp313t-Windows 100.00% <ø> (ø)
unittests-cp313t-macOS 100.00% <ø> (ø)
unittests-cp39-Linux 100.00% <ø> (ø)
unittests-cp39-Windows 100.00% <ø> (ø)
unittests-cp39-macOS 100.00% <ø> (ø)
unittests-pp310-Linux 100.00% <ø> (ø)
unittests-pp310-Windows 100.00% <ø> (ø)
unittests-pp310-macOS 100.00% <ø> (ø)
unittests-pp311-Linux 100.00% <ø> (ø)
unittests-pp311-Windows 100.00% <ø> (ø)
unittests-pp311-macOS 100.00% <ø> (ø)
unittests-pp39-Linux 100.00% <ø> (ø)
unittests-pp39-Windows 100.00% <ø> (ø)
unittests-pp39-macOS 100.00% <ø> (ø)
unittests-pydebug 100.00% <ø> (ø)
unittests-pydebug-cp310d-Linux 100.00% <ø> (ø)
unittests-pydebug-cp310d-Windows 55.63% <ø> (ø)
unittests-pydebug-cp310d-macOS 100.00% <ø> (ø)
unittests-pydebug-cp311d-Linux 100.00% <ø> (ø)
unittests-pydebug-cp311d-Windows 55.54% <ø> (ø)
unittests-pydebug-cp311d-macOS 100.00% <ø> (ø)
unittests-pydebug-cp312d-Linux 100.00% <ø> (ø)
unittests-pydebug-cp312d-Windows 55.29% <ø> (ø)
unittests-pydebug-cp312d-macOS 100.00% <ø> (ø)
unittests-pydebug-cp313d-Linux 100.00% <ø> (ø)
unittests-pydebug-cp313d-Windows 55.29% <ø> (ø)
unittests-pydebug-cp313d-macOS 100.00% <ø> (ø)
unittests-pydebug-cp313td-Linux 100.00% <ø> (ø)
unittests-pydebug-cp313td-Windows 55.29% <ø> (ø)
unittests-pydebug-cp313td-macOS 100.00% <ø> (ø)
unittests-pydebug-cp39d-Linux 100.00% <ø> (ø)
unittests-pydebug-cp39d-Windows 55.45% <ø> (ø)
unittests-pydebug-cp39d-macOS 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 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 introduces support for native enum types in the PyTreeKind enum by leveraging the new pybind11::native_enum feature when available, along with updated version checks for Python and pybind11.

  • Introduces conditional compilation blocks for native enum support.
  • Updates immutability handling for PyTreeKind, PyTreeSpec, and PyTreeIter types.
  • Enforces newer version requirements for Python and pybind11 in the macros header.

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
src/optree.cpp Adds native enum support for PyTreeKind and updates immutability.
include/optree/pymacros.h Updates version checks and rearranges namespace declaration.
Comments suppressed due to low confidence (1)

src/optree.cpp:28

  • [nitpick] Consider adding an inline comment to clarify the rationale behind the combined condition for enabling native enum support to aid future maintainability.
#if defined(PYBIND11_HAS_NATIVE_ENUM) || (defined(PYBIND11_INTERNALS_VERSION) && PYBIND11_INTERNALS_VERSION >= 8)

@XuehaiPan XuehaiPan requested a review from Copilot May 5, 2025 19:18
Copy link

@Copilot 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 introduces a new feature to define the PyTreeKind enum class using pybind11::native_enum when available, falling back to the existing py::enum_ approach otherwise. Key changes include:

  • Adding preprocessor checks and macro definitions in src/optree.cpp to detect native enum support.
  • Updating dependency references in pyproject.toml and the lint workflow to pull the latest pybind11 version.
  • Enforcing minimum version checks in include/optree/pymacros.h.

Reviewed Changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.

File Description
src/optree.cpp Adds conditional support for pybind11::native_enum and adjusts type flags.
pyproject.toml Updates pybind11 dependency to use the latest GitHub source.
include/optree/pymacros.h Adjusts preprocessor version checks for Python and pybind11.
.github/workflows/lint.yml Installs the updated pybind11 dependency in CI.

@XuehaiPan XuehaiPan force-pushed the native-enum branch 2 times, most recently from 018d358 to 70798e7 Compare May 6, 2025 15:22
@XuehaiPan XuehaiPan force-pushed the native-enum branch 2 times, most recently from 0092f05 to 7765488 Compare May 7, 2025 06:54
@XuehaiPan XuehaiPan requested a review from Copilot May 7, 2025 14:21
Copy link

@Copilot 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 introduces a new feature that conditionally creates the enum class PyTreeKind using pybind11::native_enum when available, and refines related module initialization and build steps. Key changes include:

  • Adding conditional compilation logic in src/optree.cpp to support pybind11::native_enum.
  • Updating header version checks in include/optree/pymacros.h.
  • Enhancing CI workflows to allow testing with nightly pybind11 builds.

Reviewed Changes

Copilot reviewed 8 out of 8 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
src/optree.cpp Uses pybind11::native_enum conditionally and replaces PyType_Ready calls with PyType_Modified.
include/optree/pymacros.h Adjusts version checks and organizes namespace declarations.
CHANGELOG.md Updates changelog with entries for the new enum and build improvements.
.github/workflows/tests.yml Adds step for testing with nightly pybind11 builds.
.github/workflows/tests-with-pydebug.yml Adds step for testing with nightly pybind11 builds.
.github/workflows/set_setup_requires.py Provides a script to update pyproject.toml to use nightly pybind11.
.github/workflows/lint.yml Adds nightly pybind11 installation step during linting.
.github/workflows/build.yml Revises conditions for triggering build jobs in workflows.
Comments suppressed due to low confidence (2)

src/optree.cpp:29

  • [nitpick] Consider adding a brief comment explaining the rationale behind the conditional inclusion of native_enum to assist future maintainers.
#if defined(PYBIND11_HAS_NATIVE_ENUM) || (defined(PYBIND11_INTERNALS_VERSION) && PYBIND11_INTERNALS_VERSION >= 8)

.github/workflows/set_setup_requires.py:19

  • [nitpick] Review the regex pattern to ensure it accurately targets the pybind11 dependency declaration without over-matching; consider refining it for clarity if necessary.
r'(requires\s*=\s*\[.*"\s*)pybind11[^"']*(\s*".*\])'

@XuehaiPan XuehaiPan added the test-with-nightly-pybind11 Use nightly pybind11 in tests label May 7, 2025
@XuehaiPan XuehaiPan force-pushed the native-enum branch 2 times, most recently from f968884 to 041a79c Compare May 7, 2025 14:44
@XuehaiPan XuehaiPan force-pushed the native-enum branch 4 times, most recently from bd749c0 to 044842e Compare May 8, 2025 09:32
@XuehaiPan XuehaiPan merged commit c402bf7 into metaopt:main May 8, 2025
119 checks passed
@XuehaiPan XuehaiPan deleted the native-enum branch May 8, 2025 14:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cxx Something related to the CXX source code dependency Pull requests that update a dependency file enhancement New feature or request test-with-nightly-pybind11 Use nightly pybind11 in tests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants