Skip to content

Commit fc1b4ba

Browse files
committed
Add typings
1 parent 3810530 commit fc1b4ba

File tree

11 files changed

+360
-10
lines changed

11 files changed

+360
-10
lines changed

.flake8

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
[flake8]
2-
filename = *.py,*.pyx,*.pxd,*.pxi
2+
filename = *.py,*.pyx,*.pxd,*.pxi,*.pyi
33
ignore = E402,E731,D100,D101,D102,D103,D104,D105,W503,W504,E252
44
exclude = .git,__pycache__,build,dist,.eggs,postgres,vendor
55

6-
per-file-ignores = *.pyx,*.pxd,*.pxi: E211, E222, E225, E226, E227, E999
6+
per-file-ignores =
7+
*.pyx,*.pxd,*.pxi: E211, E222, E225, E226, E227, E999
8+
*.pyi: F401, F403, F405, F811, E127, E128, E203, E266, E301, E302, E305, E501, E701, E704, E741, B303, W503, W504

.github/workflows/test-requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ aiohttp
33
psutil
44
pyOpenSSL==19.0.0
55
flake8>=3.7.5
6+
mypy>=0.790

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,4 @@ docs/_build
3030
uvloop/loop.*.pyd
3131
/.pytest_cache/
3232
/.mypy_cache/
33-
33+
/.venv*

MANIFEST.in

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
recursive-include docs *.py *.rst
22
recursive-include examples *.py
33
recursive-include tests *.py *.pem
4-
recursive-include uvloop *.pyx *.pxd *.pxi *.py *.c *.h
4+
recursive-include uvloop *.pyx *.pxd *.pxi *.py *.c *.h *.pyi py.typed
55
recursive-include vendor/libuv *
66
recursive-exclude vendor/libuv/.git *
77
recursive-exclude vendor/libuv/docs *
88
recursive-exclude vendor/libuv/img *
9-
include LICENSE-MIT LICENSE-APACHE README.rst Makefile performance.png .flake8
9+
include LICENSE-MIT LICENSE-APACHE README.rst Makefile performance.png .flake8 mypy.ini

mypy.ini

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[mypy]
2+
incremental = True
3+
strict = True
4+
implicit_reexport = True
5+
# disallow_any_unimported = True
6+
7+
[mypy-uvloop._patch]
8+
ignore_errors = True
9+
10+
[mypy-uvloop._testbase]
11+
ignore_errors = True

requirements.dev.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ Sphinx>=1.4.1
33
psutil
44
pyOpenSSL==19.0.0
55
flake8>=3.7.5
6+
mypy>=0.790

tests/test_sourcecode.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,34 @@ def test_flake8(self):
3535
output = ex.output.decode()
3636
raise AssertionError(
3737
'flake8 validation failed:\n{}'.format(output)) from None
38+
39+
def test_mypy(self):
40+
edgepath = find_uvloop_root()
41+
config_path = os.path.join(edgepath, 'mypy.ini')
42+
if not os.path.exists(config_path):
43+
raise RuntimeError('could not locate mypy.ini file')
44+
45+
try:
46+
import mypy # NoQA
47+
except ImportError:
48+
raise unittest.SkipTest('mypy moudule is missing')
49+
50+
try:
51+
subprocess.run(
52+
[
53+
sys.executable,
54+
'-m',
55+
'mypy',
56+
'--config-file',
57+
config_path,
58+
'uvloop'
59+
],
60+
check=True,
61+
stdout=subprocess.PIPE,
62+
stderr=subprocess.PIPE,
63+
cwd=edgepath
64+
)
65+
except subprocess.CalledProcessError as ex:
66+
output = ex.output.decode()
67+
raise AssertionError(
68+
'mypy validation failed:\n{}'.format(output)) from None

uvloop/__init__.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import asyncio as __asyncio
2+
import typing as _typing
23

34
from asyncio.events import BaseDefaultEventLoopPolicy as __BasePolicy
45

@@ -11,16 +12,16 @@
1112
__all__ = ('new_event_loop', 'install', 'EventLoopPolicy')
1213

1314

14-
class Loop(__BaseLoop, __asyncio.AbstractEventLoop):
15+
class Loop(__BaseLoop, __asyncio.AbstractEventLoop): # type: ignore[misc]
1516
pass
1617

1718

18-
def new_event_loop():
19+
def new_event_loop() -> Loop:
1920
"""Return a new event loop."""
2021
return Loop()
2122

2223

23-
def install():
24+
def install() -> None:
2425
"""A helper function to install uvloop policy."""
2526
__asyncio.set_event_loop_policy(EventLoopPolicy())
2627

@@ -37,5 +38,18 @@ class EventLoopPolicy(__BasePolicy):
3738
<uvloop.Loop running=False closed=False debug=False>
3839
"""
3940

40-
def _loop_factory(self):
41+
def _loop_factory(self) -> Loop:
4142
return new_event_loop()
43+
44+
if _typing.TYPE_CHECKING:
45+
# EventLoopPolicy doesn't implement these, but since they are marked
46+
# as abstract in typeshed, we have to put them in so mypy thinks
47+
# the base methods are overridden. This is the same approach taken
48+
# for the Windows event loop policy classes in typeshed.
49+
def get_child_watcher(self) -> _typing.NoReturn:
50+
...
51+
52+
def set_child_watcher(
53+
self, watcher: _typing.Any
54+
) -> _typing.NoReturn:
55+
...

uvloop/_noop.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
def noop():
1+
def noop() -> None:
22
"""Empty function to invoke CPython ceval loop."""
33
return

0 commit comments

Comments
 (0)