diff --git a/include/pybind11/detail/init.h b/include/pybind11/detail/init.h index 3ef78c1179f..376f1142b10 100644 --- a/include/pybind11/detail/init.h +++ b/include/pybind11/detail/init.h @@ -293,6 +293,11 @@ template ::value, int> = 0> void setstate(value_and_holder &v_h, std::pair &&result, bool need_alias) { construct(v_h, std::move(result.first), need_alias); + auto d = handle(result.second); + if (PyDict_Check(d.ptr()) && PyDict_Size(d.ptr()) == 0) { + // Skipping setattr below, to not force use of py::dynamic_attr() for Class unnecessarily. + return; + } setattr((PyObject *) v_h.inst, "__dict__", result.second); }