Add type hints

pull/1/head
miyakogi 2017-06-03 18:53:03 +09:00
parent a2cdad2a22
commit a69dc3d357
4 changed files with 25 additions and 10 deletions

3
mypy.ini Normal file
View File

@ -0,0 +1,3 @@
[mypy]
follow_imports=skip
ignore_missing_imports=True

View File

@ -1,6 +1,7 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
from os import path
try:
@ -12,6 +13,8 @@ readme_file = path.join(path.dirname(path.abspath(__file__)), 'README.rst')
with open(readme_file) as readme_file:
readme = readme_file.read()
install_requires = ['typing'] if sys.version_info < (3, 6) else []
setup(
name='syncer',
version='1.2.1',
@ -37,4 +40,5 @@ setup(
'Programming Language :: Python :: 3.6',
],
test_suite='test_syncer',
install_requires=install_requires,
)

View File

@ -2,16 +2,17 @@
# -*- coding: utf-8 -*-
import sys
import functools
from functools import singledispatch, wraps
import asyncio
import inspect
import types
from typing import Any, Callable, Generator
PY35 = sys.version_info >= (3, 5)
def _is_awaitable(co):
def _is_awaitable(co: Generator[Any, None, Any]) -> bool:
if PY35:
return inspect.isawaitable(co)
else:
@ -19,14 +20,14 @@ def _is_awaitable(co):
isinstance(co, asyncio.Future))
@functools.singledispatch
def sync(co):
@singledispatch
def sync(co: Any):
raise TypeError('Called with unsupported argument: {}'.format(co))
@sync.register(asyncio.Future)
@sync.register(types.GeneratorType)
def sync_co(co):
def sync_co(co: Generator[Any, None, Any]) -> Any:
if not _is_awaitable(co):
raise TypeError('Called with unsupported argument: {}'.format(co))
return asyncio.get_event_loop().run_until_complete(co)
@ -34,11 +35,11 @@ def sync_co(co):
@sync.register(types.FunctionType)
@sync.register(types.MethodType)
def sync_fu(f):
def sync_fu(f: Callable[..., Any]) -> Callable[..., Any]:
if not asyncio.iscoroutinefunction(f):
raise TypeError('Called with unsupported argument: {}'.format(f))
@functools.wraps(f)
@wraps(f)
def run(*args, **kwargs):
return asyncio.get_event_loop().run_until_complete(f(*args, **kwargs))
return run

13
tox.ini
View File

@ -1,5 +1,5 @@
[tox]
envlist = {py34,py35,py36}-{unittest,green,pytest,nose},flake8
envlist = {py34,py35,py36}-{unittest,green,pytest,nose},flake8,mypy
[testenv]
changedir = {toxworkdir}
@ -8,7 +8,7 @@ deps=
pytest: pytest
nose: nose
green: green
install = pip install -q {toxinidir}
install = pip install {toxinidir}
commands =
py34-unittest: python -m unittest discover -p test*py34.py {toxinidir}/tests
py34-green: green -p test*py34.py {toxinidir}/tests
@ -20,10 +20,17 @@ commands =
{py35,py36}-nose: nosetests {toxinidir}/tests
[testenv:flake8]
whitelist_externals = rm
changedir ={toxinidir}
basepython=python3.6
deps=flake8
commands=
flake8 syncer
[testenv:mypy]
whitelist_externals = rm
changedir ={toxinidir}
basepython=python3.6
deps=mypy
commands=
mypy syncer.py
rm -rf build dist **/__pycache__ **/*.pyc syncer.egg-info