From 945c2986af4e27f2194df8fe9d992b6dd62a3854 Mon Sep 17 00:00:00 2001 From: Maxim Andryunin Date: Thu, 24 Oct 2024 14:40:11 +0700 Subject: [PATCH 1/3] raise exception instead of returning it --- object_tracker/changelog.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/object_tracker/changelog.py b/object_tracker/changelog.py index d8eca28..820d5f1 100644 --- a/object_tracker/changelog.py +++ b/object_tracker/changelog.py @@ -177,7 +177,7 @@ def exclude(self, *attrs, changes_only=False) -> 'ChangeLog': eg: obj.exclude('name').all() """ if not attrs: - return InvalidChangeLogOperationException("exclude method needs atleast one attribute") + raise InvalidChangeLogOperationException("exclude method needs atleast one attribute") return self.apply_filters(attrs, True, changes_only) def first(self) -> Optional[Entry]: From 5a3438c232910ad62f5d9b7e3b920badc1b80eb8 Mon Sep 17 00:00:00 2001 From: Maxim Andryunin Date: Thu, 24 Oct 2024 14:42:21 +0700 Subject: [PATCH 2/3] fix typing to pass default mypy checks --- object_tracker/__init__.py | 13 +++++++------ object_tracker/changelog.py | 4 ++-- object_tracker/tracker.py | 12 ++++++------ 3 files changed, 15 insertions(+), 14 deletions(-) diff --git a/object_tracker/__init__.py b/object_tracker/__init__.py index 58d1da9..5e1a526 100644 --- a/object_tracker/__init__.py +++ b/object_tracker/__init__.py @@ -58,7 +58,7 @@ class MyClass: import inspect import logging -from typing import Dict, List +from typing import Dict, List, Optional from .exceptions import InitialStateMissingException, InvalidChangeLogOperationException from .changelog import Entry, ChangeLog @@ -107,11 +107,12 @@ def __init__(self, *args, **kwargs) -> None: super().__init__(*args, **kwargs) logger.debug(f"Tracker initialized for {self}") - def __track_changes(self, attr, value, tracker=None) -> None: + def __track_changes(self, attr, value) -> None: if attr == self.tracker_attr: return - tracker: Tracker = getattr(self, self.tracker_attr, None) + tracker: Optional[Tracker] = getattr(self, self.tracker_attr, None) + if tracker is None: return None @@ -132,13 +133,13 @@ def __setattr__(self, attr, value) -> None: def __setitem__(self, attr, value) -> None: self.__track_changes(attr, value) - super().__setitem__(attr, value) + super().__setitem__(attr, value) # type: ignore def track( *attributes: List[str], - observers: List[ObserverType] = None, - attribute_observer_map: Dict[str, List[ObserverType]] = None, + observers: Optional[List[ObserverType]] = None, + attribute_observer_map: Optional[Dict[str, List[ObserverType]]] = None, auto_notify: bool = True, stack_trace: bool = True, tracker_attribute: str = 'tracker', diff --git a/object_tracker/changelog.py b/object_tracker/changelog.py index 820d5f1..6058497 100644 --- a/object_tracker/changelog.py +++ b/object_tracker/changelog.py @@ -49,7 +49,7 @@ class Entry(namedtuple('Entry', ['attr', 'old', 'new', 'timestamp', 'stack'])): """ The Entry class is a named tuple that represents a single log entry in the ChangeLog. """ - def __new__(cls, attr, old, new, stack: List[Frame] = None): + def __new__(cls, attr, old, new, stack: Optional[List[Frame]] = None): if stack: stack = [Frame(frame) for frame in stack] @@ -132,7 +132,7 @@ def __len__(self) -> int: def __iter__(self): return iter(self.log) - def to_dict(self) -> dict: + def to_dict(self) -> List[dict]: return [entry.to_dict() for entry in self.log] def reset_buffer(self): diff --git a/object_tracker/tracker.py b/object_tracker/tracker.py index 0c8db63..06a6955 100644 --- a/object_tracker/tracker.py +++ b/object_tracker/tracker.py @@ -7,7 +7,7 @@ import logging from copy import deepcopy -from typing import Dict, List +from typing import Dict, List, Any, Optional from object_tracker.exceptions import InitialStateMissingException from object_tracker.changelog import ChangeLog @@ -42,10 +42,10 @@ class MyClass: def __init__( self, - initial_state: any = None, - attributes: List[str] = None, - observers: List[ObserverType] = None, - attribute_observer_map: Dict[str, List[ObserverType]] = None, + initial_state: Any = None, + attributes: Optional[List[str]] = None, + observers: Optional[List[ObserverType]] = None, + attribute_observer_map: Optional[Dict[str, List[ObserverType]]] = None, auto_notify: bool = True, stack_trace: bool = True, changes_only: bool = False, @@ -145,7 +145,7 @@ def set_initial_state(self, obj) -> None: self.initial_state = deepcopy(obj) logger.debug(f"Initial state set for {self}") - def to_dict(self) -> dict: + def to_dict(self) -> List[dict]: """ Returns the log as a dictionary """ From 621272bc8226d84f31db14f92c996bfee5b6f8fe Mon Sep 17 00:00:00 2001 From: Maxim Andryunin Date: Thu, 24 Oct 2024 14:45:16 +0700 Subject: [PATCH 3/3] add py.typed mark library as typed (see PEP 561) --- object_tracker/py.typed | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 object_tracker/py.typed diff --git a/object_tracker/py.typed b/object_tracker/py.typed new file mode 100644 index 0000000..e69de29