Skip to content

Commit

Permalink
fix: Correct the type annotation of create_timer() (#61)
Browse files Browse the repository at this point in the history
Backported-from: main
Backported-to: 1.x
  • Loading branch information
achimnol committed Aug 25, 2023
1 parent 13291e2 commit 0f431ba
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 4 deletions.
1 change: 1 addition & 0 deletions changes/61.fix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Correct the type annotation of the callback argument in `create_timer()`
12 changes: 8 additions & 4 deletions src/aiotools/timer.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

from .compat import get_running_loop
from .taskgroup import TaskGroup
from .types import CoroutineLike

__all__ = (
'create_timer',
Expand All @@ -28,9 +29,12 @@ class TimerDelayPolicy(enum.Enum):
CANCEL = 1


def create_timer(cb: Callable[[float], None], interval: float,
delay_policy: TimerDelayPolicy = TimerDelayPolicy.DEFAULT,
loop: Optional[asyncio.AbstractEventLoop] = None) -> asyncio.Task:
def create_timer(
cb: Callable[[float], CoroutineLike[None]],
interval: float,
delay_policy: TimerDelayPolicy = TimerDelayPolicy.DEFAULT,
loop: Optional[asyncio.AbstractEventLoop] = None,
) -> asyncio.Task:
"""
Schedule a timer with the given callable and the interval in seconds.
The interval value is also passed to the callable.
Expand Down Expand Up @@ -59,7 +63,7 @@ async def _timer():
fired_tasks.clear()
else:
fired_tasks[:] = [t for t in fired_tasks if not t.done()]
t = task_group.create_task(cb(interval=interval))
t = task_group.create_task(cb(interval))
fired_tasks.append(t)
await asyncio.sleep(interval)
except asyncio.CancelledError:
Expand Down
21 changes: 21 additions & 0 deletions src/aiotools/types.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from __future__ import annotations

import sys
from typing import (
Any,
Awaitable,
Coroutine,
Generator,
TypeVar,
)
from typing_extensions import TypeAlias

_T = TypeVar("_T")

# taken from the typeshed
if sys.version_info >= (3, 12):
AwaitableLike: TypeAlias = Awaitable[_T] # noqa: Y047
CoroutineLike: TypeAlias = Coroutine[Any, Any, _T] # noqa: Y047
else:
AwaitableLike: TypeAlias = Generator[Any, None, _T] | Awaitable[_T]
CoroutineLike: TypeAlias = Generator[Any, None, _T] | Coroutine[Any, Any, _T]

0 comments on commit 0f431ba

Please sign in to comment.