Type hint in parent class that take child class as argument

84 Views Asked by At

I have a parent class for pandas dataframe models. This class contains usefull functions for validations etc. Child class are used to define models.

One of the method in the parent class takes an instance of the child class as argument. How do I specify this in the type hint ?

Pseudo code here:

import pandas as pd

class ParentPandasModel(pd.DataFrame):
    """Parent class for all the models that are based on pandas"""
    @classmethod
    def validate_dataframe(cls, arg1: Instance_of_child) -> bool:
        """ Checks if the instance has all needed columns etc."""
        ...
        return is_data_frame_valid

class ChildModel(ParentPandasModel):
    """ dataframe model """
    column1 = "price"
    column2 = "number"
    column3 = "date"

The point is to validate a dataframe df by calling:

ChildModel.validate_dataframe(df)

This works fine. However I do not know what to put in the type hint arg1 so that my linter does not go. crazy. What should I put there ?

I must had that placing the method in the ChildModel is not an option has I have too many models and wish to define that method only once in the parent class so that they all inherit the method. I also do not want to specify the exact childmodel has all child models will need this method (hitting with "|" would be too much overhead.

Tryied various import type/typing/Any without avail.

0

There are 0 best solutions below