diff --git a/include/pybind11/cast.h b/include/pybind11/cast.h index 79e743d9bb..53a98b0154 100644 --- a/include/pybind11/cast.h +++ b/include/pybind11/cast.h @@ -1543,7 +1543,9 @@ struct handle_type_name { template struct pyobject_caster { - template ::value, int> = 0> + template ::value && !std::is_base_of::value, + int> = 0> pyobject_caster() : value() {} // `type` may not be default constructible (e.g. frozenset, anyset). Initializing `value` @@ -1551,7 +1553,9 @@ struct pyobject_caster { template ::value, int> = 0> pyobject_caster() : value(reinterpret_steal(handle())) {} - template ::value, int> = 0> + template ::value && !std::is_base_of::value, + int> = 0> bool load(handle src, bool /* convert */) { value = src; return static_cast(value); diff --git a/include/pybind11/pybind11.h b/include/pybind11/pybind11.h index 2459a12f5f..2cdccf1d74 100644 --- a/include/pybind11/pybind11.h +++ b/include/pybind11/pybind11.h @@ -1121,7 +1121,7 @@ struct property_cpp_function_sh_raw_ptr_member { type_info *tinfo = get_type_info(typeid(T), /*throw_if_missing=*/true); if (tinfo->holder_enum_v == holder_enum_t::smart_holder) { return cpp_function( - [pm](handle c_hdl) -> std::shared_ptr { + [pm](typing::Handle c_hdl) -> std::shared_ptr { std::shared_ptr c_sp = type_caster>::shared_ptr_with_responsible_parent( c_hdl); @@ -1161,7 +1161,8 @@ struct property_cpp_function_sh_member_held_by_value { type_info *tinfo = get_type_info(typeid(T), /*throw_if_missing=*/true); if (tinfo->holder_enum_v == holder_enum_t::smart_holder) { return cpp_function( - [pm](handle c_hdl) -> std::shared_ptr::type> { + [pm]( + typing::Handle c_hdl) -> std::shared_ptr::type> { std::shared_ptr c_sp = type_caster>::shared_ptr_with_responsible_parent( c_hdl); @@ -1178,7 +1179,7 @@ struct property_cpp_function_sh_member_held_by_value { type_info *tinfo = get_type_info(typeid(T), /*throw_if_missing=*/true); if (tinfo->holder_enum_v == holder_enum_t::smart_holder) { return cpp_function( - [pm](handle c_hdl) -> std::shared_ptr { + [pm](typing::Handle c_hdl) -> std::shared_ptr { std::shared_ptr c_sp = type_caster>::shared_ptr_with_responsible_parent( c_hdl); @@ -1220,7 +1221,7 @@ struct property_cpp_function_sh_unique_ptr_member { type_info *tinfo = get_type_info(typeid(T), /*throw_if_missing=*/true); if (tinfo->holder_enum_v == holder_enum_t::smart_holder) { return cpp_function( - [pm](handle c_hdl) -> D { + [pm](typing::Handle c_hdl) -> D { std::shared_ptr c_sp = type_caster>::shared_ptr_with_responsible_parent( c_hdl); diff --git a/include/pybind11/pytypes.h b/include/pybind11/pytypes.h index adf512cc23..87ef7f0e2f 100644 --- a/include/pybind11/pytypes.h +++ b/include/pybind11/pytypes.h @@ -767,14 +767,18 @@ bool isinstance(handle obj) { return T::check_(obj); } -template ::value, int> = 0> +template ::value, int> = 0> bool isinstance(handle obj) { return detail::isinstance_native_enum(obj, typeid(T)) || detail::isinstance_generic(obj, typeid(T)); } -template <> -bool isinstance(handle) = delete; +template < + typename T, + detail::enable_if_t::value && !std::is_base_of::value, + int> = 0> +bool isinstance(handle) = delete; + template <> inline bool isinstance(handle obj) { return obj.ptr() != nullptr; diff --git a/include/pybind11/typing.h b/include/pybind11/typing.h index 4b027dbfea..1a728d6fd0 100644 --- a/include/pybind11/typing.h +++ b/include/pybind11/typing.h @@ -36,6 +36,28 @@ PYBIND11_NAMESPACE_BEGIN(typing) // Tuple type hint defined in cast.h for use in py::make_tuple to avoid circular includes +template +class Handle : public handle { +public: + Handle() = default; + using handle::handle; + // NOLINTNEXTLINE(google-explicit-constructor) + Handle(const handle &h) : handle(h) {} +}; + +template +class Object : public object { +public: + Object() = default; + // NOLINTNEXTLINE(google-explicit-constructor) + Object(const object &o) : object(o) {} + // NOLINTNEXTLINE(google-explicit-constructor) + Object(object &&other) noexcept : object(std::move(other)) {} + Object(handle h, borrowed_t) : object(h, borrowed_t{}) {} + Object(handle h, stolen_t) : object(h, stolen_t{}) {} + static bool check_(handle h) noexcept { return h.ptr() != nullptr; } +}; + template class Dict : public dict { using dict::dict; @@ -141,6 +163,18 @@ PYBIND11_NAMESPACE_END(typing) PYBIND11_NAMESPACE_BEGIN(detail) +template +struct handle_type_name> { + static constexpr auto name = ::pybind11::detail::const_name( + make_caster::name, ::pybind11::detail::return_descr(make_caster::name)); +}; + +template +struct handle_type_name> { + static constexpr auto name = ::pybind11::detail::const_name( + make_caster::name, ::pybind11::detail::return_descr(make_caster::name)); +}; + template struct handle_type_name> { static constexpr auto name = const_name("tuple[") diff --git a/tests/test_methods_and_attributes.cpp b/tests/test_methods_and_attributes.cpp index 7fa7555fc7..c0a2a2272d 100644 --- a/tests/test_methods_and_attributes.cpp +++ b/tests/test_methods_and_attributes.cpp @@ -105,6 +105,13 @@ struct TestPropertiesOverride : TestProperties { }; int TestPropertiesOverride::static_value = 99; +struct TestProperties2 { + TestProperties value; + TestProperties *raw_ptr = nullptr; + std::unique_ptr unique_ptr; + std::shared_ptr shared_ptr; +}; + struct TestPropRVP { UserType v1{1}; UserType v2{1}; @@ -467,6 +474,16 @@ TEST_SUBMODULE(methods_and_attributes, m) { .def_readonly("def_readonly", &TestPropertiesOverride::value) .def_readonly_static("def_readonly_static", &TestPropertiesOverride::static_value); + py::class_(m, "TestProperties2") + .def(py::init<>()) + .def_readonly("def_readonly_value", &TestProperties2::value) + .def_readwrite("def_readwrite_value", &TestProperties2::value) + .def_readonly("def_readonly_raw_ptr", &TestProperties2::raw_ptr) + .def_readwrite("def_readwrite_raw_ptr", &TestProperties2::raw_ptr) + .def_readwrite("def_readwrite_unique_ptr", &TestProperties2::unique_ptr) + .def_readonly("def_readonly_shared_ptr", &TestProperties2::shared_ptr) + .def_readwrite("def_readwrite_shared_ptr", &TestProperties2::shared_ptr); + auto static_get1 = [](const py::object &) -> const UserType & { return TestPropRVP::sv1; }; auto static_get2 = [](const py::object &) -> const UserType & { return TestPropRVP::sv2; }; auto static_set1 = [](const py::object &, int v) { TestPropRVP::sv1.set(v); }; diff --git a/tests/test_methods_and_attributes.py b/tests/test_methods_and_attributes.py index 1d5d8ae32b..4efc686e11 100644 --- a/tests/test_methods_and_attributes.py +++ b/tests/test_methods_and_attributes.py @@ -220,6 +220,63 @@ def test_properties_docstring(): ) +TestProperties2GetDocstring = "(self: pybind11_tests.methods_and_attributes.TestProperties2) -> pybind11_tests.methods_and_attributes.TestProperties" +TestProperties2SetDocstring = "(self: pybind11_tests.methods_and_attributes.TestProperties2, arg0: pybind11_tests.methods_and_attributes.TestProperties) -> None" + + +def test_properties_2_docstring(): + assert ( + m.TestProperties2.def_readonly_value.fget.__doc__.strip() + == TestProperties2GetDocstring + ) + + assert ( + m.TestProperties2.def_readwrite_value.fget.__doc__.strip() + == TestProperties2GetDocstring + ) + assert ( + m.TestProperties2.def_readwrite_value.fset.__doc__.strip() + == TestProperties2SetDocstring + ) + + assert ( + m.TestProperties2.def_readonly_raw_ptr.fget.__doc__.strip() + == TestProperties2GetDocstring + ) + + assert ( + m.TestProperties2.def_readwrite_raw_ptr.fget.__doc__.strip() + == TestProperties2GetDocstring + ) + assert ( + m.TestProperties2.def_readwrite_raw_ptr.fset.__doc__.strip() + == TestProperties2SetDocstring + ) + + assert ( + m.TestProperties2.def_readwrite_unique_ptr.fget.__doc__.strip() + == TestProperties2GetDocstring + ) + assert ( + m.TestProperties2.def_readwrite_unique_ptr.fset.__doc__.strip() + == TestProperties2SetDocstring + ) + + assert ( + m.TestProperties2.def_readonly_shared_ptr.fget.__doc__.strip() + == TestProperties2GetDocstring + ) + + assert ( + m.TestProperties2.def_readwrite_shared_ptr.fget.__doc__.strip() + == TestProperties2GetDocstring + ) + assert ( + m.TestProperties2.def_readwrite_shared_ptr.fset.__doc__.strip() + == TestProperties2SetDocstring + ) + + @pytest.mark.xfail( reason="The signature of a cpp_function without is_method is fixed at construction", strict=True, diff --git a/tests/test_pytypes.cpp b/tests/test_pytypes.cpp index afa344536f..f1a3d951e5 100644 --- a/tests/test_pytypes.cpp +++ b/tests/test_pytypes.cpp @@ -915,6 +915,12 @@ TEST_SUBMODULE(pytypes, m) { return a; }); + m.def("annotate_handle", + [](py::typing::Handle>) + -> py::typing::Handle> { return py::handle(); }); + m.def("annotate_object", + [](const py::typing::Object> &) + -> py::typing::Object> { return py::object(); }); m.def("annotate_tuple_float_str", [](const py::typing::Tuple &) {}); m.def("annotate_tuple_empty", [](const py::typing::Tuple<> &) {}); m.def("annotate_tuple_variable_length", diff --git a/tests/test_pytypes.py b/tests/test_pytypes.py index 7e419b98e0..18fbd5609d 100644 --- a/tests/test_pytypes.py +++ b/tests/test_pytypes.py @@ -1029,6 +1029,14 @@ def test_inplace_rshift(a, b): assert m.inplace_rshift(a, b) == expected +def test_typing_handle_annotations(doc): + assert doc(m.annotate_handle) == "annotate_handle(arg0: list[int]) -> list[int]" + + +def test_typing_object_annotations(doc): + assert doc(m.annotate_object) == "annotate_object(arg0: list[int]) -> list[int]" + + def test_tuple_nonempty_annotations(doc): assert ( doc(m.annotate_tuple_float_str)