Skip to content

Commit

Permalink
Improve typing on check_temperature
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
TimotheusBachinger committed Jun 21, 2024
1 parent eb83897 commit d4ab1ae
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 22 deletions.
32 changes: 31 additions & 1 deletion cmk/plugins/lib/temperature.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down
21 changes: 0 additions & 21 deletions tests/unit/cmk/plugins/lib/test_temperature.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
)

0 comments on commit d4ab1ae

Please sign in to comment.