diff --git a/include/pybind11/pytypes.h b/include/pybind11/pytypes.h index d4c6f39ae..4e5d7f3f8 100644 --- a/include/pybind11/pytypes.h +++ b/include/pybind11/pytypes.h @@ -206,6 +206,18 @@ public: void operator=(handle value) && { Policy::set(obj, key, value); } void operator=(handle value) & { get_cache() = object(value, true); } + template + PYBIND11_DEPRECATED("Use of obj.attr(...) as bool is deprecated in favor of pybind11::hasattr(obj, ...)") + operator enable_if_t::value || + std::is_same::value, bool>() const { + return hasattr(obj, key); + } + template + PYBIND11_DEPRECATED("Use of obj[key] as bool is deprecated in favor of obj.contains(key)") + operator enable_if_t::value, bool>() const { + return obj.contains(key); + } + operator object() const { return get_cache(); } PyObject *ptr() const { return get_cache().ptr(); } template T cast() const { return get_cache().template cast(); }