Skip to content

Commit b0ed1e4

Browse files
committed
Enforce mypy-cleanliness on trio._core
1 parent f5abb0e commit b0ed1e4

File tree

9 files changed

+43
-19
lines changed

9 files changed

+43
-19
lines changed

check.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@ flake8 trio/ \
2323
--ignore=D,E,W,F401,F403,F405,F821,F822\
2424
|| EXIT_STATUS=$?
2525

26+
# Run mypy
27+
# We specify Linux so that we get the same results on all platforms. Without
28+
# that, mypy would complain on macOS that epoll does not exist, and we can't
29+
# ignore it as it would cause a mypy error on Linux
30+
mypy -p trio._core --platform linux || EXIT_STATUS=$?
31+
2632
# Finally, leave a really clear warning of any issues and exit
2733
if [ $EXIT_STATUS -ne 0 ]; then
2834
cat <<EOF

test-requirements.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ jedi # for jedi code completion tests
1111
black; implementation_name == "cpython"
1212
flake8
1313
astor # code generation
14+
mypy
1415

1516
# https://github.com/python-trio/trio/pull/654#issuecomment-420518745
1617
typed_ast; implementation_name == "cpython"

test-requirements.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ jedi==0.17.0 # via -r test-requirements.in, ipython
2626
lazy-object-proxy==1.4.3 # via astroid
2727
mccabe==0.6.1 # via flake8, pylint
2828
more-itertools==8.3.0 # via pytest
29+
mypy-extensions==0.4.3 # via mypy
30+
mypy==0.780 # via -r test-requirements.in
2931
outcome==1.0.1 # via -r test-requirements.in
3032
packaging==20.4 # via pytest
3133
parso==0.7.0 # via jedi
@@ -53,5 +55,6 @@ toml==0.10.1 # via black, pylint
5355
traitlets==4.3.3 # via ipython
5456
trustme==0.6.0 # via -r test-requirements.in
5557
typed-ast==1.4.1 ; implementation_name == "cpython" # via -r test-requirements.in, black
58+
typing-extensions==3.7.4.2 # via mypy
5659
wcwidth==0.2.4 # via prompt-toolkit, pytest
5760
wrapt==1.12.1 # via astroid

trio/_core/__init__.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
are publicly available in either trio, trio.lowlevel, or trio.testing.
55
"""
66

7+
import typing as _t
8+
import sys
9+
710
from ._exceptions import (
811
TrioInternalError,
912
RunFinishedError,
@@ -74,13 +77,18 @@
7477
from ._mock_clock import MockClock
7578

7679
# Kqueue imports
77-
try:
78-
from ._run import current_kqueue, monitor_kevent, wait_kevent
79-
except ImportError:
80-
pass
80+
if (
81+
sys.platform.startswith("freebsd")
82+
or sys.platform.startswith("darwin")
83+
or not _t.TYPE_CHECKING
84+
):
85+
try:
86+
from ._run import current_kqueue, monitor_kevent, wait_kevent
87+
except ImportError:
88+
pass
8189

8290
# Windows imports
83-
try:
91+
if sys.platform == "win32":
8492
from ._run import (
8593
monitor_completion_key,
8694
current_iocp,
@@ -89,5 +97,3 @@
8997
write_overlapped,
9098
readinto_overlapped,
9199
)
92-
except ImportError:
93-
pass

trio/_core/_io_epoll.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import select
22
import attr
33
from collections import defaultdict
4+
from typing import DefaultDict
45

56
from .. import _core
67
from ._run import _public
@@ -184,8 +185,10 @@ class EpollWaiters:
184185
class EpollIOManager:
185186
_epoll = attr.ib(factory=select.epoll)
186187
# {fd: EpollWaiters}
187-
_registered = attr.ib(factory=lambda: defaultdict(EpollWaiters))
188-
_force_wakeup = attr.ib(factory=WakeupSocketpair)
188+
_registered = attr.ib(
189+
factory=lambda: defaultdict(EpollWaiters), type=DefaultDict[int, EpollWaiters]
190+
)
191+
_force_wakeup = attr.ib(factory=WakeupSocketpair, type=WakeupSocketpair)
189192
_force_wakeup_fd = attr.ib(default=None)
190193

191194
def __attrs_post_init__(self):

trio/_core/_io_kqueue.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class _KqueueStatistics:
1919

2020
@attr.s(slots=True, eq=False)
2121
class KqueueIOManager:
22-
_kqueue = attr.ib(factory=select.kqueue)
22+
_kqueue = attr.ib(factory=select.kqueue) # type: ignore
2323
# {(ident, filter): Task or UnboundedQueue}
2424
_registered = attr.ib(factory=dict)
2525
_force_wakeup = attr.ib(factory=WakeupSocketpair)

trio/_core/_multierror.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,7 @@ def traceback_exception_init(
415415
self.embedded = []
416416

417417

418-
traceback.TracebackException.__init__ = traceback_exception_init
418+
traceback.TracebackException.__init__ = traceback_exception_init # type: ignore
419419
traceback_exception_original_format = traceback.TracebackException.format
420420

421421

@@ -427,7 +427,7 @@ def traceback_exception_format(self, *, chain=True):
427427
yield from (textwrap.indent(line, " " * 2) for line in exc.format(chain=chain))
428428

429429

430-
traceback.TracebackException.format = traceback_exception_format
430+
traceback.TracebackException.format = traceback_exception_format # type: ignore
431431

432432

433433
def trio_excepthook(etype, value, tb):
@@ -489,7 +489,7 @@ class TrioFakeSysModuleForApport:
489489

490490
fake_sys = TrioFakeSysModuleForApport()
491491
fake_sys.__dict__.update(sys.__dict__)
492-
fake_sys.__excepthook__ = trio_excepthook
492+
fake_sys.__excepthook__ = trio_excepthook # type: ignore
493493
apport_python_hook.sys = fake_sys
494494

495495
monkeypatched_or_warned = True

trio/_core/_run.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from contextvars import copy_context
1717
from math import inf
1818
from time import perf_counter
19+
from typing import TYPE_CHECKING, Any
1920

2021
from sniffio import current_async_library_cvar
2122

@@ -582,7 +583,7 @@ def shield(self):
582583
"""
583584
return self._shield
584585

585-
@shield.setter
586+
@shield.setter # type: ignore # "decorated property not supported"
586587
@enable_ki_protection
587588
def shield(self, new_value):
588589
if not isinstance(new_value, bool):
@@ -1150,7 +1151,7 @@ class GuestState:
11501151
run_sync_soon_not_threadsafe = attr.ib()
11511152
done_callback = attr.ib()
11521153
unrolled_run_gen = attr.ib()
1153-
unrolled_run_next_send = attr.ib(factory=lambda: Value(None))
1154+
unrolled_run_next_send = attr.ib(factory=lambda: Value(None), type=object)
11541155

11551156
def guest_tick(self):
11561157
try:
@@ -2311,13 +2312,17 @@ async def checkpoint_if_cancelled():
23112312
task._cancel_points += 1
23122313

23132314

2314-
if os.name == "nt":
2315+
if sys.platform == "win32":
23152316
from ._io_windows import WindowsIOManager as TheIOManager
23162317
from ._generated_io_windows import *
2317-
elif hasattr(select, "epoll"):
2318+
elif sys.platform == "linux" or (not TYPE_CHECKING and hasattr(select, "epoll")):
23182319
from ._io_epoll import EpollIOManager as TheIOManager
23192320
from ._generated_io_epoll import *
2320-
elif hasattr(select, "kqueue"):
2321+
elif (
2322+
sys.platform == "darwin"
2323+
or sys.platform.startswith("freebsd")
2324+
or (not TYPE_CHECKING and hasattr(select, "epoll"))
2325+
):
23212326
from ._io_kqueue import KqueueIOManager as TheIOManager
23222327
from ._generated_io_kqueue import *
23232328
else: # pragma: no cover

trio/_core/tests/test_multierror.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
extract_tb,
66
print_exception,
77
format_exception,
8-
_cause_message,
98
)
9+
from traceback import _cause_message # type: ignore
1010
import sys
1111
import os
1212
import re

0 commit comments

Comments
 (0)