use bpy.context.view_layer.update()
and ensure that the object is visible otherwise the object might not be updated. Credits to the original answer and comment of mgibsonbr and Ray.
A similar problem arises for Justas' and Don Hatch's answer since the matrix_inverse_parent
might not have been computed yet
(I wanted to add this as a comment since i don't think it's relevant as a separate answer, but I don't have enough reputation)