Skip to content

Replace pybind11 type casters with mpl::convert - #32581

Draft
iccir wants to merge 17 commits into
matplotlib:mainfrom
iccir:pybind-mplconvert
Draft

iccir wants to merge 17 commits into
matplotlib:mainfrom
iccir:pybind-mplconvert

Conversation

@iccir

@iccir iccir commented Nov 8, 2026 •

Copy link
Copy Markdown
Contributor

PR summary

Important

This PR depends on #32536 and will appear larger than it really is until #32536 is merged. To view the actual changes in this PR, compare pybind-mplarray → pybind-mplconvert.

As mentioned in #32532 and in the nanobind Porting Guide, type casters may not raise exceptions:

Type casters may not raise C++ exceptions. Both from_python() and from_cpp() must be annotated with noexcept.

Since we used exceptions for error messages, I introduced mpl::defer to defer array creation until after a bound function was called. This PR extends mpl::defer to other types using a new mpl::convert function.

mpl::convert has the following signature:

using py::object; // or using nb::object

template <typename T>
void convert(T &dst, const object &src, std::string arg_name)

Each type caster is replaced by a mpl::convert overload:

void convert(agg::trans_affine &dst, const object &src, std::string arg_name)
{
    // If None assume identity transform
    if (src.is_none()) {
        return;
    }

    auto arr = mpl::array<double, mpl::shape<3, 3>>(src, arg_name);
    auto data = arr.data();

    dst.sx = data[0];
    dst.shx = data[1];
    dst.tx = data[2];
    dst.shy = data[3];
    dst.sy = data[4];
    dst.ty = data[5];
}

mpl::defer will will automatically use the appropriate convert() function when value() is called. This allows us to do the following:

static OutputPointArray
Py_transform_points(const mpl::defer<InputPointArray> &points_obj,
                    const mpl::defer<agg::trans_affine> &trans_obj)
{
    auto points = points_obj.value("points");
    auto trans = trans_obj.value("trans");

    auto result = OutputPointArray(points.shape(0));
    transform_points(points, trans, result);

    return result;
}

Additional Changes

  • mpl::PathIterator and mpl::PathGenerator are no longer in the mpl namespace. This matches other classes like GCAgg or the path converters.
  • I removed some unneeded template parameters (template <class PathIterator>) and replaced with the actual class (PathIterator).
  • I renamed some template parameters.
  • I removed "py_converters.cpp" as the converter is now inline in "py_converters.h".
  • I renamed some function arguments to match those in backend_bases.py.

AI Disclosure

  • I generally use AI as a web search engine.
  • I asked AI for examples on different approaching to implementing mpl::convert and hooking it up to mpl::defer. I used a variation of one of these approaches and wrote the code myself.

PR quality check

  • Use an expressive title, e.g. "Fix title font property precedence"
  • New and changed code is tested
  • [/] Plotting related features are demonstrated in an example
  • [/] New features and API changes have release notes
  • [/] Documentation complies with general and docstring guidelines

@iccir iccir mentioned this pull request Nov 8, 2026
@iccir iccir moved this from In Progress to Blocked in nanobind Nov 14, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Status: Blocked

Development

Successfully merging this pull request may close these issues.

1 participant