Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Typing fixes to make mypy happy #2

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions object_tracker/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand All @@ -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',
Expand Down
6 changes: 3 additions & 3 deletions object_tracker/changelog.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]

Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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]:
Expand Down
Empty file added object_tracker/py.typed
Empty file.
12 changes: 6 additions & 6 deletions object_tracker/tracker.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
"""
Expand Down