Skip to content

Commit

Permalink
Add some type hints
Browse files Browse the repository at this point in the history
  • Loading branch information
Josef-Friedrich committed Mar 2, 2024
1 parent 51deb11 commit cabfa76
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 12 deletions.
8 changes: 4 additions & 4 deletions check_systemd.py
Original file line number Diff line number Diff line change
Expand Up @@ -895,15 +895,15 @@ def count_by_states(
states: Sequence[str],
include: str | Sequence[str] | None = None,
exclude: str | Sequence[str] | None = None,
) -> dict:
states_normalized = []
counter = {}
) -> dict[str, int]:
states_normalized: list[dict[str, str]] = []
counter: dict[str, int] = {}
for state_spec in states:
# state_proerty:state_value
# for example: active_state:failed
state_property = state_spec.split(":")[0]
state_value = state_spec.split(":")[1]
state = {
state: dict[str, str] = {
"property": state_property,
"value": state_value,
"spec": state_spec,
Expand Down
26 changes: 20 additions & 6 deletions tests/test_data_acquisition_units.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
"""Tests related to data acquisition of systemd units."""

from __future__ import annotations

from typing import Sequence

from check_systemd import Unit, UnitCache, UnitNameFilter

Expand Down Expand Up @@ -48,6 +51,8 @@ def test_initialization(self) -> None:


class TestClassUnitCache:
unit_cache: UnitCache

def setup_method(self) -> None:
self.unit_cache = UnitCache()
self.unit_cache.add_unit(unit_modem_manager)
Expand All @@ -60,8 +65,12 @@ def setup_method(self) -> None:
self.unit_cache.add_unit(unit_nmdb)
self.unit_cache.add_unit(unit_php)

def list(self, include=None, exclude=None):
units = []
def list(
self,
include: str | Sequence[str] | None = None,
exclude: str | Sequence[str] | None = None,
) -> list[str]:
units: list[str] = []
for unit in self.unit_cache.list(include=include, exclude=exclude):
units.append(unit.name)
return units
Expand All @@ -71,14 +80,15 @@ def test_method_add_with_kwargs(self) -> None:
unit = self.unit_cache.add_unit(
name="test.service",
active_state="active",
sub_state="sub",
load_state="load",
sub_state="running",
load_state="loaded",
)
assert unit.name == "test.service"
assert 9 == self.unit_cache.count

def test_method_get(self) -> None:
unit = self.unit_cache.get(name="ModemManager.service")
assert unit
assert "ModemManager.service" == unit.name

def test_method_list(self) -> None:
Expand Down Expand Up @@ -130,8 +140,12 @@ def setup_method(self) -> None:
self.filter.add("nmbd.timer")
self.filter.add("php7.4-fpm.service")

def list(self, include=None, exclude=None):
unit_names = []
def list(
self,
include: str | Sequence[str] | None = None,
exclude: str | Sequence[str] | None = None,
):
unit_names: list[str] = []
for unit_name in self.filter.list(include=include, exclude=exclude):
unit_names.append(unit_name)
return unit_names
Expand Down
4 changes: 2 additions & 2 deletions tests/test_option_exclude.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from tests.helper import execute_main
from tests.helper import MockResult, execute_main


def execute_with_opt_e(argv, unit_suffix="failed"):
def execute_with_opt_e(argv: list[str], unit_suffix: str = "failed") -> MockResult:
return execute_main(
argv=argv,
stdout=[
Expand Down

0 comments on commit cabfa76

Please sign in to comment.