feat(functools): add Placeholder support and tests for functools.partial#276
feat(functools): add Placeholder support and tests for functools.partial#276XuehaiPan wants to merge 1 commit intometaopt:mainfrom
Placeholder support and tests for functools.partial#276Conversation
Placeholder support and tests for partialPlaceholder support and tests for functools.partial
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #276 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 15 15
Lines 1561 1565 +4
Branches 207 207
=========================================
+ Hits 1561 1565 +4 Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. |
There was a problem hiding this comment.
Pull request overview
Adds Python 3.14+ functools.Placeholder convenience support to optree.functools by re-exporting the symbol, documenting usage with optree.functools.partial, and expanding the test suite to cover Placeholder roundtrips and call semantics in pytrees.
Changes:
- Conditionally re-export
functools.Placeholderfromoptree.functoolsand include it in__all__on Python 3.14+. - Extend
optree.functools.partialdocstring with a Placeholder example and a note about leaf/identity semantics in pytrees. - Add comprehensive tests for Placeholder behavior across flatten/unflatten, tree_map, wrapping/nesting partials, validation errors, repr, and re-export identity.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
optree/functools.py |
Conditional import/re-export of Placeholder, __all__ update, and docstring updates for partial. |
tests/test_functools.py |
Adds Placeholder-focused test coverage for roundtrips, calling behavior, tree interactions, and validation/repr. |
CHANGELOG.md |
Records the new Placeholder support/re-export under [Unreleased] > Added. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
c2a5336 to
3e538e8
Compare
…artial` Re-export `functools.Placeholder` (Python 3.14+) from `optree.functools` and add comprehensive tests verifying that `optree.functools.partial` works consistently with stdlib's `functools.partial` when Placeholders are used in positional arguments.
Description
Add
functools.Placeholder(Python 3.14+, PEP 309 extension) support foroptree.functools.partialand re-export it fromoptree.functoolsfor user convenience.Since
optree.functools.partialsubclassesfunctools.partial, Placeholder already works out of the box — construction, calling, validation (trailing/keyword rejection), and repr are all handled by the stdlib's C implementation. This PR adds the re-export, documentation, and comprehensive test coverage.Changes
optree/functools.pyfunctools.Placeholderon Python 3.14+__all__to include'Placeholder'on Python 3.14+partialdocstringtests/test_functools.py— 14 new test functions:tree_leavesincludes Placeholder,tree_mapidentity preserves itfunctools.partialwith Placeholder (with/without extra args)optree.functools.partialwith PlaceholderCHANGELOG.md— Added entry under[Unreleased] > AddedDesign decisions
Placeholderobjects inpartial.argsare treated as leaves in the pytree, consistent with how any non-collection value inargsis handled. This preserves correct accessor paths (GetAttrEntry('args') → SequenceEntry(i)).__tree_flatten__/__tree_unflatten__: The existing implementation works correctly with Placeholder because it delegates construction tofunctools.partial.__new__().self.func,self.args,self.keywords, call behavior).Types of changes
Checklist
make format. (required)make lint. (required)make testpass. (required)