How to get correct context in animation nodes script
Then you launch script from AN, bpy.context doesn't include active_object anymore. So, I suggest creating context object from scratch, just 2 elements are necessary:

context_py = {
    "constraint" : const,
    "active_object" : rig
    }


This code works for me (but constantly resetting Inverse makes weird result):

import bpy

if Inverse: 
    rig = bpy.data.objects['metarig2']
    pbones = rig.pose.bones

    for pbone in pbones:
          for const in pbone.constraints:

                if const.type == "CHILD_OF":
                    context_py = {
                        "constraint" : const,
                        "active_object" : rig
                        }

                    bpy.data.objects['metarig2'].data.bones.active = pbone.bone

                    bpy.ops.constraint.childof_set_inverse(context_py, constraint="Child Of", owner='BONE')