Blender 2.8 : python code to switch shading mode (between wireframe and solid mode)
As to why the script generates an error:
When directly clicking on a button on the screen Blender understands the context of which workspace (tabs across the top of the screen) and area (window within the screen) you are accessing.
Your code fails to identify where exactly you want to apply the change.

import bpy

my_areas = bpy.context.workspace.screens[0].areas
my_shading = 'WIREFRAME'  # 'WIREFRAME' 'SOLID' 'MATERIAL' 'RENDERED'


for area in my_areas:
    for space in area.spaces:
        if space.type == 'VIEW_3D':
            space.shading.type = my_shading