Skip to content

Conversation

oremanj
Copy link
Collaborator

@oremanj oremanj commented Oct 14, 2025

Description

This is a pure refactor (no observable behavior changes) that centralizes the logic for choosing the C++ source object and type to use for a to-Python conversion. It replaces the old src_and_type() function (also written by yours truly, some eight years ago) that had some cumbersome interactions with smart_holder. It anticipates changes that will be needed in #5800 to provide more information to type_caster_generic::cast() for it to use when casting to a foreign type; such information will be carried in the cast_sources object. With cast_sources, the downcasting and type lookup behavior can be separated from the call to cast(), so it becomes possible to insert custom logic before or after the actual cast, which is especially useful when casting to foreign via a holder.

@oremanj oremanj requested a review from rwgk October 14, 2025 00:17
Copy link
Collaborator

@rwgk rwgk left a comment

Choose a reason for hiding this comment

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

The refactor is awesome!

My comments are purely about naming, please feel free to disregard, but I believe giving a few minutes of thought to intuitive (less generic) naming could help future maintainers and potential new contributors a lot to navigate the pybind11 code more easily.

Copy link
Collaborator

@rwgk rwgk left a comment

Choose a reason for hiding this comment

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

Awesome, thanks!

@oremanj oremanj merged commit a2c5971 into pybind:master Oct 15, 2025
85 checks passed
@github-actions github-actions bot added the needs changelog Possibly needs a changelog entry label Oct 15, 2025
@PhilipDeegan
Copy link
Contributor

PhilipDeegan commented Oct 15, 2025

It seems since the addition of this I am no longer able to compile.

I get a

pybind11/include/pybind11/detail/type_caster_base.h:1637:20: error: call of overloaded ‘cast(PHARE::pydata::PatchData<std::vector<double>, 1>*, pybind11::return_value_policy, pybind11::handle&)’ is ambiguous
 1637 |         return cast(std::addressof(src), return_value_policy::move, parent);

It's entirely possible we're using pybind wrong of course, but it was compiling

from a cursory glance, it seems I cannot have a something like this

template<typename T>
struct A{};

template<typename T, typename Y>
struct B{

     auto getA(){
         return A<T>{};
     }
}

class declaration

    py::class_<A<double>, py::smart_holder>(m, "ad");

    py::class_<B<double, double>, py::smart_holder>(m, "bdd")
         .def("getA", &B<double, double>::getA);
    py::class_<B<double, float>, py::smart_holder>(m, "bdf")
         .def("getA", &B<double, float>::getA);

@oremanj
Copy link
Collaborator Author

oremanj commented Oct 15, 2025

@PhilipDeegan I am unable to reproduce this compiler error using your example code. I somewhat doubt that you actually tested using your example code, because it doesn't compile (it's missing a semicolon after the definition of B). But if I fix that, it seems to compile just fine.

Please provide the entire compiler error. There should be quite a bit more after the two lines you pasted. There is probably some relevant content before those lines as well.

What compiler version and operating system are you running on?

Do you have any local customizations to pybind11 in your project?

It would be very helpful if you can provide a full example (not a sketch) that you have confirmed reproduces the problem.

@PhilipDeegan
Copy link
Contributor

PhilipDeegan commented Oct 15, 2025

@oremanj thanks for the reply our project is somewhat complex so I was trying to minimize the overhead

What compiler version and operating system are you running on?

several, locally I have gcc 14 and our CI is clang 19 and both seem to show this

full error log from our CI is here

the stack in our code is from here

the function call specifically is this

and the return type is a vector of this

Do you have any local customizations to pybind11 in your project?

not sure what this means

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

Labels

needs changelog Possibly needs a changelog entry

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants