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 617aeac
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 10 deletions.
7 changes: 7 additions & 0 deletions 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
14 changes: 5 additions & 9 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 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
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 617aeac

Please sign in to comment.