From d4ab1ae8ee92f27ed9e75c2b475d70665becc678 Mon Sep 17 00:00:00 2001 From: Timotheus Bachinger Date: Thu, 20 Jun 2024 21:24:01 +0200 Subject: [PATCH] Improve typing on check_temperature * this function is not considered an official API function * however, it is used all over the place * it has the big issue that callers can pass a "bad" combination of arguments, causing the function raise during run-time only * now mypy will scream during type checking * and remove a test covered now by the typing Change-Id: Ibcacfdf97c91a436b2267020c42de730af423635 --- cmk/plugins/lib/temperature.py | 32 ++++++++++++++++++- .../unit/cmk/plugins/lib/test_temperature.py | 21 ------------ 2 files changed, 31 insertions(+), 22 deletions(-) diff --git a/cmk/plugins/lib/temperature.py b/cmk/plugins/lib/temperature.py index 58788ae7f35..2aac0bf06af 100644 --- a/cmk/plugins/lib/temperature.py +++ b/cmk/plugins/lib/temperature.py @@ -7,7 +7,7 @@ import math import time from collections.abc import Generator, Iterator, MutableMapping, Sequence -from typing import Any, TypedDict +from typing import Any, overload, TypedDict from cmk.agent_based.v1 import check_levels from cmk.agent_based.v2 import CheckResult, get_average, get_rate, Metric, Result, State @@ -261,6 +261,36 @@ def __next__(self) -> Result | Metric: return next(self._iter) +@overload +def check_temperature( + reading: float, + params: TempParamType, + *, + unique_name: str, + value_store: MutableMapping[str, Any], + dev_unit: str | None = "c", + dev_levels: tuple[float, float] | None = None, + dev_levels_lower: tuple[float, float] | None = None, + dev_status: StatusType | None = None, + dev_status_name: str | None = None, +) -> TemperatureResult: ... + + +@overload +def check_temperature( + reading: float, + params: TempParamType, + *, + unique_name: None = None, + value_store: None = None, + dev_unit: str | None = "c", + dev_levels: tuple[float, float] | None = None, + dev_levels_lower: tuple[float, float] | None = None, + dev_status: StatusType | None = None, + dev_status_name: str | None = None, +) -> TemperatureResult: ... + + def check_temperature( # pylint: disable=too-many-branches reading: float, params: TempParamType, diff --git a/tests/unit/cmk/plugins/lib/test_temperature.py b/tests/unit/cmk/plugins/lib/test_temperature.py index 99a7fe0dde2..c0002659f73 100644 --- a/tests/unit/cmk/plugins/lib/test_temperature.py +++ b/tests/unit/cmk/plugins/lib/test_temperature.py @@ -950,24 +950,3 @@ def test_check_temperature_ignores_trend_computation() -> None: notice="Configuration: prefer user levels over device levels (no levels found)", ), ] - - -@pytest.mark.parametrize( - "unique_name, value_store", - [ - (None, mock_value_store()), - ("unique_name", None), - ], -) -def test_check_temperature_either_unique_name_or_value_store( - unique_name: str | None, value_store: MutableMapping[str, Any] | None -) -> None: - with pytest.raises(ValueError): - list( - temperature.check_temperature( - 20.0, - {}, - unique_name=unique_name, - value_store=value_store, - ) - )