Changing area type creates weird bar a top of blender

I’m using this code to access the dopesheet editor before doing some stuff with actions in 4.0.

current_context = bpy.context.area.type
bpy.context.area.type = "DOPESHEET_EDITOR"
bpy.context.space_data.mode = "ACTION"

#Do some stuff

bpy.context.area.type = current_context

However after the script ends I’m left with the top of my blender window looking like this.

It’s like it’s adding the dope sheet editor to the top but it won’t let me close it as it’s really messed up. The code is from an addon for 2.7 and obviously it’s no longer working like it used to. Does any one have an idea on the correct way to do this?

It seems I don’t even need to change the area type in order to access the actions. Removing the above code fixes the issue.