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:
Ville Skyttä 2021-01-30 23:33:53 +02:00 committed by GitHub
parent e43cee163f
commit d13b58a4e6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 27 additions and 11 deletions

View File

@ -205,7 +205,7 @@ class CoreState(enum.Enum):
def __str__(self) -> str: # pylint: disable=invalid-str-returned def __str__(self) -> str: # pylint: disable=invalid-str-returned
"""Return the event.""" """Return the event."""
return self.value # type: ignore return self.value
class HomeAssistant: class HomeAssistant:
@ -584,7 +584,7 @@ class EventOrigin(enum.Enum):
def __str__(self) -> str: # pylint: disable=invalid-str-returned def __str__(self) -> str: # pylint: disable=invalid-str-returned
"""Return the event.""" """Return the event."""
return self.value # type: ignore return self.value
class Event: class Event:

View File

@ -116,7 +116,9 @@ class TrackTemplateResult:
result: Any 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.""" """Convert an async event helper to a threaded one."""
@ft.wraps(async_factory) @ft.wraps(async_factory)

View File

@ -18,9 +18,8 @@ def has_location(state: State) -> bool:
Async friendly. Async friendly.
""" """
# type ignore: https://github.com/python/mypy/issues/7207
return ( return (
isinstance(state, State) # type: ignore isinstance(state, State)
and isinstance(state.attributes.get(ATTR_LATITUDE), float) and isinstance(state.attributes.get(ATTR_LATITUDE), float)
and isinstance(state.attributes.get(ATTR_LONGITUDE), float) and isinstance(state.attributes.get(ATTR_LONGITUDE), float)
) )

View File

@ -6,7 +6,7 @@ import logging
import logging.handlers import logging.handlers
import queue import queue
import traceback 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.const import EVENT_HOMEASSISTANT_CLOSE
from homeassistant.core import HomeAssistant, callback 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) 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( def catch_log_exception(
func: Callable[..., Any], format_err: Callable[..., Any], *args: Any 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.""" """Decorate a callback to catch and log exceptions."""
# Check for partials to properly determine if coroutine function # Check for partials to properly determine if coroutine function
@ -116,14 +130,15 @@ def catch_log_exception(
while isinstance(check_func, partial): while isinstance(check_func, partial):
check_func = check_func.func check_func = check_func.func
wrapper_func = None wrapper_func: Union[Callable[..., None], Callable[..., Awaitable[None]]]
if asyncio.iscoroutinefunction(check_func): if asyncio.iscoroutinefunction(check_func):
async_func = cast(Callable[..., Awaitable[None]], func)
@wraps(func) @wraps(async_func)
async def async_wrapper(*args: Any) -> None: async def async_wrapper(*args: Any) -> None:
"""Catch and log exception.""" """Catch and log exception."""
try: try:
await func(*args) await async_func(*args)
except Exception: # pylint: disable=broad-except except Exception: # pylint: disable=broad-except
log_exception(format_err, *args) log_exception(format_err, *args)

View File

@ -8,7 +8,7 @@ codecov==2.1.10
coverage==5.4 coverage==5.4
jsonpickle==1.4.1 jsonpickle==1.4.1
mock-open==1.4.0 mock-open==1.4.0
mypy==0.790 mypy==0.800
pre-commit==2.9.3 pre-commit==2.9.3
pylint==2.6.0 pylint==2.6.0
astroid==2.4.2 astroid==2.4.2