GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Replace pybind11 type casters with `mpl::convert` by iccir · Pull Request #32581 · matplotlib/matplotlib

Conversation

@iccir

@iccir iccir commented Nov 8, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

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, const char *arg_name)

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

void convert(agg::trans_affine &dst, const object &src, const char *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

  • I removed some unneeded template parameters (template <class PathIterator>) and replaced with the actual class (mpl::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

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
iccir added 14 commits October 26, 2026 10:59
@iccir
Fully adopt mpl::array and mpl::defer
4202b36
@iccir
Cleanup 1D case in transform_affine()
a70676f
# Conflicts:
#	src/_path.h
@iccir
Convert agg and path to mpl::array, remove unused code
7176460
@iccir
Add more defer<> templates
6b5141f
@iccir
Fix comment
a5f5e39
@iccir
hatchcolors should be an std::optional
7d278a0
@iccir
Port tkagg to use mpl::array
944d4fa
@iccir
Port convert_polygon_vector and Py_cleanup_path
4aacf36
@iccir
Adopt mpl::array in ft2font
5f2f49f
@iccir
We don't need asarray() until nanobind
95ac539
@iccir
Merge branch 'main' of https://github.com/matplotlib/matplotlib into …
dd73e17
…pybind-mplarray

# Conflicts:
#	lib/matplotlib/transforms.py
#	src/_path.h
#	src/_path_wrapper.cpp
@iccir
Fix _tri types
96ffd9e
@iccir
Merge branch 'pybind-mplarray' into pybind-defers
230054f
@iccir
Replace pybind11 type casters with mpl::convert
df69d73
@github-actions github-actions Bot added GUI: tk backend: agg topic: path handling topic: images topic: text/fonts topic: transforms and scales topic: contour topic: tri labels Nov 8, 2026
@iccir iccir added this to nanobind Nov 8, 2026
@github-project-automation github-project-automation Bot moved this to Todo in nanobind Nov 8, 2026
@iccir iccir moved this from Todo to In Progress in nanobind Nov 8, 2026
@iccir
Fix issue from local merge
3117cea
@iccir iccir mentioned this pull request Nov 8, 2026
nanobind Umbrella #32560
Open
@iccir
Fix remaining mypy errors
a6b5a09

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

Reviewers

No reviews

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

backend: agg GUI: tk topic: contour topic: images topic: path handling topic: text/fonts topic: transforms and scales topic: tri

Projects

nanobind
Status: In Progress

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

1 participant

@iccir
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.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.