From 1d12b1158295c31587f0c0b61b68d8cb7ef65b5c Mon Sep 17 00:00:00 2001 From: gentlegiantJGC Date: Tue, 17 Nov 2026 11:42:41 +0000 Subject: [PATCH 01/19] Add tests for property docstrings --- tests/test_methods_and_attributes.cpp | 17 ++++++++ tests/test_methods_and_attributes.py | 57 +++++++++++++++++++++++++++ 2 files changed, 74 insertions(+) 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, From b93682ea214edfe2a438758d9bdec2b2b9d0a310 Mon Sep 17 00:00:00 2001 From: gentlegiantJGC Date: Tue, 17 Nov 2026 12:17:35 +0000 Subject: [PATCH 02/19] Add type hinted subclasses of handle and object The classes can be used as Handle and Object and will be type hinted as T instead of object. This does not change the runtime behaviour. --- include/pybind11/typing.h | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/include/pybind11/typing.h b/include/pybind11/typing.h index 4b027dbfea..963095d328 100644 --- a/include/pybind11/typing.h +++ b/include/pybind11/typing.h @@ -36,6 +36,16 @@ 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 { + using handle::handle; +}; + +template +class Object : public object { + using object::object; +}; + template class Dict : public dict { using dict::dict; @@ -141,6 +151,16 @@ PYBIND11_NAMESPACE_END(typing) PYBIND11_NAMESPACE_BEGIN(detail) +template +struct handle_type_name> { + static constexpr auto name = make_caster::name; +}; + +template +struct handle_type_name> { + static constexpr auto name = make_caster::name; +}; + template struct handle_type_name> { static constexpr auto name = const_name("tuple[") From 7155dcd3e9df98b60f3966ef65e31ee55dd96559 Mon Sep 17 00:00:00 2001 From: gentlegiantJGC Date: Tue, 17 Nov 2026 12:18:39 +0000 Subject: [PATCH 03/19] Correctly type hint self argument --- include/pybind11/pybind11.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/include/pybind11/pybind11.h b/include/pybind11/pybind11.h index 2459a12f5f..76fbd9cbe8 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,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::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 +1178,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 +1220,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); From 680af28dc6749ebc8ddadee50d350dfe87c56931 Mon Sep 17 00:00:00 2001 From: gentlegiantJGC Date: Tue, 17 Nov 2026 13:27:55 +0000 Subject: [PATCH 04/19] Add tests --- tests/test_pytypes.cpp | 2 ++ tests/test_pytypes.py | 14 ++++++++++++++ 2 files changed, 16 insertions(+) diff --git a/tests/test_pytypes.cpp b/tests/test_pytypes.cpp index afa344536f..a6d68bfe5a 100644 --- a/tests/test_pytypes.cpp +++ b/tests/test_pytypes.cpp @@ -915,6 +915,8 @@ TEST_SUBMODULE(pytypes, m) { return a; }); + m.def("annotate_handle", [](py::typing::Handle>) -> py::typing::Handle> { return py::handle(); }); + m.def("annotate_object", [](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..270d86233d 100644 --- a/tests/test_pytypes.py +++ b/tests/test_pytypes.py @@ -1029,6 +1029,20 @@ 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) From 03f4d6f0ddcc1f082dce9e70e60f8f39ff90d9dc Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 17 Nov 2026 13:28:27 +0000 Subject: [PATCH 05/19] style: pre-commit fixes --- include/pybind11/pybind11.h | 3 ++- tests/test_pytypes.cpp | 8 ++++++-- tests/test_pytypes.py | 10 ++-------- 3 files changed, 10 insertions(+), 11 deletions(-) diff --git a/include/pybind11/pybind11.h b/include/pybind11/pybind11.h index 76fbd9cbe8..2cdccf1d74 100644 --- a/include/pybind11/pybind11.h +++ b/include/pybind11/pybind11.h @@ -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](typing::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); diff --git a/tests/test_pytypes.cpp b/tests/test_pytypes.cpp index a6d68bfe5a..461c24cc10 100644 --- a/tests/test_pytypes.cpp +++ b/tests/test_pytypes.cpp @@ -915,8 +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", [](py::typing::Object>) -> py::typing::Object> { return py::object(); }); + m.def("annotate_handle", + [](py::typing::Handle>) + -> py::typing::Handle> { return py::handle(); }); + m.def("annotate_object", + [](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 270d86233d..18fbd5609d 100644 --- a/tests/test_pytypes.py +++ b/tests/test_pytypes.py @@ -1030,17 +1030,11 @@ def test_inplace_rshift(a, b): def test_typing_handle_annotations(doc): - assert ( - doc(m.annotate_handle) - == "annotate_handle(arg0: list[int]) -> list[int]" - ) + 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]" - ) + assert doc(m.annotate_object) == "annotate_object(arg0: list[int]) -> list[int]" def test_tuple_nonempty_annotations(doc): From fc6ab3b75e9faf3b3e71ae1fe89c72e9b2d5040a Mon Sep 17 00:00:00 2001 From: gentlegiantJGC Date: Tue, 17 Nov 2026 13:58:56 +0000 Subject: [PATCH 06/19] Expand pyobject_caster constructors This did not match py::typing::Handle because it was only looking for py::handle --- include/pybind11/cast.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/pybind11/cast.h b/include/pybind11/cast.h index 79e743d9bb..d209b051ec 100644 --- a/include/pybind11/cast.h +++ b/include/pybind11/cast.h @@ -1543,7 +1543,7 @@ 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 +1551,7 @@ 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); From d194d5875fcad26b3674a81b4cbc3d6388a45553 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 17 Nov 2026 13:59:30 +0000 Subject: [PATCH 07/19] style: pre-commit fixes --- include/pybind11/cast.h | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/include/pybind11/cast.h b/include/pybind11/cast.h index d209b051ec..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 && !std::is_base_of::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 && !std::is_base_of::value, int> = 0> + template ::value && !std::is_base_of::value, + int> = 0> bool load(handle src, bool /* convert */) { value = src; return static_cast(value); From b875909592fbe95607332f4296aab0fb3831d645 Mon Sep 17 00:00:00 2001 From: gentlegiantJGC Date: Tue, 17 Nov 2026 15:13:02 +0000 Subject: [PATCH 08/19] Add constructors --- include/pybind11/typing.h | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/include/pybind11/typing.h b/include/pybind11/typing.h index 963095d328..3ee879c137 100644 --- a/include/pybind11/typing.h +++ b/include/pybind11/typing.h @@ -38,12 +38,21 @@ PYBIND11_NAMESPACE_BEGIN(typing) 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 { - using object::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)) {} }; template From efb7deb835b0e3486d6b3a2766527b3c2c89f198 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 17 Nov 2026 15:13:37 +0000 Subject: [PATCH 09/19] style: pre-commit fixes --- include/pybind11/typing.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/pybind11/typing.h b/include/pybind11/typing.h index 3ee879c137..b7f6638d4e 100644 --- a/include/pybind11/typing.h +++ b/include/pybind11/typing.h @@ -50,9 +50,9 @@ class Object : public object { public: Object() = default; // NOLINTNEXTLINE(google-explicit-constructor) - Object(const object& o) : object(o) {} + Object(const object &o) : object(o) {} // NOLINTNEXTLINE(google-explicit-constructor) - Object(object&& other) noexcept : object(std::move(other)) {} + Object(object &&other) noexcept : object(std::move(other)) {} }; template From e670f7b7d21d3fa6bfd75401d056cc8f1d26d7c2 Mon Sep 17 00:00:00 2001 From: gentlegiantJGC Date: Tue, 17 Nov 2026 15:24:43 +0000 Subject: [PATCH 10/19] Inherit constructors --- include/pybind11/typing.h | 1 + 1 file changed, 1 insertion(+) diff --git a/include/pybind11/typing.h b/include/pybind11/typing.h index b7f6638d4e..2b1f36a561 100644 --- a/include/pybind11/typing.h +++ b/include/pybind11/typing.h @@ -53,6 +53,7 @@ class Object : public object { Object(const object &o) : object(o) {} // NOLINTNEXTLINE(google-explicit-constructor) Object(object &&other) noexcept : object(std::move(other)) {} + using object::object; }; template From 16defd84b5457956b426a4ca61f9bf1bac824fd6 Mon Sep 17 00:00:00 2001 From: gentlegiantJGC Date: Tue, 17 Nov 2026 15:35:22 +0000 Subject: [PATCH 11/19] Replace constructor inheritance and define check_ --- include/pybind11/typing.h | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/include/pybind11/typing.h b/include/pybind11/typing.h index 2b1f36a561..3d21361f59 100644 --- a/include/pybind11/typing.h +++ b/include/pybind11/typing.h @@ -53,7 +53,9 @@ class Object : public object { Object(const object &o) : object(o) {} // NOLINTNEXTLINE(google-explicit-constructor) Object(object &&other) noexcept : object(std::move(other)) {} - using object::object; + 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 From 9a0ea18cc1e2267d90540834506e331150f71f55 Mon Sep 17 00:00:00 2001 From: gentlegiantJGC Date: Tue, 17 Nov 2026 17:40:30 +0000 Subject: [PATCH 12/19] Make the non-casting type hint the default --- include/pybind11/typing.h | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/include/pybind11/typing.h b/include/pybind11/typing.h index 3d21361f59..05a3eb0a9d 100644 --- a/include/pybind11/typing.h +++ b/include/pybind11/typing.h @@ -163,14 +163,20 @@ PYBIND11_NAMESPACE_END(typing) PYBIND11_NAMESPACE_BEGIN(detail) -template +template struct handle_type_name> { - static constexpr auto name = make_caster::name; + static constexpr auto name = + ::pybind11::detail::const_name( + make_caster::name, + ::pybind11::detail::return_descr(make_caster::name)); }; -template +template struct handle_type_name> { - static constexpr auto name = make_caster::name; + static constexpr auto name = + ::pybind11::detail::const_name( + make_caster::name, + ::pybind11::detail::return_descr(make_caster::name)); }; template From 734d039554ff790f0fd7e7fbabe35980dc2f363f Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 17 Nov 2026 17:41:02 +0000 Subject: [PATCH 13/19] style: pre-commit fixes --- include/pybind11/typing.h | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/include/pybind11/typing.h b/include/pybind11/typing.h index 05a3eb0a9d..3a866d3388 100644 --- a/include/pybind11/typing.h +++ b/include/pybind11/typing.h @@ -165,18 +165,14 @@ 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)); + 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)); + static constexpr auto name = ::pybind11::detail::const_name( + make_caster::name, ::pybind11::detail::return_descr(make_caster::name)); }; template From b49baf1bcdbd1ed96931833505ff8a0c4fe98fa0 Mon Sep 17 00:00:00 2001 From: gentlegiantJGC Date: Tue, 17 Nov 2026 17:44:36 +0000 Subject: [PATCH 14/19] Fix template --- include/pybind11/typing.h | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/include/pybind11/typing.h b/include/pybind11/typing.h index 3a866d3388..1a728d6fd0 100644 --- a/include/pybind11/typing.h +++ b/include/pybind11/typing.h @@ -36,7 +36,7 @@ PYBIND11_NAMESPACE_BEGIN(typing) // Tuple type hint defined in cast.h for use in py::make_tuple to avoid circular includes -template +template class Handle : public handle { public: Handle() = default; @@ -45,7 +45,7 @@ class Handle : public handle { Handle(const handle &h) : handle(h) {} }; -template +template class Object : public object { public: Object() = default; @@ -163,14 +163,14 @@ PYBIND11_NAMESPACE_END(typing) PYBIND11_NAMESPACE_BEGIN(detail) -template -struct handle_type_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> { +template +struct handle_type_name> { static constexpr auto name = ::pybind11::detail::const_name( make_caster::name, ::pybind11::detail::return_descr(make_caster::name)); }; From 5d37a62b003dd25ced1a9709e4e861bc7c322f0f Mon Sep 17 00:00:00 2001 From: gentlegiantJGC Date: Tue, 17 Nov 2026 18:00:36 +0000 Subject: [PATCH 15/19] Fix lint error --- tests/test_pytypes.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_pytypes.cpp b/tests/test_pytypes.cpp index 461c24cc10..1b121f0303 100644 --- a/tests/test_pytypes.cpp +++ b/tests/test_pytypes.cpp @@ -919,7 +919,7 @@ TEST_SUBMODULE(pytypes, m) { [](py::typing::Handle>) -> py::typing::Handle> { return py::handle(); }); m.def("annotate_object", - [](py::typing::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<> &) {}); From 66bbd354d3c8369bbcfa4ebb4907c904124917c9 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 17 Nov 2026 18:01:40 +0000 Subject: [PATCH 16/19] style: pre-commit fixes --- tests/test_pytypes.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_pytypes.cpp b/tests/test_pytypes.cpp index 1b121f0303..f1a3d951e5 100644 --- a/tests/test_pytypes.cpp +++ b/tests/test_pytypes.cpp @@ -919,7 +919,7 @@ TEST_SUBMODULE(pytypes, m) { [](py::typing::Handle>) -> py::typing::Handle> { return py::handle(); }); m.def("annotate_object", - [](const py::typing::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<> &) {}); From 0d464867bd73b7de6fe48807e49317b827794fda Mon Sep 17 00:00:00 2001 From: gentlegiantJGC Date: Fri, 20 Nov 2026 10:12:05 +0000 Subject: [PATCH 17/19] Delete isinstance --- include/pybind11/typing.h | 3 +++ 1 file changed, 3 insertions(+) diff --git a/include/pybind11/typing.h b/include/pybind11/typing.h index 1a728d6fd0..d1e2a43383 100644 --- a/include/pybind11/typing.h +++ b/include/pybind11/typing.h @@ -161,6 +161,9 @@ class TypeVar : public object { PYBIND11_NAMESPACE_END(typing) +template <> +bool isinstance(handle) = delete; + PYBIND11_NAMESPACE_BEGIN(detail) template From 5ea92c988b0e91019ac7e6e93b1a60a642ef1e69 Mon Sep 17 00:00:00 2001 From: gentlegiantJGC Date: Fri, 20 Nov 2026 11:14:34 +0000 Subject: [PATCH 18/19] Fix isinstance --- include/pybind11/pytypes.h | 7 ++++--- include/pybind11/typing.h | 3 --- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/include/pybind11/pytypes.h b/include/pybind11/pytypes.h index adf512cc23..12b603c447 100644 --- a/include/pybind11/pytypes.h +++ b/include/pybind11/pytypes.h @@ -767,14 +767,15 @@ 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 ::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 d1e2a43383..1a728d6fd0 100644 --- a/include/pybind11/typing.h +++ b/include/pybind11/typing.h @@ -161,9 +161,6 @@ class TypeVar : public object { PYBIND11_NAMESPACE_END(typing) -template <> -bool isinstance(handle) = delete; - PYBIND11_NAMESPACE_BEGIN(detail) template From a2f4b430abf08ef1fcff54e57210571d37cc7bbf Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 20 Nov 2026 11:15:06 +0000 Subject: [PATCH 19/19] style: pre-commit fixes --- include/pybind11/pytypes.h | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/include/pybind11/pytypes.h b/include/pybind11/pytypes.h index 12b603c447..87ef7f0e2f 100644 --- a/include/pybind11/pytypes.h +++ b/include/pybind11/pytypes.h @@ -773,7 +773,10 @@ bool isinstance(handle obj) { || detail::isinstance_generic(obj, typeid(T)); } -template ::value && !std::is_base_of::value, int> = 0> +template < + typename T, + detail::enable_if_t::value && !std::is_base_of::value, + int> = 0> bool isinstance(handle) = delete; template <>