Skip to content

Commit

Permalink
Supports Py36
Browse files Browse the repository at this point in the history
  • Loading branch information
cevans87 committed Jun 5, 2019
1 parent 6f7761a commit 91588e7
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 22 deletions.
11 changes: 7 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
language: python
python:
- '3.7'
- '3.6'
- '3.7'
dist: xenial
env:
PYTHONPATH=.
before_install:
- pip install pytest pytest-cov
- pip install coveralls
- pip install pytest pytest-cov
- pip install coveralls
install:
- python setup.py -q install
script: pytest --verbose --cov=atools
after_success:
- coveralls
- coveralls
deploy:
provider: pypi
user: cevans87
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[![Build Status](https://travis-ci.org/cevans87/atools.svg?branch=master&kill_cache=1)](https://travis-ci.org/cevans87/atools)
[![Coverage Status](https://coveralls.io/repos/github/cevans87/atools/badge.svg?branch=master&kill_cache=1)](https://coveralls.io/github/cevans87/atools?branch=master)
# atools
Python 3.7+ async-enabled decorators and tools including
Python 3.6+ async-enabled decorators and tools including

- __memoize__ - a function decorator for sync and async functions that memoizes results.
- __async_test_case__ - a test class/function decorator that enables test functions to be async.
Expand Down
3 changes: 1 addition & 2 deletions atools/async_test_case_decorator.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from __future__ import annotations
import asyncio
from atools.decorator_mixin import DecoratorMixin, Fn, Decoratee, Decorator
import inspect
Expand Down Expand Up @@ -44,7 +43,7 @@ def __init__(self, fn: Fn) -> None:
def __call__(self, *args, **kwargs) -> Any:
sync_result = self._fn(*args, **kwargs)
if inspect.iscoroutinefunction(self._fn):
return asyncio.run(sync_result)
return asyncio.get_event_loop().run_until_complete(sync_result)
elif not inspect.iscoroutine(sync_result):
return sync_result
else:
Expand Down
3 changes: 1 addition & 2 deletions atools/decorator_mixin.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from __future__ import annotations
from functools import wraps
import inspect
from typing import Any, Awaitable, Callable, Dict, Optional, Tuple, Type, Union
Expand All @@ -13,7 +12,7 @@
class _DecoratorMeta(type):
def __new__(
mcs, name: str, bases: Tuple[Type, ...], namespace: Dict[str, Any]
) -> _DecoratorMeta:
) -> '_DecoratorMeta':
# DecoratorMixin has a __doc__, but we want the __doc__ from the actual decorator class.
if len(bases) > 1 and bases[0] is DecoratorMixin and '__doc__' not in namespace:
namespace['__doc__'] = bases[1].__doc__
Expand Down
2 changes: 2 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,7 @@ long_description_content_type = text/markdown
[options]
packages =
atools
install_requires =
dataclasses>="0.6";python_version=="3.6"
py_modules =
__init__
6 changes: 3 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

setup(
name='atools',
version='0.5.2',
version='0.6.0',
packages=['', 'atools'],
python_requires='>=3.7',
python_requires='>=3.6',
url='https://github.com/cevans87/atools',
license='mit',
author='cevans',
author_email='[email protected]',
description='Python 3.7+ async decorators and testing tools'
description='Python 3.6+ async decorators and testing tools'
)
23 changes: 15 additions & 8 deletions test/test_memoize_decorator.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from asyncio import coroutine, create_task, Event, gather, get_event_loop
from asyncio import (
coroutine, ensure_future, Event, gather, get_event_loop, new_event_loop, set_event_loop
)
from atools import async_test_case, memoize
from datetime import timedelta
import unittest
Expand All @@ -7,7 +9,7 @@

@async_test_case
class TestMemoize(unittest.TestCase):

def test_zero_args(self) -> None:
body = MagicMock()

Expand Down Expand Up @@ -348,12 +350,17 @@ def foo() -> None:

def test_async_no_event_loop_does_not_raise(self) -> None:
# Show that we decorate without having an active event loop
with self.assertRaises(RuntimeError):
self.assertIsNone(get_event_loop())
# noinspection PyTypeChecker
set_event_loop(None)
try:
with self.assertRaises(RuntimeError):
self.assertIsNone(get_event_loop())

@memoize
async def foo() -> None:
...
@memoize
async def foo() -> None:
...
finally:
set_event_loop(new_event_loop())

def test_memoizes_class(self) -> None:
body = MagicMock()
Expand Down Expand Up @@ -449,7 +456,7 @@ async def foo() -> int:
await foo_finish_event.wait()
return 0

task_a, task_b = create_task(foo()), create_task(foo())
task_a, task_b = ensure_future(foo()), ensure_future(foo())

await foo_start_event.wait()
foo_finish_event.set()
Expand Down
3 changes: 1 addition & 2 deletions test/test_rate_decorator.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from __future__ import annotations
import asyncio
from asyncio import gather
from atools import async_test_case, rate
Expand Down Expand Up @@ -111,7 +110,7 @@ async def foo() -> None:

await foo()

task = asyncio.create_task(foo())
task = asyncio.ensure_future(foo())

while foo.rate.async_waiters != 1:
await asyncio.sleep(0)
Expand Down

0 comments on commit 91588e7

Please sign in to comment.