equinox icon indicating copy to clipboard operation
equinox copied to clipboard

Abstract/final pattern does not support abstract __init__

Open mjo22 opened this issue 5 months ago • 3 comments

The following is not allowed in the abstract/final pattern

import abc
import equinox as eqx

some_function = lambda arg1: ...

class AbstractModule(eqx.Module, strict=True):
    
    @abc.abstractmethod
    def __init__(arg1, arg2):
        raise NotImplementedError

    @classmethod
    def some_custom_constructor(cls, arg3):
        # Compute arg1 and arg2 with arg3
        return cls(arg1, arg2)

class Module1(AbstractModule, strict=True):

    arg1: Array
    arg2: Array

    @override
    def __init__(arg1, arg2):
        self.arg1 = arg1
        self.arg2 = arg2


class Module1(AbstractModule, strict=True):

    not_arg1: Array
    arg2: Array

    @override
    def __init__(arg1, arg2):
        self.not_arg1 = some_function(arg1)
        self.arg2 = arg2

Here, the custom constructor requires that the __init__ be of a certain form. However, one may not necessarily want set the same exact fields even if the arguments to the __init__ are the same. Is there a reason this does not fall into the abstract/final design pattern? Or should this be supported with strict = True?

mjo22 avatar Jan 29 '24 21:01 mjo22