mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 03:07:37 +00:00
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 <marhje52@gmail.com>
This commit is contained in:
parent
e43cee163f
commit
d13b58a4e6
@ -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:
|
||||
|
@ -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)
|
||||
|
@ -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)
|
||||
)
|
||||
|
@ -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)
|
||||
|
||||
|
@ -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
|
||||
|
Loading…
x
Reference in New Issue
Block a user