Best practice to pydantic @validate_arguments for non built-in types like pandas DataFrame

Viewed 3452

I am currently trying to validate the input arguments of a function with pydantic. This works fine for the built-in datatypes, but not for types like pandas.DataFrame or numpy.array. Example:

@validate_arguments
def some_function(params: pd.DataFrame,
                  var_name: str
                  ) -> dict:
    # do something
    return my_dict

When calling some_function the following error is shown:

RuntimeError: no validator found for <class 'pandas.core.frame.DataFrame'>, see `arbitrary_types_allowed` in Config`

My first idea was to define my own DataFrame type with something like this:

class MyDF(pd.DataFrame):
    @classmethod
    def __get_validators__(cls):
        yield cls.validate

    @classmethod
    def validate(cls, val):
        return pd.DataFrame(val)

I am not sure, if this is the best practice. Does anyone have better ideas?

1 Answers

You can define a custom config to allow arbitrary types, so that pydantic checks the parameter is an instance of that type.

For example:

@validate_arguments(config=dict(arbitrary_types_allowed=True))
def some_function(params: pd.DataFrame,
                  var_name: str
                  ) -> dict:
    # do something
    return my_dict

Documentation for model config option is available here

Related