Skip to content

Commit

Permalink
make BaseObserver an ABC and ruff fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
quaquel committed Sep 22, 2024
1 parent 0e67153 commit 2a3f108
Showing 1 changed file with 10 additions and 12 deletions.
22 changes: 10 additions & 12 deletions mesa/experimental/signals/signal.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import functools
import itertools
import weakref
from abc import ABC, abstractmethod
from collections import defaultdict, namedtuple
from collections.abc import Callable
from typing import Any
Expand Down Expand Up @@ -54,10 +55,10 @@ def __get__(self, instance, owner):
self._is_dirty = False
return self.value

class BaseObservable(ABC):
"""Abstract base class for all Observables."""

class BaseObservable:
"""Base class for all Observables."""

@abstractmethod
def __init__(self):
"""Initialize a BaseObservable."""
self.public_name: str
Expand All @@ -71,15 +72,16 @@ def __init__(self):
self.signal_types: set
self.fallback_value = None # fixme, should this be user specifiable?

def __get__(self, instance, owner):
def __get__(self, instance, owner): # noqa: D103
return getattr(instance, self.private_name)

def __set_name__(self, owner: "HasObservables", name: str):
def __set_name__(self, owner: "HasObservables", name: str): # noqa: D103
self.public_name = name
self.private_name = f"_{name}"
owner.register_observable(self)

def __set__(self, instance: "HasObservables", value):
@abstractmethod
def __set__(self, instance: "HasObservables", value): # noqa: D103
# this only emits an on change signal, subclasses need to specify
# this in more detail
instance.notify(
Expand All @@ -91,11 +93,7 @@ def __set__(self, instance: "HasObservables", value):


class Observable(BaseObservable):
"""Base Observable class."""

# fixme, we might want to have a base observable
# and move some of this into this class and use super to allways use it
# for example the on_change notify can go there
"""Observable class."""

def __init__(self):
"""Initialize an Observable."""
Expand Down Expand Up @@ -157,7 +155,7 @@ def __new__(cls, *args, **kwargs): # noqa D102
return obj

@classmethod
def register_observable(cls, observable: Observable):
def register_observable(cls, observable: BaseObservable):
"""Register an Observable.
Args:
Expand Down

0 comments on commit 2a3f108

Please sign in to comment.