(partially cherry-picked from #32432)
Part of Meta-ticket #32414
Depends on #32593
CC: @tscrim @fchapoton
Component: refactoring
Author: Matthias Koeppe
Branch/Commit: 907b57c
Reviewer: Travis Scrimshaw
Issue created by migration from https://trac.sagemath.org/ticket/32606