How to get the grid size in a Python script?
Overlays are set per view.
You need to find the (or a) 3d view area.
import bpy

def get_areas_by_type(context, type):
    return [a for a in context.screen.areas if a.type == type]

# Get view3d areas
view3d_areas = get_areas_by_type(bpy.context, 'VIEW_3D')

# Assuming there is one (if not test it in your code)
view3d_area = view3d_areas[0]

# The view space
view_space = view3d_area.spaces[0]

# It has overlay
view_space.overlay.grid_scale = 4.0

Or if you know that your context is the 3d view, you can use:
context.space_data.overlay.grid_scale