Skip to content

Commit 5ddba38

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 5ddba38

File tree

3 files changed

+11
-1
lines changed

3 files changed

+11
-1
lines changed

include/pybind11/pytypes.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1581,7 +1581,7 @@ str_attr_accessor object_api<D>::doc() const { return attr("__doc__"); }
15811581

15821582
template <typename D>
15831583
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); }
1584+
handle object_api<D>::get_type() const { return type::handle_of(derived()); }
15851585

15861586
template <typename D>
15871587
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)