Skip to content

Add typings #366

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

Merged
merged 3 commits into from
Feb 9, 2021
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: 4 additions & 2 deletions .flake8
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
[flake8]
filename = *.py,*.pyx,*.pxd,*.pxi
filename = *.py,*.pyx,*.pxd,*.pxi,*.pyi
ignore = E402,E731,D100,D101,D102,D103,D104,D105,W503,W504,E252
exclude = .git,__pycache__,build,dist,.eggs,postgres,vendor

per-file-ignores = *.pyx,*.pxd,*.pxi: E211, E222, E225, E226, E227, E999
per-file-ignores =
*.pyx,*.pxd,*.pxi: E211, E222, E225, E226, E227, E999
*.pyi: F401, F403, F405, F811, E127, E128, E203, E266, E301, E302, E305, E501, E701, E704, E741, B303, W503, W504
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,4 @@ uvloop/loop.*.pyd
/.mypy_cache/
/.vscode
/.eggs
/.venv*
4 changes: 2 additions & 2 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
recursive-include docs *.py *.rst
recursive-include examples *.py
recursive-include tests *.py *.pem
recursive-include uvloop *.pyx *.pxd *.pxi *.py *.c *.h
recursive-include uvloop *.pyx *.pxd *.pxi *.py *.c *.h *.pyi py.typed
recursive-include vendor/libuv *
recursive-exclude vendor/libuv/.git *
recursive-exclude vendor/libuv/docs *
recursive-exclude vendor/libuv/img *
include LICENSE-MIT LICENSE-APACHE README.rst Makefile performance.png .flake8
include LICENSE-MIT LICENSE-APACHE README.rst Makefile performance.png .flake8 mypy.ini
6 changes: 6 additions & 0 deletions mypy.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[mypy]
incremental = True
strict = True

[mypy-uvloop._testbase]
ignore_errors = True
7 changes: 4 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,11 @@
# their combination breaks too often
# (example breakage: https://gitlab.com/pycqa/flake8/issues/427)
'aiohttp',
'flake8~=3.7.9',
'flake8~=3.8.4',
'psutil',
'pycodestyle~=2.5.0',
'pyOpenSSL~=19.0.0'
'pycodestyle~=2.6.0',
'pyOpenSSL~=19.0.0',
'mypy>=0.800',
]

# Dependencies required to build documentation.
Expand Down
33 changes: 32 additions & 1 deletion tests/test_sourcecode.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def find_uvloop_root():
return os.path.dirname(os.path.dirname(os.path.abspath(__file__)))


class TestFlake8(unittest.TestCase):
class TestSourceCode(unittest.TestCase):

def test_flake8(self):
edgepath = find_uvloop_root()
Expand All @@ -33,3 +33,34 @@ def test_flake8(self):
output = ex.output.decode()
raise AssertionError(
'flake8 validation failed:\n{}'.format(output)) from None

def test_mypy(self):
edgepath = find_uvloop_root()
config_path = os.path.join(edgepath, 'mypy.ini')
if not os.path.exists(config_path):
raise RuntimeError('could not locate mypy.ini file')

try:
import mypy # NoQA
except ImportError:
raise unittest.SkipTest('mypy moudule is missing')

try:
subprocess.run(
[
sys.executable,
'-m',
'mypy',
'--config-file',
config_path,
'uvloop'
],
check=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
cwd=edgepath
)
except subprocess.CalledProcessError as ex:
output = ex.output.decode()
raise AssertionError(
'mypy validation failed:\n{}'.format(output)) from None
22 changes: 18 additions & 4 deletions uvloop/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import asyncio as __asyncio
import typing as _typing

from asyncio.events import BaseDefaultEventLoopPolicy as __BasePolicy

Expand All @@ -10,16 +11,16 @@
__all__ = ('new_event_loop', 'install', 'EventLoopPolicy')


class Loop(__BaseLoop, __asyncio.AbstractEventLoop):
class Loop(__BaseLoop, __asyncio.AbstractEventLoop): # type: ignore[misc]
pass


def new_event_loop():
def new_event_loop() -> Loop:
"""Return a new event loop."""
return Loop()


def install():
def install() -> None:
"""A helper function to install uvloop policy."""
__asyncio.set_event_loop_policy(EventLoopPolicy())

Expand All @@ -36,5 +37,18 @@ class EventLoopPolicy(__BasePolicy):
<uvloop.Loop running=False closed=False debug=False>
"""

def _loop_factory(self):
def _loop_factory(self) -> Loop:
return new_event_loop()

if _typing.TYPE_CHECKING:
# EventLoopPolicy doesn't implement these, but since they are marked
# as abstract in typeshed, we have to put them in so mypy thinks
# the base methods are overridden. This is the same approach taken
# for the Windows event loop policy classes in typeshed.
def get_child_watcher(self) -> _typing.NoReturn:
...

def set_child_watcher(
self, watcher: _typing.Any
) -> _typing.NoReturn:
...
2 changes: 1 addition & 1 deletion uvloop/_noop.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
def noop():
def noop() -> None:
"""Empty function to invoke CPython ceval loop."""
return
Loading