Skip to content
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

Initial work towards adding type hints #656

Merged
merged 3 commits into from
Sep 10, 2018
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ htmlcov/
.coverage.*
.cache
.pytest_cache/
.mypy_cache/
nosetests.xml
coverage.xml

Expand Down
26 changes: 26 additions & 0 deletions mypy.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
[mypy]
# TODO: run mypy against several OS/version combos in CI
# https://mypy.readthedocs.io/en/latest/command_line.html#platform-configuration

# Be flexible about unannotated imports
follow_imports = silent
ignore_missing_imports = True

# Be strict about use of Mypy
warn_unused_ignores = True
warn_unused_configs = True
warn_redundant_casts = True
warn_return_any = True

# Avoid subtle backsliding
#disallow_any_decorated = True
#disallow_incomplete_defs = True
#disallow_subclassing_any = True

# Enable gradually / for new modules
check_untyped_defs = False
disallow_untyped_calls = False
disallow_untyped_defs = False

# DO NOT use `ignore_errors`; it doesn't apply
# downstream and users have to deal with them.
36 changes: 14 additions & 22 deletions trio/_core/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,39 +14,31 @@ def _public(fn):
return fn


__all__ = []
from ._exceptions import (
TrioInternalError, RunFinishedError, WouldBlock, Cancelled,
ResourceBusyError, ClosedResourceError
)

from ._exceptions import *
__all__ += _exceptions.__all__
from ._multierror import MultiError

from ._multierror import *
__all__ += _multierror.__all__
from ._traps import cancel_shielded_checkpoint, Abort, wait_task_rescheduled

from ._traps import *
__all__ += _traps.__all__

from ._ki import *
__all__ += _ki.__all__
from ._ki import (
enable_ki_protection, disable_ki_protection, currently_ki_protected
)

# TODO: make the _run namespace a lot less magical
from ._run import *
__all__ += _run.__all__

from ._entry_queue import *
__all__ += _entry_queue.__all__
from ._entry_queue import TrioToken

from ._parking_lot import *
__all__ += _parking_lot.__all__
from ._parking_lot import ParkingLot

from ._unbounded_queue import *
__all__ += _unbounded_queue.__all__
from ._unbounded_queue import UnboundedQueue

from ._local import *
__all__ += _local.__all__
from ._local import RunVar

if hasattr(_run, "wait_readable"):
wait_socket_readable = wait_readable
wait_socket_writable = wait_writable
notify_socket_close = notify_fd_close
__all__ += [
"wait_socket_readable", "wait_socket_writable", "notify_socket_close"
]
10 changes: 8 additions & 2 deletions trio/_core/_ki.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@

from .._util import is_main_thread

if False:
from typing import Any, TypeVar, Callable
F = TypeVar('F', bound=Callable[..., Any])

__all__ = [
"enable_ki_protection",
"disable_ki_protection",
Expand Down Expand Up @@ -166,10 +170,12 @@ def wrapper(*args, **kwargs):
return decorator


enable_ki_protection = _ki_protection_decorator(True)
enable_ki_protection = _ki_protection_decorator(True) # type: Callable[[F], F]
enable_ki_protection.__name__ = "enable_ki_protection"

disable_ki_protection = _ki_protection_decorator(False)
disable_ki_protection = _ki_protection_decorator(
False
) # type: Callable[[F], F]
disable_ki_protection.__name__ = "disable_ki_protection"


Expand Down
2 changes: 1 addition & 1 deletion trio/_subprocess/unix_pipes.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@


class _PipeMixin:
def __init__(self, pipefd: int):
def __init__(self, pipefd: int) -> None:
if not isinstance(pipefd, int):
raise TypeError(
"{0.__class__.__name__} needs a pipe fd".format(self)
Expand Down
10 changes: 5 additions & 5 deletions trio/_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,11 +196,6 @@ def total_tokens(self):
"""
return self._total_tokens

def _wake_waiters(self):
available = self._total_tokens - len(self._borrowers)
for woken in self._lot.unpark(count=available):
self._borrowers.add(self._pending_borrowers.pop(woken))

@total_tokens.setter
def total_tokens(self, new_total_tokens):
if not isinstance(new_total_tokens, int):
Expand All @@ -210,6 +205,11 @@ def total_tokens(self, new_total_tokens):
self._total_tokens = new_total_tokens
self._wake_waiters()

def _wake_waiters(self):
available = self._total_tokens - len(self._borrowers)
for woken in self._lot.unpark(count=available):
self._borrowers.add(self._pending_borrowers.pop(woken))

@property
def borrowed_tokens(self):
"""The amount of capacity that's currently in use.
Expand Down
31 changes: 13 additions & 18 deletions trio/testing/__init__.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,24 @@
__all__ = []

# re-export
from .._core import wait_all_tasks_blocked
__all__.append("wait_all_tasks_blocked")

from ._trio_test import *
__all__ += _trio_test.__all__
from ._trio_test import trio_test

from ._mock_clock import *
__all__ += _mock_clock.__all__
from ._mock_clock import MockClock

from ._checkpoints import *
__all__ += _checkpoints.__all__
from ._checkpoints import assert_checkpoints, assert_no_checkpoints

from ._sequencer import *
__all__ += _sequencer.__all__
from ._sequencer import Sequencer

from ._check_streams import *
__all__ += _check_streams.__all__
from ._check_streams import (
check_one_way_stream, check_two_way_stream, check_half_closeable_stream
)

from ._memory_streams import *
__all__ += _memory_streams.__all__
from ._memory_streams import (
MemorySendStream, MemoryReceiveStream, memory_stream_pump,
memory_stream_one_way_pair, memory_stream_pair,
lockstep_stream_one_way_pair, lockstep_stream_pair
)

from ._network import *
__all__ += _network.__all__
from ._network import open_stream_to_socket_listener

################################################################

Expand Down
18 changes: 9 additions & 9 deletions trio/testing/_mock_clock.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,15 @@ def rate(self, new_rate):
def autojump_threshold(self):
return self._autojump_threshold

@autojump_threshold.setter
def autojump_threshold(self, new_autojump_threshold):
self._autojump_threshold = float(new_autojump_threshold)
self._maybe_spawn_autojump_task()
if self._autojump_cancel_scope is not None:
# Task is running and currently blocked on the old setting, wake
# it up so it picks up the new setting
self._autojump_cancel_scope.cancel()

async def _autojumper(self):
while True:
with _core.open_cancel_scope() as cancel_scope:
Expand Down Expand Up @@ -161,15 +170,6 @@ def _maybe_spawn_autojump_task(self):
if clock is self:
self._autojump_task = _core.spawn_system_task(self._autojumper)

@autojump_threshold.setter
def autojump_threshold(self, new_autojump_threshold):
self._autojump_threshold = float(new_autojump_threshold)
self._maybe_spawn_autojump_task()
if self._autojump_cancel_scope is not None:
# Task is running and currently blocked on the old setting, wake
# it up so it picks up the new setting
self._autojump_cancel_scope.cancel()

def _real_to_virtual(self, real):
real_offset = real - self._real_base
virtual_offset = self._rate * real_offset
Expand Down
9 changes: 6 additions & 3 deletions trio/testing/_sequencer.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
from .. import _util
from .. import Event

if False:
from typing import DefaultDict, Set

__all__ = ["Sequencer"]


Expand Down Expand Up @@ -53,13 +56,13 @@ async def main():

_sequence_points = attr.ib(
default=attr.Factory(lambda: defaultdict(Event)), init=False
)
_claimed = attr.ib(default=attr.Factory(set), init=False)
) # type: DefaultDict[int, Event]
_claimed = attr.ib(default=attr.Factory(set), init=False) # type: Set[int]
_broken = attr.ib(default=False, init=False)

@asynccontextmanager
@async_generator
async def __call__(self, position):
async def __call__(self, position: int):
if position in self._claimed:
raise RuntimeError(
"Attempted to re-use sequence point {}".format(position)
Expand Down