From d13b58a4e60cfd59a29047dfd6284f0a223680f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ville=20Skytt=C3=A4?= Date: Sat, 30 Jan 2021 23:33:53 +0200 Subject: [PATCH] Upgrade mypy to 0.800 (#45485) * Upgrade mypy to 0.800 https://mypy-lang.blogspot.com/2021/01/mypy-0800-released.html * Fix issues flagged by mypy 0.800 * Add overloads + small changes * Apply grammar Co-authored-by: Marc Mueller <30130371+cdce8p@users.noreply.github.com> Co-authored-by: Martin Hjelmare --- homeassistant/core.py | 4 ++-- homeassistant/helpers/event.py | 4 +++- homeassistant/helpers/location.py | 3 +-- homeassistant/util/logging.py | 25 ++++++++++++++++++++----- requirements_test.txt | 2 +- 5 files changed, 27 insertions(+), 11 deletions(-) diff --git a/homeassistant/core.py b/homeassistant/core.py index 58d9d1e6754..6d187225685 100644 --- a/homeassistant/core.py +++ b/homeassistant/core.py @@ -205,7 +205,7 @@ class CoreState(enum.Enum): def __str__(self) -> str: # pylint: disable=invalid-str-returned """Return the event.""" - return self.value # type: ignore + return self.value class HomeAssistant: @@ -584,7 +584,7 @@ class EventOrigin(enum.Enum): def __str__(self) -> str: # pylint: disable=invalid-str-returned """Return the event.""" - return self.value # type: ignore + return self.value class Event: diff --git a/homeassistant/helpers/event.py b/homeassistant/helpers/event.py index f06ac8aca3f..da7f6cd52e8 100644 --- a/homeassistant/helpers/event.py +++ b/homeassistant/helpers/event.py @@ -116,7 +116,9 @@ class TrackTemplateResult: result: Any -def threaded_listener_factory(async_factory: Callable[..., Any]) -> CALLBACK_TYPE: +def threaded_listener_factory( + async_factory: Callable[..., Any] +) -> Callable[..., CALLBACK_TYPE]: """Convert an async event helper to a threaded one.""" @ft.wraps(async_factory) diff --git a/homeassistant/helpers/location.py b/homeassistant/helpers/location.py index bca2996dfa2..19058bc3e7f 100644 --- a/homeassistant/helpers/location.py +++ b/homeassistant/helpers/location.py @@ -18,9 +18,8 @@ def has_location(state: State) -> bool: Async friendly. """ - # type ignore: https://github.com/python/mypy/issues/7207 return ( - isinstance(state, State) # type: ignore + isinstance(state, State) and isinstance(state.attributes.get(ATTR_LATITUDE), float) and isinstance(state.attributes.get(ATTR_LONGITUDE), float) ) diff --git a/homeassistant/util/logging.py b/homeassistant/util/logging.py index feef339a200..9b04c2ab007 100644 --- a/homeassistant/util/logging.py +++ b/homeassistant/util/logging.py @@ -6,7 +6,7 @@ import logging import logging.handlers import queue import traceback -from typing import Any, Callable, Coroutine +from typing import Any, Awaitable, Callable, Coroutine, Union, cast, overload from homeassistant.const import EVENT_HOMEASSISTANT_CLOSE from homeassistant.core import HomeAssistant, callback @@ -106,9 +106,23 @@ def log_exception(format_err: Callable[..., Any], *args: Any) -> None: logging.getLogger(module_name).error("%s\n%s", friendly_msg, exc_msg) +@overload +def catch_log_exception( # type: ignore + func: Callable[..., Awaitable[Any]], format_err: Callable[..., Any], *args: Any +) -> Callable[..., Awaitable[None]]: + """Overload for Callables that return an Awaitable.""" + + +@overload def catch_log_exception( func: Callable[..., Any], format_err: Callable[..., Any], *args: Any -) -> Callable[[], None]: +) -> Callable[..., None]: + """Overload for Callables that return Any.""" + + +def catch_log_exception( + func: Callable[..., Any], format_err: Callable[..., Any], *args: Any +) -> Union[Callable[..., None], Callable[..., Awaitable[None]]]: """Decorate a callback to catch and log exceptions.""" # Check for partials to properly determine if coroutine function @@ -116,14 +130,15 @@ def catch_log_exception( while isinstance(check_func, partial): check_func = check_func.func - wrapper_func = None + wrapper_func: Union[Callable[..., None], Callable[..., Awaitable[None]]] if asyncio.iscoroutinefunction(check_func): + async_func = cast(Callable[..., Awaitable[None]], func) - @wraps(func) + @wraps(async_func) async def async_wrapper(*args: Any) -> None: """Catch and log exception.""" try: - await func(*args) + await async_func(*args) except Exception: # pylint: disable=broad-except log_exception(format_err, *args) diff --git a/requirements_test.txt b/requirements_test.txt index 8bd10f3ed61..69e66239f83 100644 --- a/requirements_test.txt +++ b/requirements_test.txt @@ -8,7 +8,7 @@ codecov==2.1.10 coverage==5.4 jsonpickle==1.4.1 mock-open==1.4.0 -mypy==0.790 +mypy==0.800 pre-commit==2.9.3 pylint==2.6.0 astroid==2.4.2