From 0e07f9d55a609d4f450fb443ab4de4eac6f2fc91 Mon Sep 17 00:00:00 2001 From: generatedunixname89002005287564 Date: Wed, 30 Oct 2024 19:59:33 -0700 Subject: [PATCH] Pre-silence Pyre Errors for upcoming upgrade] [batch:85/596] [shard:2/N] Reviewed By: MaggieMoss Differential Revision: D65218362 fbshipit-source-id: 87ebd67eb1b92b346e51641a4a37d2be4178828b --- later/task.py | 20 ++++++++++++++++++++ later/tests/test_task.py | 11 +++++++++++ 2 files changed, 31 insertions(+) diff --git a/later/task.py b/later/task.py index 368dc61..2899f7c 100644 --- a/later/task.py +++ b/later/task.py @@ -66,7 +66,9 @@ class TaskSentinel(asyncio.Task): def __init__(self) -> None: fake = Mock() + # pyre-fixme[6]: For 1st argument expected `Future[_T]` but got `TaskSentinel`. asyncio.Future.__init__(self, loop=fake) # typing: ignore, don't create a loop + # pyre-fixme[6]: For 1st argument expected `Future[_T]` but got `TaskSentinel`. asyncio.Future.set_result(self, None) @@ -201,6 +203,8 @@ async def _run_scheduled(self) -> None: task = await task if isinstance(task, asyncio.Task): + # pyre-fixme[6]: For 1st argument expected `Future[Any]` but got + # `Task[Any]`. self._tasks[task] = fixer else: raise TypeError(f"{fixer}(START_TASK) failed to return a task.") @@ -245,6 +249,8 @@ async def tasks_changed() -> None: return True elif task is not START_TASK: if task in self._tasks: + # pyre-fixme[6]: For 1st argument expected `Future[Any]` but got + # `Task[Any]`. del self._tasks[task] await tasks_changed() return True @@ -288,6 +294,8 @@ def watch( self._shielded_tasks[task] = asyncio.shield(task) self._tasks[self._shielded_tasks[task]] = None else: + # pyre-fixme[6]: For 1st argument expected `Future[Any]` but got + # `Task[Any]`. self._tasks[task] = fixer self._tasks_changed.set_nowait() @@ -353,6 +361,7 @@ async def __aexit__( if cancel_task in done: break # Don't bother doing fixes just break out for task in done: + # pyre-fixme[22]: The cast is redundant. task = cast(asyncio.Task, task) if task is changed_task: continue @@ -370,6 +379,8 @@ async def __aexit__( async def _event_task_cleanup(self, *tasks: asyncio.Task) -> None: for task in tasks: if task is not START_TASK: + # pyre-fixme[6]: For 1st argument expected `Future[Any]` but got + # `Task[Any]`. await cancel(task) async def _fix_task(self, task: asyncio.Task) -> None: @@ -379,8 +390,11 @@ async def _fix_task(self, task: asyncio.Task) -> None: task.result() if self.done_ok: # clean up the task is done. And thats okay. + # pyre-fixme[6]: For 1st argument expected `Future[Any]` but got + # `Task[Any]`. del self._tasks[task] return + # pyre-fixme[6]: For 1st argument expected `Future[Any]` but got `Task[Any]`. fixer = self._tasks[task] if fixer is None: raise RuntimeError(f"{task} finished and there is no fixer!") from exc @@ -389,7 +403,11 @@ async def _fix_task(self, task: asyncio.Task) -> None: new_task = await new_task if isinstance(new_task, asyncio.Task): + # pyre-fixme[6]: For 1st argument expected `Future[Any]` but got + # `Task[Any]`. del self._tasks[task] + # pyre-fixme[6]: For 1st argument expected `Future[Any]` but got + # `Task[Any]`. self._tasks[new_task] = fixer else: raise TypeError( @@ -534,6 +552,8 @@ async def wrapped(*args: TParams.args, **kwargs: TParams.kwargs) -> T: return await asyncio.shield(task) except asyncio.CancelledError: if count_task.count == 1: + # pyre-fixme[6]: For 1st argument expected `Future[Any]` but got + # `Task[Any]`. await cancel(task) raise # always re-raise CancelledError finally: diff --git a/later/tests/test_task.py b/later/tests/test_task.py index d3439a4..f2d5a7d 100644 --- a/later/tests/test_task.py +++ b/later/tests/test_task.py @@ -30,12 +30,14 @@ async def test_as_task(self) -> None: tsleep = later.as_task(asyncio.sleep) task = tsleep(500) self.assertIsInstance(task, asyncio.Task) + # pyre-fixme[6]: For 1st argument expected `Future[Any]` but got `Task[None]`. await later.cancel(task) self.assertTrue(task.done()) self.assertTrue(task.cancelled()) async def test_cancel_task(self) -> None: task: asyncio.Task = asyncio.get_running_loop().create_task(asyncio.sleep(500)) + # pyre-fixme[6]: For 1st argument expected `Future[Any]` but got `Task[None]`. await later.cancel(task) self.assertTrue(task.done()) self.assertTrue(task.cancelled()) @@ -56,6 +58,8 @@ async def _coro() -> None: await asyncio.sleep(0) self.assertTrue(started) with self.assertRaises(TypeError): + # pyre-fixme[6]: For 1st argument expected `Future[Any]` but got + # `Task[None]`. await later.cancel(task) self.assertTrue(task.done()) self.assertFalse(task.cancelled()) @@ -72,6 +76,7 @@ async def _coro() -> None: await asyncio.sleep(0) self.assertTrue(started) self.assertTrue(task.done()) + # pyre-fixme[6]: For 1st argument expected `Future[Any]` but got `Task[None]`. await later.cancel(task) async def test_cancel_task_completes(self) -> None: @@ -93,6 +98,8 @@ async def _coro() -> int | None: # BaseException)]], typing.Tuple[Type[Variable[_E (bound to BaseException)]], # ...]]` but got `Type[InvalidStateError]`. with self.assertRaises(asyncio.InvalidStateError): + # pyre-fixme[6]: For 1st argument expected `Future[Any]` but got + # `Task[Optional[int]]`. await later.cancel(task) self.assertTrue(task.done()) self.assertFalse(task.cancelled()) @@ -116,6 +123,7 @@ async def test() -> None: otask = test() # task created a scheduled. await asyncio.sleep(0) # let test start self.assertTrue(started) + # pyre-fixme[6]: For 1st argument expected `Future[Any]` but got `Task[None]`. ctask = cast(asyncio.Task, cancel_as_task(otask)) await asyncio.sleep(0) # let the cancel as task start self.assertFalse(otask.cancelled()) @@ -210,6 +218,7 @@ async def coro() -> None: watcher.watch(task) loop.call_later(0.2, watcher.cancel) # insure the task isn't still pending so we don't fail the later TestCase checks + # pyre-fixme[6]: For 1st argument expected `Future[Any]` but got `Task[None]`. await later.cancel(task) async def test_watcher_cancel(self) -> None: @@ -392,6 +401,7 @@ async def fun(event): self.assertEqual(called, 1) self.assertFalse(original_cancelled) + # pyre-fixme[6]: For 1st argument expected `Future[Any]` but got `Task[Any]`. await later.cancel(call1) self.assertFalse(original_cancelled) @@ -401,6 +411,7 @@ async def fun(event): # Only after there is only one pending left do we allow the original task # to be cancelled. + # pyre-fixme[6]: For 1st argument expected `Future[Any]` but got `Task[Any]`. await later.cancel(call2) self.assertTrue(original_cancelled)