How to prevent shearing caused by scale in hierarchies

Create two cubes, parent one to the other. Scale the parent on one axis. Rotate the child. The cube will be sheared. The same happens with bones.

Is there any way to prevent this, some matrix math?

I figured a workaround for my particular case with bones. Set the child to “Don’t inherit scale” (under Armature Bones) and copy over the the parent’s scaling with a pyconstraint or something.

No idea about how to do it with regular objects though. Any hints?