Skip to content

Commit 084660f

Browse files
committed
fix: names of types held by smart holder should be the actual type
Required for pybind11-stubgen to work properly
1 parent 5bbb8f9 commit 084660f

File tree

3 files changed

+29
-4
lines changed

3 files changed

+29
-4
lines changed

include/pybind11/detail/smart_holder_type_casters.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -703,7 +703,7 @@ struct smart_holder_type_caster : smart_holder_type_caster_load<T>,
703703
template <typename T>
704704
struct smart_holder_type_caster<std::shared_ptr<T>> : smart_holder_type_caster_load<T>,
705705
smart_holder_type_caster_class_hooks {
706-
static constexpr auto name = const_name<std::shared_ptr<T>>();
706+
static constexpr auto name = const_name<T>();
707707

708708
static handle cast(const std::shared_ptr<T> &src, return_value_policy policy, handle parent) {
709709
switch (policy) {
@@ -760,7 +760,7 @@ struct smart_holder_type_caster<std::shared_ptr<T>> : smart_holder_type_caster_l
760760
template <typename T>
761761
struct smart_holder_type_caster<std::shared_ptr<T const>> : smart_holder_type_caster_load<T>,
762762
smart_holder_type_caster_class_hooks {
763-
static constexpr auto name = const_name<std::shared_ptr<T const>>();
763+
static constexpr auto name = const_name<T>();
764764

765765
static handle
766766
cast(const std::shared_ptr<T const> &src, return_value_policy policy, handle parent) {
@@ -780,7 +780,7 @@ struct smart_holder_type_caster<std::shared_ptr<T const>> : smart_holder_type_ca
780780
template <typename T, typename D>
781781
struct smart_holder_type_caster<std::unique_ptr<T, D>> : smart_holder_type_caster_load<T>,
782782
smart_holder_type_caster_class_hooks {
783-
static constexpr auto name = const_name<std::unique_ptr<T, D>>();
783+
static constexpr auto name = const_name<T>();
784784

785785
static handle cast(std::unique_ptr<T, D> &&src, return_value_policy policy, handle parent) {
786786
if (policy != return_value_policy::automatic
@@ -857,7 +857,7 @@ struct smart_holder_type_caster<std::unique_ptr<T, D>> : smart_holder_type_caste
857857
template <typename T, typename D>
858858
struct smart_holder_type_caster<std::unique_ptr<T const, D>>
859859
: smart_holder_type_caster_load<T>, smart_holder_type_caster_class_hooks {
860-
static constexpr auto name = const_name<std::unique_ptr<T const, D>>();
860+
static constexpr auto name = const_name<T>();
861861

862862
static handle
863863
cast(std::unique_ptr<T const, D> &&src, return_value_policy policy, handle parent) {

tests/test_class_sh_basic.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,12 @@ TEST_SUBMODULE(class_sh_basic, m) {
154154
m.def("py_type_handle_of_atyp", []() {
155155
return py::type::handle_of<atyp>(); // Exercises static_cast in this function.
156156
});
157+
158+
// Checks for type names used as arguments
159+
m.def("args_shared_ptr", [](std::shared_ptr<atyp> p){ return p; });
160+
m.def("args_shared_ptr_const", [](std::shared_ptr<atyp const> p){ return p; });
161+
m.def("args_unique_ptr", [](std::unique_ptr<atyp> p){ return p; });
162+
m.def("args_unique_ptr_const", [](std::unique_ptr<atyp const> p){ return p; });
157163
}
158164

159165
} // namespace class_sh_basic

tests/test_class_sh_basic.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,3 +163,22 @@ def test_unique_ptr_consumer_roundtrip(pass_f, rtrn_f, moved_out, moved_in):
163163
def test_py_type_handle_of_atyp():
164164
obj = m.py_type_handle_of_atyp()
165165
assert obj.__class__.__name__ == "pybind11_type"
166+
167+
168+
def test_function_signatures(doc):
169+
assert (
170+
doc(m.args_shared_ptr)
171+
== "args_shared_ptr(arg0: m.class_sh_basic.atyp) -> m.class_sh_basic.atyp"
172+
)
173+
assert (
174+
doc(m.args_shared_ptr_const)
175+
== "args_shared_ptr_const(arg0: m.class_sh_basic.atyp) -> m.class_sh_basic.atyp"
176+
)
177+
assert (
178+
doc(m.args_unique_ptr)
179+
== "args_unique_ptr(arg0: m.class_sh_basic.atyp) -> m.class_sh_basic.atyp"
180+
)
181+
assert (
182+
doc(m.args_unique_ptr_const)
183+
== "args_unique_ptr_const(arg0: m.class_sh_basic.atyp) -> m.class_sh_basic.atyp"
184+
)

0 commit comments

Comments
 (0)