Skip to content

Commit 636d5cf

Browse files
committed
refactor: use py::type::of
1 parent e950722 commit 636d5cf

File tree

4 files changed

+29
-12
lines changed

4 files changed

+29
-12
lines changed

docs/advanced/classes.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1247,6 +1247,6 @@ object, just like ``type(ob)`` in Python.
12471247

12481248
.. note::
12491249

1250-
Other types, like ``py::type::of<int>``, do not work, see :ref:`type-conversions`.
1250+
Other types, like ``py::type::of<int>()``, do not work, see :ref:`type-conversions`.
12511251

12521252
.. versionadded:: 2.6

include/pybind11/pytypes.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -895,10 +895,9 @@ class iterator : public object {
895895

896896
class type : public object {
897897
public:
898-
PYBIND11_OBJECT_COMMON(type, object, PyType_Check)
898+
PYBIND11_OBJECT(type, object, PyType_Check)
899899

900-
explicit type(handle h): type((PyObject*) Py_TYPE(h.ptr()), borrowed_t{}) {}
901-
explicit type(object ob): type((PyObject*) Py_TYPE(ob.ptr()), borrowed_t{}) {}
900+
static type of(handle h) { return type((PyObject*) Py_TYPE(h.ptr()), borrowed_t{}); }
902901

903902
/// Convert C++ type to py::type if previously registered. Does not convert
904903
// standard types, like int, float. etc. yet.

tests/test_class.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,16 @@ TEST_SUBMODULE(class_, m) {
148148
return py::type::of<Invalid>();
149149
});
150150

151-
m.def("get_type", [](py::object ob) {
152-
return py::type(ob);
151+
m.def("get_type_of", [](py::object ob) {
152+
return py::type::of(ob);
153+
});
154+
155+
m.def("as_type", [](py::object ob) {
156+
auto tp = py::type(ob);
157+
if (py::isinstance<py::type>(ob))
158+
return tp;
159+
else
160+
throw std::runtime_error("Invalid type");
153161
});
154162

155163
// test_mismatched_holder

tests/test_class.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,25 @@ def test_type():
3939
# assert m.check_type(2) == int
4040

4141

42-
def test_type_py():
43-
assert m.get_type(1) == int
44-
assert m.get_type(m.DerivedClass1()) == m.DerivedClass1
45-
assert m.get_type(int) == type
42+
def test_type_of_py():
43+
assert m.get_type_of(1) == int
44+
assert m.get_type_of(m.DerivedClass1()) == m.DerivedClass1
45+
assert m.get_type_of(int) == type
4646

4747

48-
def test_type_py_nodelete():
48+
def test_type_of_py_nodelete():
4949
# If the above test deleted the class, this will segfault
50-
assert m.get_type(m.DerivedClass1()) == m.DerivedClass1
50+
assert m.get_type_of(m.DerivedClass1()) == m.DerivedClass1
51+
52+
53+
def test_as_type_py():
54+
assert m.as_type(int) == int
55+
56+
with pytest.raises(RuntimeError):
57+
assert m.as_type(1) == int
58+
59+
with pytest.raises(RuntimeError):
60+
assert m.as_type(m.DerivedClass1()) == m.DerivedClass1
5161

5262

5363
def test_docstrings(doc):

0 commit comments

Comments
 (0)