Skip to content

Commit

Permalink
fix typing for 3.8
Browse files Browse the repository at this point in the history
  • Loading branch information
kristjanvalur committed Dec 16, 2023
1 parent acb7687 commit 8794848
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 22 deletions.
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ allow_untyped_calls = true
module = [
"asynkit.compat",
"asynkit.experimental.interrupt",
"asynkit.experimental.priority",
"asynkit.loop.eventloop",
"asynkit.tools",
"tests.test_coro",
Expand Down
9 changes: 8 additions & 1 deletion src/asynkit/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from asyncio import AbstractEventLoop, Handle, events
from contextvars import Context
from typing import TYPE_CHECKING, Any, Callable, Coroutine, Optional, TypeVar
from weakref import ReferenceType

"""Compatibility routines for earlier asyncio versions"""

Expand All @@ -13,10 +14,16 @@

T = TypeVar("T")

# The following is needed for mypy to work with Python 3.8
# which doesn't allow subscripting many types
if TYPE_CHECKING:
_TaskAny = asyncio.Task[Any]
FutureBool = asyncio.Future[bool]
ReferenceTypeTaskAny = ReferenceType[_TaskAny]
else:
_TaskAny = asyncio.Task
FutureBool = asyncio.Future
ReferenceTypeTaskAny = ReferenceType

# create_task() got the name argument in 3.8

Expand Down Expand Up @@ -56,7 +63,7 @@ def call_soon(


if not PYTHON_39: # pragma: no cover
from asyncio.mixins import _LoopBoundMixin
from asyncio.mixins import _LoopBoundMixin # type: ignore[import]

LoopBoundMixin = _LoopBoundMixin

Expand Down
53 changes: 33 additions & 20 deletions src/asynkit/experimental/priority.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

from typing_extensions import Literal

from asynkit.compat import LockHelper
from asynkit.compat import PYTHON_38, FutureBool, LockHelper, ReferenceTypeTaskAny
from asynkit.loop.default import task_from_handle
from asynkit.loop.schedulingloop import AbstractSchedulingLoop
from asynkit.loop.types import TaskAny
Expand Down Expand Up @@ -105,17 +105,13 @@ class PriorityLock(Lock, BasePriorityObject, LockHelper):
of all waiting Tasks.
"""

_waiters: Optional[
PriorityQueue[
float, Tuple[asyncio.Future[bool], weakref.ReferenceType[TaskAny]]
]
]
_waiters: Optional[PriorityQueue[float, Tuple[FutureBool, ReferenceTypeTaskAny]]]

def __init__(self) -> None:
# we use weakrefs to avoid reference cycles. Tasks _own_ locks,
# but locks don't own tasks. These are _backrefs_.
super().__init__()
self._owning: Optional[weakref.ReferenceType[TaskAny]] = None
self._owning: Optional[ReferenceTypeTaskAny] = None

async def acquire(self) -> Literal[True]:
"""Acquire a lock.
Expand Down Expand Up @@ -232,7 +228,7 @@ def propagate_priority(self, from_obj: Any) -> None:
else:

def key(
entry: Tuple[asyncio.Future[bool], weakref.ReferenceType[TaskAny]],
entry: Tuple[FutureBool, ReferenceTypeTaskAny],
) -> bool:
fut, _ = entry
return fut is from_obj
Expand All @@ -249,7 +245,7 @@ class PriorityCondition(asyncio.Condition, LockHelper):
def __init__(self, lock: Optional[asyncio.Lock] = None) -> None:
lock = lock or self.LockType()
super().__init__(lock=lock)
self._waiters: PriorityQueue[float, asyncio.Future[bool]] = PriorityQueue()
self._waiters: PriorityQueue[float, FutureBool] = PriorityQueue()

async def wait(self) -> Literal[True]:
if not self.locked(): # pragma: no cover
Expand Down Expand Up @@ -593,14 +589,25 @@ def reschedule_all(self) -> None:
self._pq.extend(newpri)


class EventLoopLike(Protocol):
def call_soon(
self,
callback: Callable[..., Any],
*args: Any,
context: Optional[Context] = None,
) -> Handle:
... # pragma: no cover
class EventLoopLike(Protocol): # pragma: no cover
if not PYTHON_38:

def call_soon(
self,
callback: Callable[..., Any],
*args: Any,
context: Optional[Context] = None,
) -> Handle:
... # pragma: no cover

else:

def call_soon( # type: ignore[misc]
self,
callback: Callable[..., Any],
*args: Any,
) -> Handle:
... # pragma: no cover


class PrioritySchedulingMixin(AbstractSchedulingLoop, EventLoopLike):
Expand Down Expand Up @@ -649,7 +656,10 @@ def call_pos(
This is effectively the same as calling
`call_soon()`, `queue_remove()` and `queue_insert_pos()` in turn.
"""
handle = self.call_soon(callback, *args, context=context)
if not PYTHON_38: # pragma: no cover
handle = self.call_soon(callback, *args, context=context)
else: # pragma: no cover
handle = self.call_soon(callback, *args)
self.queue_remove(handle)
self.queue_insert_pos(handle, position)
return handle
Expand Down Expand Up @@ -680,7 +690,10 @@ def key(handle: Handle) -> bool:
self.ready_queue.reschedule(key, priority)


class PrioritySelectorEventLoop(asyncio.SelectorEventLoop, PrioritySchedulingMixin):
class PrioritySelectorEventLoop(
asyncio.SelectorEventLoop,
PrioritySchedulingMixin,
): # type: ignore
def __init__(self, arg: Any = None) -> None:
super().__init__(arg)
self.init()
Expand All @@ -691,7 +704,7 @@ def __init__(self, arg: Any = None) -> None:

if hasattr(asyncio, "ProactorEventLoop"): # pragma: no coverage

class PriorityProactorEventLoop(asyncio.ProactorEventLoop, PrioritySchedulingMixin):
class PriorityProactorEventLoop(asyncio.ProactorEventLoop, PrioritySchedulingMixin): # type: ignore
def __init__(self, arg: Any = None) -> None:
super().__init__(arg)
self.init()
Expand Down
2 changes: 1 addition & 1 deletion tests/experimental/test_priority.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
import pytest

from asynkit.experimental.priority import (
DefaultPriorityEventLoop,
PosPriorityQueue,
PriorityCondition,
PriorityLock,
DefaultPriorityEventLoop,
PriorityTask,
)

Expand Down

0 comments on commit 8794848

Please sign in to comment.