Skip to content

Commit

Permalink
Correct return typing for catch_log_exception (#78399)
Browse files Browse the repository at this point in the history
  • Loading branch information
jbouwh committed Sep 17, 2022
1 parent 5a6a474 commit 35221e9
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions homeassistant/util/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,27 +120,30 @@ def log_exception(format_err: Callable[..., Any], *args: Any) -> None:
def catch_log_exception(
func: Callable[..., Coroutine[Any, Any, Any]], format_err: Callable[..., Any]
) -> Callable[..., Coroutine[Any, Any, None]]:
"""Overload for Callables that return a Coroutine."""
"""Overload for Coroutine that returns a Coroutine."""


@overload
def catch_log_exception(
func: Callable[..., Any], format_err: Callable[..., Any]
) -> Callable[..., None | Coroutine[Any, Any, None]]:
"""Overload for Callables that return Any."""
) -> Callable[..., None] | Callable[..., Coroutine[Any, Any, None]]:
"""Overload for a callback that returns a callback."""


def catch_log_exception(
func: Callable[..., Any], format_err: Callable[..., Any]
) -> Callable[..., None | Coroutine[Any, Any, None]]:
"""Decorate a callback to catch and log exceptions."""
) -> Callable[..., None] | Callable[..., Coroutine[Any, Any, None]]:
"""Decorate a function func to catch and log exceptions.
If func is a coroutine function, a coroutine function will be returned.
If func is a callback, a callback will be returned.
"""
# Check for partials to properly determine if coroutine function
check_func = func
while isinstance(check_func, partial):
check_func = check_func.func

wrapper_func: Callable[..., None | Coroutine[Any, Any, None]]
wrapper_func: Callable[..., None] | Callable[..., Coroutine[Any, Any, None]]
if asyncio.iscoroutinefunction(check_func):
async_func = cast(Callable[..., Coroutine[Any, Any, None]], func)

Expand Down

0 comments on commit 35221e9

Please sign in to comment.