Skip to content

Commit 11f756f

Browse files
committed
fix: type bug intruduced in #2492
This now tests the old form too, and fixes the bug introduced.
1 parent dec33c2 commit 11f756f

File tree

3 files changed

+12
-3
lines changed

3 files changed

+12
-3
lines changed

include/pybind11/pytypes.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ class object_api : public pyobject_tag {
153153
/// Return the object's current reference count
154154
int ref_count() const { return static_cast<int>(Py_REFCNT(derived().ptr())); }
155155

156-
PYBIND11_DEPRECATED("Call py::type::handle_of(h) or py::type::of(h) instead of h.get_type()")
156+
// TODO PYBIND11_DEPRECATED("Call py::type::handle_of(h) or py::type::of(h) instead of h.get_type()")
157157
handle get_type() const;
158158

159159
private:
@@ -1580,8 +1580,7 @@ template <typename D>
15801580
str_attr_accessor object_api<D>::doc() const { return attr("__doc__"); }
15811581

15821582
template <typename D>
1583-
PYBIND11_DEPRECATED("Use py::type::of(h) instead of h.get_type()")
1584-
handle object_api<D>::get_type() const { return type::handle_of(*this); }
1583+
handle object_api<D>::get_type() const { return type::handle_of(derived()); }
15851584

15861585
template <typename D>
15871586
bool object_api<D>::rich_compare(object_api const &other, int value) const {

tests/test_class.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,10 @@ TEST_SUBMODULE(class_, m) {
152152
return py::type::of(ob);
153153
});
154154

155+
m.def("get_type_classic", [](py::handle h) {
156+
return h.get_type();
157+
});
158+
155159
m.def("as_type", [](py::object ob) {
156160
auto tp = py::type(ob);
157161
if (py::isinstance<py::type>(ob))

tests/test_class.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@ def test_type_of_py():
4545
assert m.get_type_of(int) == type
4646

4747

48+
def test_type_of_classic():
49+
assert m.get_type_classic(1) == int
50+
assert m.get_type_classic(m.DerivedClass1()) == m.DerivedClass1
51+
assert m.get_type_classic(int) == type
52+
53+
4854
def test_type_of_py_nodelete():
4955
# If the above test deleted the class, this will segfault
5056
assert m.get_type_of(m.DerivedClass1()) == m.DerivedClass1

0 commit comments

Comments
 (0)