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

[#35] Since 3.8, CancelledError is a subclass of BaseException rather… #37

Merged
Merged
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
6 changes: 6 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
3.1.1
-----

* Since 3.8, CancelledError is a subclass of BaseException rather than Exception, so we need to catch it explicitly.
* Enabled `mypy` for `wrapper` function.

3.1.0
-----

Expand Down
6 changes: 3 additions & 3 deletions memoize/statuses.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import datetime
import logging
from abc import ABCMeta, abstractmethod
from asyncio import Future
from asyncio import Future, CancelledError
from typing import Dict, Awaitable, Union

from memoize.entry import CacheKey, CacheEntry
Expand All @@ -30,7 +30,7 @@ def mark_updated(self, key: CacheKey, entry: CacheEntry) -> None:
raise NotImplementedError()

@abstractmethod
def mark_update_aborted(self, key: CacheKey, exception: Exception) -> None:
def mark_update_aborted(self, key: CacheKey, exception: Union[Exception, CancelledError]) -> None:
"""Informs that update failed to complete.
Calls to 'is_being_updated' will return False until 'mark_being_updated' will be called.
Accepts exception to propagate it across all clients awaiting an update."""
Expand Down Expand Up @@ -79,7 +79,7 @@ def mark_updated(self, key: CacheKey, entry: CacheEntry) -> None:
update = self._updates_in_progress.pop(key)
update.set_result(entry)

def mark_update_aborted(self, key: CacheKey, exception: Exception) -> None:
def mark_update_aborted(self, key: CacheKey, exception: Union[Exception, CancelledError]) -> None:
if key not in self._updates_in_progress:
raise ValueError('Key {} is not being updated'.format(key))
update = self._updates_in_progress.pop(key)
Expand Down
10 changes: 5 additions & 5 deletions memoize/wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import datetime
import functools
import logging
from asyncio import Future
from asyncio import Future, CancelledError
from typing import Optional, Callable

from memoize.configuration import CacheConfiguration, NotConfiguredCacheCalledException, \
Expand All @@ -17,8 +17,8 @@
from memoize.statuses import UpdateStatuses, InMemoryLocks


def memoize(method: Optional[Callable] = None, configuration: CacheConfiguration = None,
invalidation: InvalidationSupport = None, update_statuses: UpdateStatuses = None):
def memoize(method: Optional[Callable] = None, configuration: Optional[CacheConfiguration] = None,
invalidation: Optional[InvalidationSupport] = None, update_statuses: Optional[UpdateStatuses] = None):
"""Wraps function with memoization.

If entry reaches time it should be updated, refresh is performed in background,
Expand Down Expand Up @@ -116,14 +116,14 @@ async def refresh(actual_entry: Optional[CacheEntry], key: CacheKey,
logger.debug('Timeout for %s: %s', key, e)
update_statuses.mark_update_aborted(key, e)
raise CachedMethodFailedException('Refresh timed out') from e
except Exception as e:
except (Exception, CancelledError) as e:
logger.debug('Error while refreshing cache for %s: %s', key, e)
update_statuses.mark_update_aborted(key, e)
raise CachedMethodFailedException('Refresh failed to complete') from e

@functools.wraps(method)
async def wrapper(*args, **kwargs):
if not configuration.configured():
if configuration is None or not configuration.configured():
raise NotConfiguredCacheCalledException()

configuration_snapshot = MutableCacheConfiguration.initialized_with(configuration)
Expand Down
1 change: 1 addition & 0 deletions mypy.ini
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
[mypy]
no_implicit_optional=False
check_untyped_defs=True
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def prepare_description():

setup(
name='py-memoize',
version='3.1.0',
version='3.1.1',
author='Michal Zmuda',
author_email='zmu.michal@gmail.com',
url='https://github.com/DreamLab/memoize',
Expand Down
32 changes: 32 additions & 0 deletions tests/end2end/test_wrapper.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import asyncio
import time
from asyncio import CancelledError
from datetime import timedelta
from unittest.mock import Mock

Expand Down Expand Up @@ -174,6 +175,37 @@ async def get_value(arg, kwarg=None):
assert context.value.__class__ == CachedMethodFailedException
assert str(context.value.__cause__) == str(ValueError('stub0'))

async def test_should_return_cancelled_exception_for_all_concurrent_callers(self):
# given
value = 0

@memoize()
async def get_value(arg, kwarg=None):
new_task = asyncio.create_task(asyncio.sleep(1))
new_task.cancel() # this will raise CancelledError
await new_task

# when
res1 = get_value('test', kwarg='args1')
res2 = get_value('test', kwarg='args1')
res3 = get_value('test', kwarg='args1')

# then
with pytest.raises(Exception) as context:
await res1
assert context.value.__class__ == CachedMethodFailedException
assert str(context.value.__cause__) == str(CancelledError())

with pytest.raises(Exception) as context:
await res2
assert context.value.__class__ == CachedMethodFailedException
assert str(context.value.__cause__) == str(CancelledError())

with pytest.raises(Exception) as context:
await res3
assert context.value.__class__ == CachedMethodFailedException
assert str(context.value.__cause__) == str(CancelledError())

async def test_should_return_timeout_for_all_concurrent_callers(self):
# given
value = 0
Expand Down