1
0
mirror of https://github.com/home-assistant/core.git synced 2025-12-24 21:06:19 +00:00

Add typing to homeassistant/*.py and homeassistant/util/ (#15569)

* Add typing to homeassistant/*.py and homeassistant/util/

* Fix wrong merge

* Restore iterable in OrderedSet

* Fix tests
This commit is contained in:
Andrey
2018-07-23 11:24:39 +03:00
committed by Paulus Schoutsen
parent b7c336a687
commit 140a874917
27 changed files with 532 additions and 384 deletions

View File

@@ -3,22 +3,25 @@ import concurrent.futures
import threading
import logging
from asyncio import coroutines
from asyncio.events import AbstractEventLoop
from asyncio.futures import Future
from asyncio import ensure_future
from typing import Any, Union, Coroutine, Callable, Generator
_LOGGER = logging.getLogger(__name__)
def _set_result_unless_cancelled(fut, result):
def _set_result_unless_cancelled(fut: Future, result: Any) -> None:
"""Set the result only if the Future was not cancelled."""
if fut.cancelled():
return
fut.set_result(result)
def _set_concurrent_future_state(concurr, source):
def _set_concurrent_future_state(
concurr: concurrent.futures.Future,
source: Union[concurrent.futures.Future, Future]) -> None:
"""Copy state from a future to a concurrent.futures.Future."""
assert source.done()
if source.cancelled():
@@ -33,7 +36,8 @@ def _set_concurrent_future_state(concurr, source):
concurr.set_result(result)
def _copy_future_state(source, dest):
def _copy_future_state(source: Union[concurrent.futures.Future, Future],
dest: Union[concurrent.futures.Future, Future]) -> None:
"""Copy state from another Future.
The other Future may be a concurrent.futures.Future.
@@ -53,7 +57,9 @@ def _copy_future_state(source, dest):
dest.set_result(result)
def _chain_future(source, destination):
def _chain_future(
source: Union[concurrent.futures.Future, Future],
destination: Union[concurrent.futures.Future, Future]) -> None:
"""Chain two futures so that when one completes, so does the other.
The result (or exception) of source will be copied to destination.
@@ -74,20 +80,23 @@ def _chain_future(source, destination):
else:
dest_loop = None
def _set_state(future, other):
def _set_state(future: Union[concurrent.futures.Future, Future],
other: Union[concurrent.futures.Future, Future]) -> None:
if isinstance(future, Future):
_copy_future_state(other, future)
else:
_set_concurrent_future_state(future, other)
def _call_check_cancel(destination):
def _call_check_cancel(
destination: Union[concurrent.futures.Future, Future]) -> None:
if destination.cancelled():
if source_loop is None or source_loop is dest_loop:
source.cancel()
else:
source_loop.call_soon_threadsafe(source.cancel)
def _call_set_state(source):
def _call_set_state(
source: Union[concurrent.futures.Future, Future]) -> None:
if dest_loop is None or dest_loop is source_loop:
_set_state(destination, source)
else:
@@ -97,7 +106,9 @@ def _chain_future(source, destination):
source.add_done_callback(_call_set_state)
def run_coroutine_threadsafe(coro, loop):
def run_coroutine_threadsafe(
coro: Union[Coroutine, Generator],
loop: AbstractEventLoop) -> concurrent.futures.Future:
"""Submit a coroutine object to a given event loop.
Return a concurrent.futures.Future to access the result.
@@ -110,7 +121,7 @@ def run_coroutine_threadsafe(coro, loop):
raise TypeError('A coroutine object is required')
future = concurrent.futures.Future() # type: concurrent.futures.Future
def callback():
def callback() -> None:
"""Handle the call to the coroutine."""
try:
_chain_future(ensure_future(coro, loop=loop), future)
@@ -125,7 +136,8 @@ def run_coroutine_threadsafe(coro, loop):
return future
def fire_coroutine_threadsafe(coro, loop):
def fire_coroutine_threadsafe(coro: Coroutine,
loop: AbstractEventLoop) -> None:
"""Submit a coroutine object to a given event loop.
This method does not provide a way to retrieve the result and
@@ -139,7 +151,7 @@ def fire_coroutine_threadsafe(coro, loop):
if not coroutines.iscoroutine(coro):
raise TypeError('A coroutine object is required: %s' % coro)
def callback():
def callback() -> None:
"""Handle the firing of a coroutine."""
ensure_future(coro, loop=loop)
@@ -147,7 +159,8 @@ def fire_coroutine_threadsafe(coro, loop):
return
def run_callback_threadsafe(loop, callback, *args):
def run_callback_threadsafe(loop: AbstractEventLoop, callback: Callable,
*args: Any) -> concurrent.futures.Future:
"""Submit a callback object to a given event loop.
Return a concurrent.futures.Future to access the result.
@@ -158,7 +171,7 @@ def run_callback_threadsafe(loop, callback, *args):
future = concurrent.futures.Future() # type: concurrent.futures.Future
def run_callback():
def run_callback() -> None:
"""Run callback and store result."""
try:
future.set_result(callback(*args))