Last active
July 24, 2020 13:04
-
-
Save MattOates/33fa709d8313e59493647e1b492f6950 to your computer and use it in GitHub Desktop.
Observer pattern in Python, but has some annoying problems with respect to mypy typing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from typing import Any, Callable, Dict, List, Optional, Set | |
from abc import ABC, abstractmethod | |
import logging | |
LOGGER = logging.getLogger(__name__) | |
class Observer(ABC): | |
@abstractmethod | |
def update(self, observed: "Observable", event: Any) -> None: | |
pass | |
class Logger(Observer): | |
def update(self, observed: "Observable", event: Any) -> None: | |
if isinstance(observed, "ImplementedThing") and isinstance(event, str): | |
LOGGER.warning(f"{observed.description} WARNING: {event}") | |
class Observable(ABC): | |
_default_observers: Set[Observer] = {Logger()} | |
_observers: Set[Observer] | |
def __init__( | |
self, | |
using: Optional[List[Observer]] = None | |
): | |
self._observers = set() | |
for observer in Observable._default_observers: | |
self.subscribe(observer) | |
if using: | |
for observer in using: | |
self.subscribe(observer) | |
def subscribe(self, observer: Observer) -> None: | |
self._observers.add(observer) | |
def unsubscribe(self, observer: Observer) -> None: | |
self._observers.remove(observer) | |
def publish(self, event: object) -> None: | |
for observer in self._observers: | |
observer.update(self, event) | |
class ImplementedThing(Observable): | |
description: str = "Hello World" | |
def __init__(self, description: str): | |
super().__init__() | |
self.description = description | |
def implement(self): | |
print(self.description) | |
self.publish(self.description) | |
thing = ImplementedThing("Goodbye") | |
thing.implement() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
With the above code we get the error:
However if you attempt to resolve this its impossible to avoid a
This violates the Liskov substitution principle
which isn't strictly true as the more general class is an abstract class and can never be instantiated. So there is no substitution here strictly.