From a3a7b39f5cf466ef19c05070ef839105836c6674 Mon Sep 17 00:00:00 2001 From: Eric Cousineau Date: Sun, 10 Apr 2022 20:27:45 -0400 Subject: [PATCH 1/2] cast: Add is_generic_type --- include/pybind11/cast.h | 9 +++++++++ include/pybind11/pytypes.h | 9 ++++++++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/include/pybind11/cast.h b/include/pybind11/cast.h index 07a56e71f..a1efb5ed5 100644 --- a/include/pybind11/cast.h +++ b/include/pybind11/cast.h @@ -36,6 +36,15 @@ class type_caster : public type_caster_base {}; template using make_caster = type_caster>; +template +struct is_generic_type>::value>> + : public std::true_type {}; + +template +struct is_generic_type>::value>> + : public std::false_type {}; + // Shortcut for calling a caster's `cast_op_type` cast operator for casting a type_caster to a T template typename make_caster::template cast_op_type cast_op(make_caster &caster) { diff --git a/include/pybind11/pytypes.h b/include/pybind11/pytypes.h index ba0fda0a8..cf92e6827 100644 --- a/include/pybind11/pytypes.h +++ b/include/pybind11/pytypes.h @@ -38,6 +38,11 @@ PYBIND11_NAMESPACE_BEGIN(detail) class args_proxy; bool isinstance_generic(handle obj, const std::type_info &tp); +// Indicates that type is generic and and does not have a specialized +// `type_caster<>` specialization. Defined in `cast.h`. +template +struct is_generic_type; + // Accessor forward declarations template class accessor; @@ -476,7 +481,7 @@ inline void raise_from(error_already_set &err, PyObject *type, const char *messa /** \ingroup python_builtins \rst Return true if ``obj`` is an instance of ``T``. Type ``T`` must be a subclass of - `object` or a class which was exposed to Python as ``py::class_``. + `object` or a class which was exposed to Python as ``py::class_`` (generic). \endrst */ template ::value, int> = 0> bool isinstance(handle obj) { @@ -485,6 +490,8 @@ bool isinstance(handle obj) { template ::value, int> = 0> bool isinstance(handle obj) { + static_assert(detail::is_generic_type::value, + "isisntance() requires specialization for this type"); return detail::isinstance_generic(obj, typeid(T)); } From 58d4aaeedc7adeb3f9861c1ac6f574bd12421933 Mon Sep 17 00:00:00 2001 From: Eric Cousineau Date: Mon, 11 Apr 2022 14:15:11 -0400 Subject: [PATCH 2/2] Update include/pybind11/pytypes.h Co-authored-by: Aaron Gokaslan --- include/pybind11/pytypes.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/pybind11/pytypes.h b/include/pybind11/pytypes.h index cf92e6827..7658142fa 100644 --- a/include/pybind11/pytypes.h +++ b/include/pybind11/pytypes.h @@ -491,7 +491,7 @@ bool isinstance(handle obj) { template ::value, int> = 0> bool isinstance(handle obj) { static_assert(detail::is_generic_type::value, - "isisntance() requires specialization for this type"); + "isinstance() requires specialization for this type"); return detail::isinstance_generic(obj, typeid(T)); }