<@ULVA73B9P> how can I have a subflow inherit the ...
# ask-marvin
a
@Marvin how can I have a subflow inherit the tags of the parent flow that called it?
m
To have a subflow inherit the tags of its parent flow in Prefect 3.x, you can use the
tags
context manager from the
prefect
module. Here's a general approach: 1. Define the Parent Flow with Tags: Use the
tags
context manager to set tags for the parent flow. 2. Call the Subflow within the Tagged Context: Ensure that when you call the subflow, it is within the context where the tags are defined. This way, the subflow will inherit the tags of the parent flow. Here's a simple example:
Copy code
python
from prefect import flow, tags

@flow
def parent_flow():
    with tags("shared-tag"):
        subflow()

@flow
def subflow():
    # This subflow will inherit the "shared-tag" from the parent flow
    pass

if __name__ == "__main__":
    parent_flow()
By using the
tags
context manager, the subflow will execute with the same tags as the parent flow when it is called within the context. You can explore more about tags in the Prefect documentation. For additional context, you can refer to this example on GitHub.