<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 async def cancel(self, task: asyncio.Task, wait_for: bool = True) -> Any: """ Cancels and waits for an `asyncio.Task` to finish. Removes it from the collection of managed tasks. Args: task (asyncio.Task):
 The to be cancelled task. It is not required that the task was was created with `TaskScheduler.create_task()`. wait_for (bool, optional):
 Whether to wait for the task to finish execution. If falsey, this function returns immediately after cancelling the task. Returns: Any: The return value of `task`. None if `wait_for` is falsey. """

        if task is None:
            return

        task.cancel()

        with suppress(KeyError):
            self._tasks.remove(task)

        with suppress(Exception):
            return (await task) if wait_for else None