Hello everyone, We are developing our custom decor...
# ask-metaflow
s
Hello everyone, We are developing our custom decorators, and I would like to test the features we created. My first intuition was creating a Meflow flow test using the decorator. Still, I can't use the decorator with the same syntax I use in the flow inside of the decorator's repository itself. What would be the correct way to test the decorators' features? Thank you !
1
v
when you say you can't use the same syntax, do you mean
run --with my_decorator
or something else?
s
Hum, I mean the use of @customdecorator in a Flow. But I might be wrong on how to properly test my functions on this matter
To be more specific, I wrote this code :
Copy code
"""This module contains the custom step decorators for Metaflow."""

from metaflow.decorators import StepDecorator


class JobStepDecorator(StepDecorator):
    """Step decorator that does something before and after the step, and also if the step fails."""

    name = "jobstepdecorator"

    def task_pre_step(
        self,
        step_name,
        task_datastore,
        metadata,
        run_id,
        task_id,
        flow,
        graph,
        retry_count,
        max_user_code_retries,
        ubf_context,
        inputs,
    ):
        """Do something before the step."""
        # Do something before the step

    def task_post_step(self, step_name, flow, graph, retry_count, max_user_code_retries):
        """Do something after the step."""
        # Do something after the step

    def task_exception(self, exception, step_name, flow, graph, retry_count, max_user_code_retries):
        """Do something if the step fails."""
I would like to test the different functions
The complexity resides in the fact that you might have to instantiate a Flow object and maybe steps to test the features
d
I am assuming you are buildng your decorators using: https://github.com/Netflix/metaflow-nflx-extensions (to get them integrated with metaflow). If so, you should be able to use just a flow. You can use something like Runner to actually execute your flow and verify that your decorator is behaving as it needs to. Note that in a lot of cases, you don’t need a full “metaflow decorator” and regular/vanilla python decorators can do the trick which may simplify things. But I don’t know what exactly you want to do so can’t comment much. Just bringing it up.
s
Yes I use the extensions. But my problem is to be able to use the @decorator synthax in a flow inside the same repository as the decorator code itself. I usually install the extension package and then I can use the syntax :
Copy code
from metaflow import customstepdecorator
But inside the repo itself, I'm not sure if it's possible. I tried workarounds by importing the class itself, in the context of a flow, but it didn't work. Is it possible to do that ? Again, maybe I don't really understand the underlying logic, and maybe using Python decorators could work. I use the decorator to log what has happened in the step and handle errors, connected to an external logging API.
d
You should be able to inside the repo by pip installing the repo itself I think (editable mode works fine too). For your decorator, if you just do something before and after the step, a simple decorator may also be sufficient. Depends what you want to log :)