How to Fix Axis of Armature Without Wrecking Mesh?
You should just be able to correct bone roll in edit mode, which won't deform the mesh. This may still cause issues if you have some constraints specified in world space, but should otherwise be fine. In python this would be changing the EditBone, not the PoseBone.