mwchase / python-structured-data

MIT License
2 stars 0 forks source link

Nicer enum variant (possibly a new project?) #23

Closed mwchase closed 5 years ago

mwchase commented 5 years ago

Investigating the use of data in Dennis revealed several instances of the following basic pattern.

class Base:
    attr: int

    def protocol_method(self) -> ???:
        raise NotImplementedError

data Concrete from Base:
    def protocol_method(self) -> ???:
        ...

class EasyWay(Concrete, enum.Enum):
    ...
    # Inherits Concrete's implementation of protocol_method

class HardWay(Base, enum.Enum):
    ...
    def protocol_method(self) -> ???:
        return typing.cast(Base, self.value).protocol_method()
    # Inherits Base's non-implementation of protocol_method, and must therefore explicitly forward it to the method on self.value

It'd be nice to have some means of forwarding the value automatically.

I'm leaning towards a method decorator that detects when it's being accessed on Enum subclasses.

I don't know if this is within the scope of ADT or not. I may attempt to create it within its own library and see if it can stand on its own.

mwchase commented 5 years ago

I've got this in its own library and it seems to work fine.