-
-
Notifications
You must be signed in to change notification settings - Fork 349
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
Enforce mypy-cleanliness on trio._core #1610
Changes from all commits
b0ed1e4
048ca9b
1c7cdc2
5ce4a1c
659c4f2
2cc70a1
0efb647
81f83f5
affba27
205a710
5d2470f
316b640
296490f
42019f1
3a21fb3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,8 @@ | |
are publicly available in either trio, trio.lowlevel, or trio.testing. | ||
""" | ||
|
||
import sys | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess technically this should be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unfortunately, importing There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Yeah, mypy's platform guards are rather brittle. It's strange since, for example, the magic There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks, added |
||
|
||
from ._exceptions import ( | ||
TrioInternalError, | ||
RunFinishedError, | ||
|
@@ -73,14 +75,8 @@ | |
|
||
from ._mock_clock import MockClock | ||
|
||
# Kqueue imports | ||
try: | ||
from ._run import current_kqueue, monitor_kevent, wait_kevent | ||
except ImportError: | ||
pass | ||
|
||
# Windows imports | ||
try: | ||
if sys.platform == "win32": | ||
from ._run import ( | ||
monitor_completion_key, | ||
current_iocp, | ||
|
@@ -89,5 +85,8 @@ | |
write_overlapped, | ||
readinto_overlapped, | ||
) | ||
except ImportError: | ||
pass | ||
# Kqueue imports | ||
elif sys.platform != "linux" and sys.platform != "win32": | ||
from ._run import current_kqueue, monitor_kevent, wait_kevent | ||
|
||
del sys # It would be better to import sys as _sys, but mypy does not understand it |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,7 +19,7 @@ class _KqueueStatistics: | |
|
||
@attr.s(slots=True, eq=False) | ||
class KqueueIOManager: | ||
_kqueue = attr.ib(factory=select.kqueue) | ||
_kqueue = attr.ib(factory=select.kqueue) # type: ignore | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This works because we require Linux when running mypy. But if I don't specify the platform, I get the following errors on macOS:
I plan to fix that in a further pull request by adding |
||
# {(ident, filter): Task or UnboundedQueue} | ||
_registered = attr.ib(factory=dict) | ||
_force_wakeup = attr.ib(factory=WakeupSocketpair) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -415,7 +415,7 @@ def traceback_exception_init( | |
self.embedded = [] | ||
|
||
|
||
traceback.TracebackException.__init__ = traceback_exception_init | ||
traceback.TracebackException.__init__ = traceback_exception_init # type: ignore | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not too worried about There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The error is |
||
traceback_exception_original_format = traceback.TracebackException.format | ||
|
||
|
||
|
@@ -427,7 +427,7 @@ def traceback_exception_format(self, *, chain=True): | |
yield from (textwrap.indent(line, " " * 2) for line in exc.format(chain=chain)) | ||
|
||
|
||
traceback.TracebackException.format = traceback_exception_format | ||
traceback.TracebackException.format = traceback_exception_format # type: ignore | ||
|
||
|
||
def trio_excepthook(etype, value, tb): | ||
|
@@ -489,7 +489,7 @@ class TrioFakeSysModuleForApport: | |
|
||
fake_sys = TrioFakeSysModuleForApport() | ||
fake_sys.__dict__.update(sys.__dict__) | ||
fake_sys.__excepthook__ = trio_excepthook | ||
fake_sys.__excepthook__ = trio_excepthook # type: ignore | ||
apport_python_hook.sys = fake_sys | ||
|
||
monkeypatched_or_warned = True | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,13 +16,14 @@ | |
from contextvars import copy_context | ||
from math import inf | ||
from time import perf_counter | ||
from typing import Callable, TYPE_CHECKING | ||
|
||
from sniffio import current_async_library_cvar | ||
|
||
import attr | ||
from heapq import heapify, heappop, heappush | ||
from sortedcontainers import SortedDict | ||
from outcome import Error, Value, capture | ||
from outcome import Error, Outcome, Value, capture | ||
|
||
from ._entry_queue import EntryQueue, TrioToken | ||
from ._exceptions import TrioInternalError, RunFinishedError, Cancelled | ||
|
@@ -655,7 +656,7 @@ def shield(self): | |
""" | ||
return self._shield | ||
|
||
@shield.setter | ||
@shield.setter # type: ignore # "decorated property not supported" | ||
@enable_ki_protection | ||
def shield(self, new_value): | ||
if not isinstance(new_value, bool): | ||
|
@@ -1223,7 +1224,8 @@ class GuestState: | |
run_sync_soon_not_threadsafe = attr.ib() | ||
done_callback = attr.ib() | ||
unrolled_run_gen = attr.ib() | ||
unrolled_run_next_send = attr.ib(factory=lambda: Value(None)) | ||
_value_factory: Callable[[], Value] = lambda: Value(None) | ||
unrolled_run_next_send = attr.ib(factory=_value_factory, type=Outcome) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The error message without the lambda type hint is So far so good, but if I switch to your inline cast which specifies the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh, this is probably because We should add type hints to |
||
|
||
def guest_tick(self): | ||
try: | ||
|
@@ -2374,13 +2376,13 @@ async def checkpoint_if_cancelled(): | |
task._cancel_points += 1 | ||
|
||
|
||
if os.name == "nt": | ||
if sys.platform == "win32": | ||
from ._io_windows import WindowsIOManager as TheIOManager | ||
from ._generated_io_windows import * | ||
elif hasattr(select, "epoll"): | ||
elif sys.platform == "linux" or (not TYPE_CHECKING and hasattr(select, "epoll")): | ||
from ._io_epoll import EpollIOManager as TheIOManager | ||
from ._generated_io_epoll import * | ||
elif hasattr(select, "kqueue"): | ||
elif TYPE_CHECKING or hasattr(select, "kqueue"): | ||
from ._io_kqueue import KqueueIOManager as TheIOManager | ||
from ._generated_io_kqueue import * | ||
else: # pragma: no cover | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you edit this comment to mention your future plans listed below? (run mypy on all platforms, use assert guards to avoid typechecking the wrong IOManagers)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, done