Skip to content

Commit

Permalink
Refactor module to package and incl py.typed. Fix #93 (#94)
Browse files Browse the repository at this point in the history
* Refactor module to package and incl py.typed

* Include type for the weakset

* Assist mypy to notice that `loop` loses `Optional`

* Ignore a mypy error about a private attribute access

* Try AbstractSet type sig for Python 3.8
  • Loading branch information
cjrh authored Jan 27, 2025
1 parent aec5682 commit b147c62
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions aiorun.py → aiorun/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from asyncio import AbstractEventLoop, CancelledError, all_tasks, get_event_loop
from concurrent.futures import Executor, ThreadPoolExecutor
from functools import partial
from typing import Awaitable, Callable, Coroutine, Optional, Union
from typing import Awaitable, Callable, Coroutine, Optional, Union, AbstractSet
from weakref import WeakSet

ShutdownCallback = Optional[
Expand All @@ -26,7 +26,8 @@
WINDOWS = sys.platform == "win32"


_DO_NOT_CANCEL_COROS = WeakSet()
# TODO: when 3.8 is dropped, replace `AbstractSet` with `WeakSet`
_DO_NOT_CANCEL_COROS: AbstractSet[Coroutine] = WeakSet()


def shutdown_waits_for(coro, loop=None):
Expand Down Expand Up @@ -170,7 +171,10 @@ def run(

loop_was_supplied = bool(loop)

if not loop_was_supplied:
# If we check `loop_was_supplied` here, mypy will forever complain
# that `loop` might be None. So we have to check loop directly here
# to silence these incorrect mypy complaints. Yay for typing.
if not loop:
if use_uvloop:
import uvloop

Expand Down Expand Up @@ -293,7 +297,7 @@ async def new_coro():
for t in tasks:
# TODO: we don't need access to the coro. We could simply
# TODO: store the task itself in the weakset.
if t._coro not in _DO_NOT_CANCEL_COROS:
if t._coro not in _DO_NOT_CANCEL_COROS: # type: ignore
t.cancel()

async def wait_for_cancelled_tasks(timeout):
Expand Down
Empty file added aiorun/py.typed
Empty file.

0 comments on commit b147c62

Please sign in to comment.