Save state for reverting on application of animation modifier

I want to create an animation modifier that, on application, saves some state so that it can revert the operation properly when requested. I am trying to save that state as variables of the modifier, but it does not seem to work.

Here is a very basic example:

In this modifier I just print a string variable both on apply and on revert. After applying, I modify the value of that variable, so I would expect that on revert I see that modified string value printed. However, that is not the case, I only see “Default message” both on apply and on revert.

In my real use case I want to store other information than strings, but it is the same idea.

Is there a way to get this to work? Or something equivalent? It seems very strange that animation modifiers cannot store a state for reverting, it constrains a lot the operations that you can apply and revert properly with a modifier.