diff --git a/requirements_tests.txt b/requirements_tests.txt index d4c5c693..ad169394 100644 --- a/requirements_tests.txt +++ b/requirements_tests.txt @@ -6,6 +6,6 @@ # ----------------------------------------------------------------------------- # Packages to run source tests # ----------------------------------------------------------------------------- -packaging == 24.0 -pytest == 7.4.4 +packaging == 23.2 +pytest == 8.1.1 pytest-asyncio == 0.23.6 diff --git a/src/HABApp/__version__.py b/src/HABApp/__version__.py index 5860aae4..7c772750 100644 --- a/src/HABApp/__version__.py +++ b/src/HABApp/__version__.py @@ -10,4 +10,4 @@ # Development versions contain the DEV-COUNTER postfix: # - 24.01.0.DEV-1 -__version__ = '24.04.1.DEV-1' +__version__ = '24.04.0.DEV-1' diff --git a/src/HABApp/core/internals/wrapped_function/wrapped_thread.py b/src/HABApp/core/internals/wrapped_function/wrapped_thread.py index c133da11..c2543cb5 100644 --- a/src/HABApp/core/internals/wrapped_function/wrapped_thread.py +++ b/src/HABApp/core/internals/wrapped_function/wrapped_thread.py @@ -1,18 +1,18 @@ import io import logging -from cProfile import Profile from concurrent.futures import ThreadPoolExecutor -from pstats import SortKey -from pstats import Stats +from cProfile import Profile +from pstats import SortKey, Stats from threading import Lock from time import monotonic -from typing import Callable, Any, Set, Final, Dict, Tuple -from typing import Optional +from typing import Any, Callable, Dict, Final, Optional, Set, Tuple from HABApp.core.const import loop from HABApp.core.internals import Context, ContextProvidingObj + from .base import WrappedFunctionBase, default_logger + POOL: Optional[ThreadPoolExecutor] = None POOL_THREADS: int = 0 POOL_INFO: Set['PoolFunc'] = set() diff --git a/src/HABApp/core/internals/wrapped_function/wrapper.py b/src/HABApp/core/internals/wrapped_function/wrapper.py index 60edb762..6a758cea 100644 --- a/src/HABApp/core/internals/wrapped_function/wrapper.py +++ b/src/HABApp/core/internals/wrapped_function/wrapper.py @@ -1,14 +1,19 @@ import logging from asyncio import iscoroutinefunction -from typing import Union, Optional, Callable, Type +from typing import Callable, Optional, Type, Union 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, TYPE_FUNC_ASYNC +from HABApp.core.internals.wrapped_function.wrapped_async import TYPE_FUNC_ASYNC, WrappedAsyncFunction 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 +from HABApp.core.internals.wrapped_function.wrapped_thread import ( + HINT_FUNC_SYNC, + WrappedThreadFunction, + create_thread_pool, + run_in_thread_pool, + stop_thread_pool, +) def wrap_func(func: Union[HINT_FUNC_SYNC, TYPE_FUNC_ASYNC], @@ -25,7 +30,9 @@ def wrap_func(func: Union[HINT_FUNC_SYNC, TYPE_FUNC_ASYNC], type_name: str = func.__class__.__name__ except Exception: type_name = type(func) - raise ValueError(f'Callable or coroutine function expected! Got "{func}" (type {type_name:s})') + + msg = f'Callable or coroutine function expected! Got "{func}" (type {type_name:s})' + raise TypeError(msg) if iscoroutinefunction(func): return WrappedAsyncFunction(func, name=name, logger=logger, context=context) diff --git a/tests/conftest.py b/tests/conftest.py index 5414a6c2..c7fa3705 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -34,14 +34,14 @@ def f(*args, **kwargs): return f -@pytest.fixture(autouse=True, scope='function') +@pytest.fixture(autouse=True) def show_errors(monkeypatch): # Patch the wrapper so that we always raise the exception monkeypatch.setattr(HABApp.core.wrapper, 'ignore_exception', raise_err) monkeypatch.setattr(HABApp.core.wrapper, 'log_exception', raise_err) -@pytest.fixture(autouse=True, scope='function') +@pytest.fixture(autouse=True) def use_dummy_cfg(monkeypatch): cfg = get_dummy_cfg() monkeypatch.setattr(HABApp, 'CONFIG', cfg) @@ -59,12 +59,12 @@ def event_loop(): async_context.reset(token) -@pytest.fixture(scope='function') +@pytest.fixture() def ir(): return ItemRegistry() -@pytest.fixture(autouse=True, scope='function') +@pytest.fixture(autouse=True) def clean_objs(ir: ItemRegistry, eb: EventBus, request): markers = request.node.own_markers for marker in markers: @@ -83,7 +83,7 @@ def clean_objs(ir: ItemRegistry, eb: EventBus, request): r.restore() -@pytest.fixture(autouse=True, scope='function') +@pytest.fixture(autouse=True) def test_logs(caplog, request): caplog.set_level(logging.DEBUG) diff --git a/tests/helpers/sync_worker.py b/tests/helpers/sync_worker.py index e29e356b..9a849cd5 100644 --- a/tests/helpers/sync_worker.py +++ b/tests/helpers/sync_worker.py @@ -1,6 +1,7 @@ import pytest from HABApp.core.internals.wrapped_function import wrapped_thread +from HABApp.core.internals.wrapped_function import wrapper as wrapper_module class SyncTestWorker: @@ -9,7 +10,9 @@ def submit(callback, *args, **kwargs): callback(*args, **kwargs) -@pytest.fixture(scope="function") +@pytest.fixture() def sync_worker(monkeypatch): monkeypatch.setattr(wrapped_thread, 'POOL', SyncTestWorker()) - yield + + assert not hasattr(wrapper_module, 'SYNC_CLS') + monkeypatch.setattr(wrapper_module, 'SYNC_CLS', wrapper_module.WrappedThreadFunction, raising=False) diff --git a/tests/rule_runner/rule_runner.py b/tests/rule_runner/rule_runner.py index dd54f8bf..1fe206b9 100644 --- a/tests/rule_runner/rule_runner.py +++ b/tests/rule_runner/rule_runner.py @@ -7,7 +7,7 @@ import HABApp.rule.rule as rule_module import HABApp.rule.scheduler.habappschedulerview as ha_sched from HABApp.core.asyncio import async_context -from HABApp.core.internals import setup_internals, ItemRegistry, EventBus +from HABApp.core.internals import EventBus, ItemRegistry, setup_internals from HABApp.core.internals.proxy import ConstProxyObj from HABApp.core.internals.wrapped_function import wrapped_thread, wrapper from HABApp.core.internals.wrapped_function.wrapped_thread import WrappedThreadFunction diff --git a/tests/test_core/test_wrapped_func.py b/tests/test_core/test_wrapped_func.py index 7c6ba1a2..4e2e4dec 100644 --- a/tests/test_core/test_wrapped_func.py +++ b/tests/test_core/test_wrapped_func.py @@ -12,15 +12,15 @@ def test_error(): - with pytest.raises(ValueError) as e: + with pytest.raises(TypeError) as e: wrap_func(None) assert str(e.value) == 'Callable or coroutine function expected! Got "None" (type NoneType)' - with pytest.raises(ValueError) as e: + with pytest.raises(TypeError) as e: wrap_func(6) assert str(e.value) == 'Callable or coroutine function expected! Got "6" (type int)' - with pytest.raises(ValueError) as e: + with pytest.raises(TypeError) as e: wrap_func(date(2023, 12, 24)) assert str(e.value) == 'Callable or coroutine function expected! Got "2023-12-24" (type date)' diff --git a/tests/test_docs.py b/tests/test_docs.py index 7831197e..3fa30520 100644 --- a/tests/test_docs.py +++ b/tests/test_docs.py @@ -1,11 +1,11 @@ from inspect import getmembers, isclass from pathlib import Path +from easyconfig import yaml from pydantic import BaseModel import HABApp.config.models from HABApp.config import CONFIG -from easyconfig import yaml def test_sample_yaml(pytestconfig): diff --git a/tests/test_packages.py b/tests/test_packages.py index c898274e..dfe545ca 100644 --- a/tests/test_packages.py +++ b/tests/test_packages.py @@ -1,6 +1,8 @@ import re from pathlib import Path + from packaging.version import VERSION_PATTERN + import HABApp.__check_dependency_packages__ from HABApp import __version__