diff --git a/include/pybind11/eigen/matrix.h b/include/pybind11/eigen/matrix.h index 2f2d2cb2c0..47483def69 100644 --- a/include/pybind11/eigen/matrix.h +++ b/include/pybind11/eigen/matrix.h @@ -880,7 +880,11 @@ struct type_caster::value>> { } static handle cast(const Type &src, return_value_policy /* policy */, handle /* parent */) { - const_cast(src).makeCompressed(); + if constexpr (!std::is_same_v(src).makeCompressed(); + // use type specific operations... + } + object matrix_type = module_::import("scipy.sparse").attr(rowMajor ? "csr_matrix" : "csc_matrix");