From c53ff896a507daa02a989f84f199487d73fb466d Mon Sep 17 00:00:00 2001 From: spacemanspiff2007 <10754716+spacemanspiff2007@users.noreply.github.com> Date: Wed, 15 Nov 2023 07:37:52 +0100 Subject: [PATCH] bump --- .ruff.toml | 5 +++++ src/HABApp/core/const/const.py | 1 + src/HABApp/core/const/hints.py | 8 +++++--- src/HABApp/core/events/filter/event.py | 8 ++++---- .../internals/item_registry/item_registry.py | 17 +++++++++-------- .../wrapped_function/wrapped_async.py | 4 ++-- .../internals/wrapped_function/wrapper.py | 4 ++-- src/HABApp/core/items/base_item.py | 19 +++++++++++++------ src/HABApp/core/items/base_item_watch.py | 16 +++++++++++----- src/HABApp/rule/rule.py | 4 ++-- tests/test_core/test_wrapped_func.py | 8 +++----- 11 files changed, 57 insertions(+), 37 deletions(-) diff --git a/.ruff.toml b/.ruff.toml index 9ae65c5e..62b8a2f8 100644 --- a/.ruff.toml +++ b/.ruff.toml @@ -60,3 +60,8 @@ builtins-ignorelist = ["id", "input"] [lint.per-file-ignores] "docs/conf.py" = ["INP001", "A001"] "setup.py" = ["PTH123"] + + +[lint.isort] +# https://docs.astral.sh/ruff/settings/#isort-lines-after-imports +lines-after-imports = 2 diff --git a/src/HABApp/core/const/const.py b/src/HABApp/core/const/const.py index 1d90db0d..be57b4e2 100644 --- a/src/HABApp/core/const/const.py +++ b/src/HABApp/core/const/const.py @@ -20,6 +20,7 @@ def __repr__(self): PYTHON_310: Final = sys.version_info >= (3, 10) PYTHON_311: Final = sys.version_info >= (3, 11) PYTHON_312: Final = sys.version_info >= (3, 12) +PYTHON_313: Final = sys.version_info >= (3, 13) # In python 3.11 there were changes to MyEnum(str, Enum), so we have to use the StrEnum diff --git a/src/HABApp/core/const/hints.py b/src/HABApp/core/const/hints.py index 4b6e9c0e..9fe059e1 100644 --- a/src/HABApp/core/const/hints.py +++ b/src/HABApp/core/const/hints.py @@ -5,12 +5,14 @@ from .const import PYTHON_310 as __IS_GE_PYTHON_310 + if __IS_GE_PYTHON_310: from typing import TypeAlias else: from typing import Final as TypeAlias -HINT_ANY_CLASS: TypeAlias = __Type[object] -HINT_FUNC_ASYNC: TypeAlias = __Callable[..., __Awaitable[__Any]] -HINT_EVENT_CALLBACK: TypeAlias = __Callable[[__Any], __Any] +TYPE_ANY_CLASS_TYPE: TypeAlias = __Type[object] +TYPE_FUNC_ASYNC: TypeAlias = __Callable[..., __Awaitable[__Any]] + +TYPE_EVENT_CALLBACK: TypeAlias = __Callable[[__Any], __Any] diff --git a/src/HABApp/core/events/filter/event.py b/src/HABApp/core/events/filter/event.py index 60c9b379..1bfb57f5 100644 --- a/src/HABApp/core/events/filter/event.py +++ b/src/HABApp/core/events/filter/event.py @@ -1,16 +1,16 @@ -from typing import Optional, Final -from typing import get_type_hints as _get_type_hints from inspect import isclass +from typing import Final, Optional +from typing import get_type_hints as _get_type_hints from HABApp.core.const import MISSING -from HABApp.core.const.hints import HINT_ANY_CLASS +from HABApp.core.const.hints import TYPE_ANY_CLASS_TYPE from HABApp.core.internals import EventFilterBase class EventFilter(EventFilterBase): """Triggers on event types and optionally on their values, too""" - def __init__(self, event_class: HINT_ANY_CLASS, **kwargs): + def __init__(self, event_class: TYPE_ANY_CLASS_TYPE, **kwargs): assert len(kwargs) < 3, 'EventFilter only allows up to two args that will be used to filter' assert isclass(event_class), f'Class for event required! Passed {event_class} ({type(event_class)})' diff --git a/src/HABApp/core/internals/item_registry/item_registry.py b/src/HABApp/core/internals/item_registry/item_registry.py index 88082886..9fa72f54 100644 --- a/src/HABApp/core/internals/item_registry/item_registry.py +++ b/src/HABApp/core/internals/item_registry/item_registry.py @@ -1,9 +1,10 @@ +from __future__ import annotations + import logging import threading -from typing import Dict -from typing import Tuple, Union, TypeVar +from typing import TypeVar -from HABApp.core.errors import ItemNotFoundException, ItemAlreadyExistsError +from HABApp.core.errors import ItemAlreadyExistsError, ItemNotFoundException from HABApp.core.internals.item_registry import ItemRegistryItem @@ -15,9 +16,9 @@ class ItemRegistry: def __init__(self): self._lock = threading.Lock() - self._items: Dict[str, ItemRegistryItem] = {} + self._items: dict[str, ItemRegistryItem] = {} - def item_exists(self, name: Union[str, ItemRegistryItem]) -> bool: + def item_exists(self, name: str | ItemRegistryItem) -> bool: if not isinstance(name, str): name = name.name return name in self._items @@ -28,10 +29,10 @@ def get_item(self, name: str) -> ItemRegistryItem: except KeyError: raise ItemNotFoundException(name) from None - def get_items(self) -> Tuple[ItemRegistryItem, ...]: + def get_items(self) -> tuple[ItemRegistryItem, ...]: return tuple(self._items.values()) - def get_item_names(self) -> Tuple[str, ...]: + def get_item_names(self) -> tuple[str, ...]: return tuple(self._items.keys()) def add_item(self, item: _HINT_ITEM_OBJ) -> _HINT_ITEM_OBJ: @@ -54,7 +55,7 @@ def add_item(self, item: _HINT_ITEM_OBJ) -> _HINT_ITEM_OBJ: item._on_item_added() return item - def pop_item(self, name: Union[str, _HINT_ITEM_OBJ]) -> _HINT_ITEM_OBJ: + def pop_item(self, name: str | _HINT_ITEM_OBJ) -> _HINT_ITEM_OBJ: if not isinstance(name, str): name = name.name diff --git a/src/HABApp/core/internals/wrapped_function/wrapped_async.py b/src/HABApp/core/internals/wrapped_function/wrapped_async.py index e3b3916f..516ee393 100644 --- a/src/HABApp/core/internals/wrapped_function/wrapped_async.py +++ b/src/HABApp/core/internals/wrapped_function/wrapped_async.py @@ -2,7 +2,7 @@ from typing import Optional from HABApp.core.asyncio import async_context, create_task -from HABApp.core.const.hints import HINT_FUNC_ASYNC +from HABApp.core.const.hints import TYPE_FUNC_ASYNC from HABApp.core.internals import Context from .base import WrappedFunctionBase @@ -10,7 +10,7 @@ class WrappedAsyncFunction(WrappedFunctionBase): - def __init__(self, func: HINT_FUNC_ASYNC, + def __init__(self, func: TYPE_FUNC_ASYNC, name: Optional[str] = None, logger: Optional[logging.Logger] = None, context: Optional[Context] = None): diff --git a/src/HABApp/core/internals/wrapped_function/wrapper.py b/src/HABApp/core/internals/wrapped_function/wrapper.py index 295445d0..60edb762 100644 --- a/src/HABApp/core/internals/wrapped_function/wrapper.py +++ b/src/HABApp/core/internals/wrapped_function/wrapper.py @@ -5,13 +5,13 @@ from HABApp.config import CONFIG from HABApp.core.internals import Context from HABApp.core.internals.wrapped_function.base import TYPE_WRAPPED_FUNC_OBJ -from HABApp.core.internals.wrapped_function.wrapped_async import WrappedAsyncFunction, HINT_FUNC_ASYNC +from HABApp.core.internals.wrapped_function.wrapped_async import WrappedAsyncFunction, TYPE_FUNC_ASYNC from HABApp.core.internals.wrapped_function.wrapped_sync import WrappedSyncFunction from HABApp.core.internals.wrapped_function.wrapped_thread import HINT_FUNC_SYNC, WrappedThreadFunction, \ create_thread_pool, stop_thread_pool, run_in_thread_pool -def wrap_func(func: Union[HINT_FUNC_SYNC, HINT_FUNC_ASYNC], +def wrap_func(func: Union[HINT_FUNC_SYNC, TYPE_FUNC_ASYNC], warn_too_long=True, name: Optional[str] = None, logger: Optional[logging.Logger] = None, diff --git a/src/HABApp/core/items/base_item.py b/src/HABApp/core/items/base_item.py index d1f38c10..7a0f4190 100644 --- a/src/HABApp/core/items/base_item.py +++ b/src/HABApp/core/items/base_item.py @@ -1,17 +1,24 @@ -from typing import Type, TypeVar, Optional +from typing import Optional, Type, TypeVar +from eascheduler.const import local_tz from pendulum import UTC, DateTime from pendulum import now as pd_now -from HABApp.core.internals import HINT_EVENT_FILTER_OBJ, HINT_EVENT_BUS_LISTENER -from HABApp.core.internals import uses_get_item, uses_item_registry, get_current_context +from HABApp.core.internals import ( + HINT_EVENT_BUS_LISTENER, + HINT_EVENT_FILTER_OBJ, + get_current_context, + uses_get_item, + uses_item_registry, +) from HABApp.core.internals.item_registry import ItemRegistryItem from HABApp.core.lib.parameters import TH_POSITIVE_TIME_DIFF, get_positive_time_diff -from eascheduler.const import local_tz + +from ..const.hints import TYPE_EVENT_CALLBACK from .base_item_times import ChangedTime, ItemNoChangeWatch, ItemNoUpdateWatch, UpdatedTime from .tmp_data import add_tmp_data as _add_tmp_data from .tmp_data import restore_tmp_data as _restore_tmp_data -from ..const.hints import HINT_EVENT_CALLBACK + get_item = uses_get_item() item_registry = uses_item_registry() @@ -79,7 +86,7 @@ def watch_update(self, secs: TH_POSITIVE_TIME_DIFF) -> ItemNoUpdateWatch: secs = get_positive_time_diff(secs, round_digits=1) return self._last_update.add_watch(secs) - def listen_event(self, callback: HINT_EVENT_CALLBACK, + def listen_event(self, callback: TYPE_EVENT_CALLBACK, event_filter: Optional[HINT_EVENT_FILTER_OBJ] = None) -> HINT_EVENT_BUS_LISTENER: """ Register an event listener which listens to all event that the item receives diff --git a/src/HABApp/core/items/base_item_watch.py b/src/HABApp/core/items/base_item_watch.py index 5503c9e2..ad86e79b 100644 --- a/src/HABApp/core/items/base_item_watch.py +++ b/src/HABApp/core/items/base_item_watch.py @@ -3,11 +3,17 @@ import HABApp from HABApp.core.asyncio import run_func_from_async -from HABApp.core.events import ItemNoChangeEvent, ItemNoUpdateEvent, EventFilter +from HABApp.core.const.hints import TYPE_EVENT_CALLBACK +from HABApp.core.events import EventFilter, ItemNoChangeEvent, ItemNoUpdateEvent +from HABApp.core.internals import ( + AutoContextBoundObj, + ContextBoundEventBusListener, + get_current_context, + uses_post_event, + wrap_func, +) from HABApp.core.lib import PendingFuture -from HABApp.core.const.hints import HINT_EVENT_CALLBACK -from HABApp.core.internals import uses_post_event, get_current_context, AutoContextBoundObj, wrap_func -from HABApp.core.internals import ContextBoundEventBusListener + log = logging.getLogger('HABApp') @@ -34,7 +40,7 @@ def cancel(self): self._ctx_unlink() run_func_from_async(self.__cancel_watch) - def listen_event(self, callback: HINT_EVENT_CALLBACK) -> 'HABApp.core.base.HINT_EVENT_BUS_LISTENER': + def listen_event(self, callback: TYPE_EVENT_CALLBACK) -> 'HABApp.core.base.HINT_EVENT_BUS_LISTENER': """Listen to (only) the event that is emitted by this watcher""" context = get_current_context() return context.add_event_listener( diff --git a/src/HABApp/rule/rule.py b/src/HABApp/rule/rule.py index 0b6d6161..6079a593 100644 --- a/src/HABApp/rule/rule.py +++ b/src/HABApp/rule/rule.py @@ -12,7 +12,7 @@ import HABApp.util from HABApp.core.asyncio import create_task from HABApp.core.const.const import PYTHON_310 -from HABApp.core.const.hints import HINT_EVENT_CALLBACK +from HABApp.core.const.hints import TYPE_EVENT_CALLBACK from HABApp.core.internals import ( HINT_EVENT_BUS_LISTENER, HINT_EVENT_FILTER_OBJ, @@ -123,7 +123,7 @@ def post_event(self, name: Union[HINT_ITEM_OBJ, str], event: Any): ) def listen_event(self, name: Union[HINT_ITEM_OBJ, str], - callback: HINT_EVENT_CALLBACK, + callback: TYPE_EVENT_CALLBACK, event_filter: Optional[HINT_EVENT_FILTER_OBJ] = None ) -> HINT_EVENT_BUS_LISTENER: """ diff --git a/tests/test_core/test_wrapped_func.py b/tests/test_core/test_wrapped_func.py index b65d9754..7c6ba1a2 100644 --- a/tests/test_core/test_wrapped_func.py +++ b/tests/test_core/test_wrapped_func.py @@ -1,15 +1,13 @@ import asyncio from datetime import date -from unittest.mock import AsyncMock -from unittest.mock import Mock +from unittest.mock import AsyncMock, Mock import pytest import HABApp from HABApp.core.const.topics import TOPIC_ERRORS as TOPIC_ERRORS from HABApp.core.events import NoEventFilter -from HABApp.core.internals import EventBusListener -from HABApp.core.internals import wrap_func +from HABApp.core.internals import EventBusListener, wrap_func from tests.helpers import TestEventBus @@ -66,7 +64,7 @@ async def async_func_div_error(): 1 / 0 -@pytest.mark.ignore_log_errors +@pytest.mark.ignore_log_errors() @pytest.mark.parametrize( 'func, name', ((func_div_error, 'func_div_error'), (async_func_div_error, 'async_func_div_error'))) async def test_async_error_wrapper(eb: TestEventBus, name, func, sync_worker):