Skip to content

Add a fallback to the example TemplateMethod descriptor - #118

Open
kwabenantim wants to merge 1 commit into
developfrom
70-template-syntax-fallback
Open

Add a fallback to the example TemplateMethod descriptor#118
kwabenantim wants to merge 1 commit into
developfrom
70-template-syntax-fallback

Conversation

@kwabenantim

Copy link
Copy Markdown
Member

Closes #70

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>
@kwabenantim
kwabenantim requested a balanced review from Copilot August 10, 2026 21:36
@kwabenantim
kwabenantim marked this pull request as ready for review August 10, 2026 21:36

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Python Syntax for Templated Methods

2 participants