Skip to content

Commit

Permalink
bump
Browse files Browse the repository at this point in the history
  • Loading branch information
spacemanspiff2007 committed Nov 15, 2023
1 parent 3c2c8b8 commit c53ff89
Show file tree
Hide file tree
Showing 11 changed files with 57 additions and 37 deletions.
5 changes: 5 additions & 0 deletions .ruff.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions src/HABApp/core/const/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 5 additions & 3 deletions src/HABApp/core/const/hints.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
8 changes: 4 additions & 4 deletions src/HABApp/core/events/filter/event.py
Original file line number Diff line number Diff line change
@@ -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)})'

Expand Down
17 changes: 9 additions & 8 deletions src/HABApp/core/internals/item_registry/item_registry.py
Original file line number Diff line number Diff line change
@@ -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


Expand All @@ -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
Expand All @@ -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:
Expand All @@ -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

Expand Down
4 changes: 2 additions & 2 deletions src/HABApp/core/internals/wrapped_function/wrapped_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
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


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):
Expand Down
4 changes: 2 additions & 2 deletions src/HABApp/core/internals/wrapped_function/wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
19 changes: 13 additions & 6 deletions src/HABApp/core/items/base_item.py
Original file line number Diff line number Diff line change
@@ -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()
Expand Down Expand Up @@ -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
Expand Down
16 changes: 11 additions & 5 deletions src/HABApp/core/items/base_item_watch.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand All @@ -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(
Expand Down
4 changes: 2 additions & 2 deletions src/HABApp/rule/rule.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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:
"""
Expand Down
8 changes: 3 additions & 5 deletions tests/test_core/test_wrapped_func.py
Original file line number Diff line number Diff line change
@@ -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


Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit c53ff89

Please sign in to comment.