Add a fallback to the example TemplateMethod descriptor - #118
Open
kwabenantim wants to merge 1 commit into
Open
Conversation
The templated-method descriptor in the shapes/cells _syntax.py is assigned onto the class under the method's name, so a same-named plain (non-templated) overload would be shadowed and unreachable. Add an optional fallback: TemplateMethod now forwards a plain obj.Bar(...) call to it, or raises a helpful TypeError when the name is purely templated, mirroring pychaste's descriptor. Demonstrate it in the shapes example: UnitSquare::GetAreaIn is now overloaded with a plain GetAreaIn(perSquareMetre) alongside the templated GetAreaIn<UNIT>(). The primitives package passes the plain overload as the fallback, so square.GetAreaIn(factor) works beside square.GetAreaIn[SquareFeet]() (new testTemplateMethodFallback). Also clarify the _syntax.py module and descriptor docstrings/comments and add Usage examples, keeping the shapes and cells copies identical. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Contributor
There was a problem hiding this comment.
Pull request overview
Adds fallback support to TemplateMethod, allowing templated and plain overloads to coexist.
Changes:
- Extends the descriptor with callable fallback behavior.
- Adds a plain
UnitSquare::GetAreaIn(double)overload and binding. - Adds fallback integration tests and documentation.
Reviewed changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
examples/shapes/wrapper/primitives/UnitSquare.cppwg.cpp |
Binds the plain overload. |
examples/shapes/src/py/tests/test_classes.py |
Tests instance-level fallback dispatch. |
examples/shapes/src/py/pyshapes/primitives/__init__.py |
Configures the fallback. |
examples/shapes/src/py/pyshapes/_syntax.py |
Implements fallback calls. |
examples/shapes/src/cpp/primitives/UnitSquare.hpp |
Adds the plain overload. |
examples/cells/src/py/pycells/_syntax.py |
Mirrors descriptor enhancements. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
| raise TypeError( | ||
| f"{self._base_name} is templated; use {self._base_name}[Arg](...)" | ||
| ) | ||
| return self._fallback(self._target, *args, **kwargs) |
| raise TypeError( | ||
| f"{self._base_name} is templated; use {self._base_name}[Arg](...)" | ||
| ) | ||
| return self._fallback(self._target, *args, **kwargs) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #70