dimanche 12 mars 2023

Python : cannot call static method on run-time retrieved type

I have a base class:

class MyBaseClass:
    @abstractmethod
    def from_list(list_float: List[float]) -> Self:
        pass

    @staticmethod
    @abstractmethod
    def fn() -> int: 
        pass

All children of that class implement from_list and static fn, since they are abstract.

Now, I have another class 'MyOptions' :

class MyOptions:
    @abstractclass
    def related_to_type(): type(MyBaseClass)

All MyOptions children implement related_to_type, which returns a type that is a child of MyBaseClass. What I want is:

Provided a MyOptions child (which is unknown until runtime), call fn on its related type, returned by related_to_type(). In other term, I want:

options: MyOptions = MyOptionChild()
type_opt = options.related_to_type() # this should return a MyBaseClass child type 
result = type_opt.fn() # error here 

The error is that type_opt contains an variable of type <class 'abc.ABCMeta'>, on which I cannot call my static function fn. PS : I cannot make MyOptions generic with a type argument because the related type is known at run-time only

What can I do to call a static function on a variable containing a "reflected class" in Python ? Typically, in java, you would do:

Method method = myBaseClassChild.getMethod("fn");
int result = method.invoke(null);




Aucun commentaire:

Enregistrer un commentaire