A previous answer is a good hint but felt a bit incomplete, so here is a more exhaustive one based on my current understanding of Blender and OP's question.
The pitfall
The UI might be misleading, but a Collection does not hold rendering properties such as indirect_only. Such properties, that might change from layer to layer, are actually stored through the ViewLayer and its LayerCollections. The LayerCollection encapsulates properties such as indirect_only, and a pointer to the Collection they apply to.
Proposed solution
So, now we can write a snippet of code that will programmatically change the value of indirect_only from a given Collection:
def iterate_layer_collections(view_layer):
"""Depth-first layer collections traversal"""
stack = [view_layer.layer_collection]
while stack:
layer_collection = stack.pop()
stack.extend(layer_collection.children)
yield layer_collection
def set_render_attr(view_layer, collection, attribute, value):
for layer_collection in iterate_layer_collections(view_layer):
if layer_collection.collection != collection:
continue # Not the layer collection we are looking for
setattr(layer_collection, attribute, value)
set_render_attr(
bpy.context.view_layer,
bpy.context.collection,
"indirect_only",
True,
)
Note on complexity
Note that in the code above, set_render_attr is looping over all the LayerCollections, so if you want to set this value for all the Collections in the scene, you will end up with $O(n)$ complexity, with $n$ the Collection count of your scene.
Might be okay for a typical use case, but something to keep an eye on.