bulwark icon indicating copy to clipboard operation
bulwark copied to clipboard

Pass function kwargs into the check function?

Open josh-gree opened this issue 5 years ago • 2 comments

Hi great project thanks very much!

I have the following use case and I am wondering if it can perhaps already be achieved? I have taken a look at the source and not sure that it can be! Maybe what I want to do is crazy!?

I have some function;

def foo(date: datetime.date) -> DataFrame:
    df = call_to_an_external_api(date)
    return df

The particular api is supposed to respond with data for only a single day specified by date - however sometimes data from days either side leak in - I would like to check that a column of df is unique and that all its values are date. However I don't know date until the function is called.

Could it be possible for a check decorator to pass in the decorated functions args in some way so this could be possible?

josh-gree avatar May 29 '20 18:05 josh-gree

Unfortunately there's not currently a way to do this flexibly with decorators. Right now the easiest way would be to use the check function version:

def foo(date: datetime.date) -> pd.DataFrame:
    df = call_to_an_external_api(date)
    ck.has_vals_within_set(df, items={"date": [date]})
    return df

or to wrap the decorated version:

def foo(date: datetime.date) -> pd.DataFrame
    @dc.HasValsWithinSet(items={"date": [date]})
    def foo(date):
        df = call_to_an_external_api(date)
        return df
    return foo(date)

This question has come up before, and I'm open to a solution if you have one. It's challenging because check argument names and the argument names in the user's function might collide, but be used for different purposes, so kwargs can't just be synced by name

ZaxR avatar May 30 '20 15:05 ZaxR

@ZaxR Thanks for the response! I did initially think it could just be as simple as passing the kwargs/args into the check function - but you are correct not clear how to deal with clashes...

I think your second approach may work for me!

josh-gree avatar Jun 04 '20 07:06 josh-gree