Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions include/pybind11/cast.h
Original file line number Diff line number Diff line change
Expand Up @@ -1543,15 +1543,19 @@ struct handle_type_name<tuple_accessor> {

template <typename type>
struct pyobject_caster {
template <typename T = type, enable_if_t<std::is_same<T, handle>::value, int> = 0>
template <typename T = type,
enable_if_t<std::is_base_of<handle, T>::value && !std::is_base_of<object, T>::value,
int> = 0>
pyobject_caster() : value() {}

// `type` may not be default constructible (e.g. frozenset, anyset). Initializing `value`
// to a nil handle is safe since it will only be accessed if `load` succeeds.
template <typename T = type, enable_if_t<std::is_base_of<object, T>::value, int> = 0>
pyobject_caster() : value(reinterpret_steal<type>(handle())) {}

template <typename T = type, enable_if_t<std::is_same<T, handle>::value, int> = 0>
template <typename T = type,
enable_if_t<std::is_base_of<handle, T>::value && !std::is_base_of<object, T>::value,
int> = 0>
bool load(handle src, bool /* convert */) {
value = src;
return static_cast<bool>(value);
Expand Down
9 changes: 5 additions & 4 deletions include/pybind11/pybind11.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<drp> {
[pm](typing::Handle<T> c_hdl) -> std::shared_ptr<drp> {
std::shared_ptr<T> c_sp
= type_caster<std::shared_ptr<T>>::shared_ptr_with_responsible_parent(
c_hdl);
Expand Down Expand Up @@ -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<typename std::add_const<D>::type> {
[pm](
typing::Handle<T> c_hdl) -> std::shared_ptr<typename std::add_const<D>::type> {
std::shared_ptr<T> c_sp
= type_caster<std::shared_ptr<T>>::shared_ptr_with_responsible_parent(
c_hdl);
Expand All @@ -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<D> {
[pm](typing::Handle<T> c_hdl) -> std::shared_ptr<D> {
std::shared_ptr<T> c_sp
= type_caster<std::shared_ptr<T>>::shared_ptr_with_responsible_parent(
c_hdl);
Expand Down Expand Up @@ -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<T> c_hdl) -> D {
std::shared_ptr<T> c_sp
= type_caster<std::shared_ptr<T>>::shared_ptr_with_responsible_parent(
c_hdl);
Expand Down
10 changes: 7 additions & 3 deletions include/pybind11/pytypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -767,14 +767,18 @@ bool isinstance(handle obj) {
return T::check_(obj);
}

template <typename T, detail::enable_if_t<!std::is_base_of<object, T>::value, int> = 0>
template <typename T, detail::enable_if_t<!std::is_base_of<handle, T>::value, int> = 0>
bool isinstance(handle obj) {
return detail::isinstance_native_enum<T>(obj, typeid(T))
|| detail::isinstance_generic(obj, typeid(T));
}

template <>
bool isinstance<handle>(handle) = delete;
template <
typename T,
detail::enable_if_t<std::is_base_of<handle, T>::value && !std::is_base_of<object, T>::value,
int> = 0>
bool isinstance(handle) = delete;

template <>
inline bool isinstance<object>(handle obj) {
return obj.ptr() != nullptr;
Expand Down
34 changes: 34 additions & 0 deletions include/pybind11/typing.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 <typename T, bool Cast = false>
class Handle : public handle {
public:
Handle() = default;
using handle::handle;
// NOLINTNEXTLINE(google-explicit-constructor)
Handle(const handle &h) : handle(h) {}
};

template <typename T, bool Cast = false>
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 <typename K, typename V>
class Dict : public dict {
using dict::dict;
Expand Down Expand Up @@ -141,6 +163,18 @@ PYBIND11_NAMESPACE_END(typing)

PYBIND11_NAMESPACE_BEGIN(detail)

template <typename T, bool Cast>
struct handle_type_name<typing::Handle<T, Cast>> {
static constexpr auto name = ::pybind11::detail::const_name<Cast>(
make_caster<T>::name, ::pybind11::detail::return_descr(make_caster<T>::name));
};

template <typename T, bool Cast>
struct handle_type_name<typing::Object<T, Cast>> {
static constexpr auto name = ::pybind11::detail::const_name<Cast>(
make_caster<T>::name, ::pybind11::detail::return_descr(make_caster<T>::name));
};

template <typename... Types>
struct handle_type_name<typing::Tuple<Types...>> {
static constexpr auto name = const_name("tuple[")
Expand Down
17 changes: 17 additions & 0 deletions tests/test_methods_and_attributes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,13 @@ struct TestPropertiesOverride : TestProperties {
};
int TestPropertiesOverride::static_value = 99;

struct TestProperties2 {
TestProperties value;
TestProperties *raw_ptr = nullptr;
std::unique_ptr<TestProperties> unique_ptr;
std::shared_ptr<TestProperties> shared_ptr;
};

struct TestPropRVP {
UserType v1{1};
UserType v2{1};
Expand Down Expand Up @@ -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_<TestProperties2>(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); };
Expand Down
57 changes: 57 additions & 0 deletions tests/test_methods_and_attributes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
6 changes: 6 additions & 0 deletions tests/test_pytypes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -915,6 +915,12 @@ TEST_SUBMODULE(pytypes, m) {
return a;
});

m.def("annotate_handle",
[](py::typing::Handle<py::typing::List<int>>)
-> py::typing::Handle<py::typing::List<int>> { return py::handle(); });
m.def("annotate_object",
[](const py::typing::Object<py::typing::List<int>> &)
-> py::typing::Object<py::typing::List<int>> { return py::object(); });
m.def("annotate_tuple_float_str", [](const py::typing::Tuple<py::float_, py::str> &) {});
m.def("annotate_tuple_empty", [](const py::typing::Tuple<> &) {});
m.def("annotate_tuple_variable_length",
Expand Down
8 changes: 8 additions & 0 deletions tests/test_pytypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading