Hi everyone - How do I have a Task as a class and...
# prefect-community
r
Hi everyone - How do I have a Task as a class and inherit from another Parent Class? For example: If I have
Copy code
# parent class
class ABC:
  def __init__(
    self,
    user,
    password
  )
    self.user = user
    self.password = password

  def query(self)
    pass
I want to do something like this
Copy code
# task
class Task_A(Task, ABC):
  def run()
    pass
Currently, I'm just doing this instead
Copy code
@task
def task_a(user, password, date_col, dataset_id):
    user: str = None,
    password: str = None,
    date_col: str = None,
    dataset_id: str = None
):

    conn = ABC(user=user, password=password)
    query = f"SELECT DISTINCT {date_col} FROM EDW_DNA_DB.WI.{dataset_id}"
    query_result = conn.query(query)
    return query_result[date_col].tolist()
j
Hi Riley, tasks are just standard classes themselves so they should follow normal Python subclass rules, including multiple inheritance
r
Thanks @Jeremiah, I tried doing this but wasn't successful - For example, here is a subclass
Copy code
class SfTask(Task, ConnectToSnowflake):
    def __init__(self,
        date_column: str = None,
        dataset_id: str = None,
        *args,
        **kwargs
    ):
        self.date_column = date_column
        self.dataset_id = dataset_id
        super(SfTask, self).__init__(*args, **kwargs)

    @defaults_from_attrs(
        "date_column",
        "dataset_id"
    )
    def run(self,
        bucket_name,
        date_column,
        dataset_id
        ):
        query = f"SELECT DISTINCT {date_column} FROM EDW_DNA_DB.WI.THINKNUM_{dataset_id}"
        query_result = self.query_to_df(query)
        return query_result[date_column].tolist()
But when I try to instantiate it, I get an error
TypeError: __init__() got an unexpected keyword argument 'bucket_name'
where 'bucket_name' is an init variable from the parent class
j
You’ll need to follow Python rules for subclassing with multiple inheritance - Python doesn’t know what arguments to pass in your
super
call. For example see https://stackoverflow.com/questions/34884567/python-multiple-inheritance-passing-arguments-to-constructors-using-super/34885285