Skip to content

Commit

Permalink
Pre-silence Pyre Errors for upcoming upgrade] [batch:85/596] [shard:2/N]
Browse files Browse the repository at this point in the history
Reviewed By: MaggieMoss

Differential Revision: D65218362

fbshipit-source-id: 87ebd67eb1b92b346e51641a4a37d2be4178828b
  • Loading branch information
generatedunixname89002005287564 authored and facebook-github-bot committed Oct 31, 2024
1 parent add7f38 commit 0e07f9d
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
20 changes: 20 additions & 0 deletions later/task.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)


Expand Down Expand Up @@ -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.")
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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()

Expand Down Expand Up @@ -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
Expand All @@ -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:
Expand All @@ -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
Expand All @@ -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(
Expand Down Expand Up @@ -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:
Expand Down
11 changes: 11 additions & 0 deletions later/tests/test_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand All @@ -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())
Expand All @@ -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:
Expand All @@ -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())
Expand All @@ -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())
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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)

Expand All @@ -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)

Expand Down

0 comments on commit 0e07f9d

Please sign in to comment.