Skip to content

Commit

Permalink
Fixes #21
Browse files Browse the repository at this point in the history
  • Loading branch information
cevans87 committed Jun 24, 2019
1 parent 5667eef commit aa3b50f
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 9 deletions.
8 changes: 4 additions & 4 deletions atools/memoize_decorator.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import inspect
from time import time
from threading import Lock as SyncLock
from typing import Any, Optional, Tuple, Union
from typing import Any, Optional, Union


class _MemoZeroValue:
Expand Down Expand Up @@ -74,15 +74,15 @@ def reset(self) -> None:
self._memos = OrderedDict()
self._expire_order = deque() if self._expire_order is not None else None

def make_key(self, *args, **kwargs) -> Tuple:
def make_key(self, *args, **kwargs) -> int:
"""Returns all params (args, kwargs, and missing default kwargs) for function as kwargs."""
args_as_kwargs = {}
for k, v in zip(self._default_kwargs, args):
args_as_kwargs[k] = v

return tuple(ChainMap(args_as_kwargs, kwargs, self._default_kwargs).values())
return hash(tuple(ChainMap(args_as_kwargs, kwargs, self._default_kwargs).values()))

def get_memo(self, key) -> _Memo:
def get_memo(self, key: int) -> _Memo:
try:
memo = self._memos[key] = self._memos.pop(key)
if self._duration is not None and memo.expire_time < time():
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

setup(
name='atools',
version='0.6.0',
version='0.6.1',
packages=['', 'atools'],
python_requires='>=3.6',
url='https://github.com/cevans87/atools',
Expand Down
19 changes: 15 additions & 4 deletions test/test_memoize_decorator.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from datetime import timedelta
import unittest
from unittest.mock import call, MagicMock, patch
from weakref import ref


@async_test_case
Expand Down Expand Up @@ -465,11 +466,21 @@ async def foo() -> int:

self.assertEqual(foo_a, foo_b)

#async def test_memoize_does_not_stop_object_cleanup(self) -> None:
# # TODO make an object, memoize a funciton with that object, and then convert local
# # variable to a weakref. Assert that the object is cleaned up.
async def test_memoize_does_not_stop_object_cleanup(self) -> None:
class Foo:
pass

@memoize
def foo(_: Foo) -> None:
...

f = Foo()
foo(f)

# raise Exception()
r = ref(f)
assert r() is not None
del f
assert r() is None


if __name__ == '__main__':
Expand Down

0 comments on commit aa3b50f

Please sign in to comment.