Hi Joe,
This issue has been fixed before for other slots such as Py_tp_members.
The right solution is to copy the contents of the PyMethodDef array into the heap type object itself. This guarantees that you will have valid pointers throughout the type’s life-cycle and they will be cleaned up once the type’s tp_dealloc function is called. Using this you can deallocate your PyMethodDef array immediately after creating the type.
You’ll need to create a PR to fix the issue by updating PyType_FromSpecWithBases. Feel free to reference this PR where I fixed this very same problem for Py_tp_members: https://github.com/python/cpython/pull/9665/files. Follow the example in PyStructSequence_NewType to dynamically create a PyMemberDef (PyMethodDef in your case), and the change in PyType_FromSpecWithBases that allocates the extra space needed to keep these pointers alive inside the type.
Feel free to ping me if you need any more help.
Best, Eddie