Skip to content

Commit

Permalink
chore: fix typing errors with new mypy version
Browse files Browse the repository at this point in the history
  • Loading branch information
languitar committed Feb 18, 2025
1 parent 1bb629e commit 17fdefe
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 17 deletions.
7 changes: 5 additions & 2 deletions src/autosuspend/checks/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from . import (
Activity,
Check,
CheckType,
ConfigurationError,
SevereCheckError,
TemporaryCheckError,
Expand All @@ -18,11 +19,13 @@ def raise_severe_if_command_not_found(error: subprocess.CalledProcessError) -> N
raise SevereCheckError(f"Command '{' '.join(error.cmd)}' does not exist")


class CommandMixin:
class CommandMixin(Check):
"""Mixin for configuring checks based on external commands."""

@classmethod
def create(cls, name: str, config: configparser.SectionProxy) -> Check:
def create(
cls: type[CheckType], name: str, config: configparser.SectionProxy
) -> CheckType:
try:
return cls(name, config["command"].strip()) # type: ignore
except KeyError as error:
Expand Down
14 changes: 11 additions & 3 deletions src/autosuspend/checks/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,21 @@
from contextlib import suppress
from typing import Any, TYPE_CHECKING

from . import Check, ConfigurationError, SevereCheckError, TemporaryCheckError
from . import (
Check,
CheckType,
ConfigurationError,
SevereCheckError,
TemporaryCheckError,
)


if TYPE_CHECKING:
import requests
import requests.models


class NetworkMixin:
class NetworkMixin(Check):
@staticmethod
def _ensure_credentials_consistent(args: dict[str, Any]) -> None:
if (args["username"] is None) != (args["password"] is None):
Expand All @@ -35,7 +41,9 @@ def collect_init_args(
raise ConfigurationError("Lacks " + str(error) + " config entry") from error

@classmethod
def create(cls, name: str, config: configparser.SectionProxy) -> Check:
def create(
cls: type[CheckType], name: str, config: configparser.SectionProxy
) -> CheckType:
return cls(name, **cls.collect_init_args(config)) # type: ignore

def __init__(
Expand Down
10 changes: 7 additions & 3 deletions src/autosuspend/checks/xpath.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import requests
import requests.exceptions

from . import Activity, Check, ConfigurationError, TemporaryCheckError, Wakeup
from . import Activity, CheckType, ConfigurationError, TemporaryCheckError, Wakeup
from .util import NetworkMixin


Expand All @@ -30,7 +30,9 @@ def collect_init_args(cls, config: configparser.SectionProxy) -> dict[str, Any]:
raise ConfigurationError("Lacks " + str(error) + " config entry") from error

@classmethod
def create(cls, name: str, config: configparser.SectionProxy) -> Check:
def create(
cls: type[CheckType], name: str, config: configparser.SectionProxy
) -> CheckType:
return cls(name, **cls.collect_init_args(config)) # type: ignore

def __init__(self, xpath: str, **kwargs: Any) -> None:
Expand Down Expand Up @@ -73,7 +75,9 @@ def __init__(self, name: str, **kwargs: Any) -> None:
XPathMixin.__init__(self, **kwargs)

def convert_result(
self, result: str, timestamp: datetime # noqa: ARG002
self,
result: str,
timestamp: datetime, # noqa: ARG002
) -> datetime:
return datetime.fromtimestamp(float(result), timezone.utc)

Expand Down
11 changes: 7 additions & 4 deletions tests/test_checks_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def test_it_works(self) -> None:
check: _CommandMixinSub = _CommandMixinSub.create(
"name",
section,
) # type: ignore
)
assert check._command == "narf bla"

def test_throws_if_no_command_is_configured(self) -> None:
Expand All @@ -52,7 +52,7 @@ def test_reports_activity_if_the_command_succeeds(
assert (
CommandActivity.create(
"name", config_section({"command": "foo bar"})
).check() # type: ignore
).check()

Check warning on line 55 in tests/test_checks_command.py

View check run for this annotation

CodeScene Delta Analysis / CodeScene Cloud Delta Analysis (main)

❌ New issue: Code Duplication

The module contains 2 functions with similar structure: TestCommandActivity.test_reports_activity_if_the_command_succeeds,TestCommandActivity.test_reports_no_activity_if_the_command_fails. Avoid duplicated, aka copy-pasted, code inside the module. More duplication lowers the code health.
is not None
)
mock.assert_called_once_with("foo bar", shell=True)
Expand All @@ -63,15 +63,18 @@ def test_reports_no_activity_if_the_command_fails(
mock = mocker.patch("subprocess.check_call")
mock.side_effect = subprocess.CalledProcessError(2, "foo bar")
assert (
CommandActivity.create("name", config_section({"command": "foo bar"})).check() is None # type: ignore
CommandActivity.create(
"name", config_section({"command": "foo bar"})
).check()
is None
)
mock.assert_called_once_with("foo bar", shell=True)

def test_reports_missing_commands(self) -> None:
with pytest.raises(SevereCheckError):
CommandActivity.create(
"name", config_section({"command": "thisreallydoesnotexist"})
).check() # type: ignore
).check()


class TestCommandWakeup(CheckTest):
Expand Down
4 changes: 2 additions & 2 deletions tests/test_checks_ical.py
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,7 @@ def test_create(self) -> None:
"timeout": "3",
}
),
) # type: ignore
)
assert check._url == "foobar"
assert check._username == "user"
assert check._password == "pass"
Expand All @@ -481,7 +481,7 @@ def test_create(self) -> None:
check: Calendar = Calendar.create(
"name",
section,
) # type: ignore
)
assert check._url == "url"
assert check._username == "user"
assert check._password == "pass"
Expand Down
2 changes: 1 addition & 1 deletion tests/test_checks_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ def test_it_works(self) -> None:
"timeout": "42",
}
),
) # type: ignore
)
assert check._jsonpath == parse("a.b")
assert check._url == "url"
assert check._username == "user"
Expand Down
4 changes: 2 additions & 2 deletions tests/test_checks_xpath.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ def test_create(self) -> None:
"timeout": "42",
}
),
) # type: ignore
)
assert check._xpath == "/xpath"
assert check._url == "url"
assert check._username == "user"
Expand Down Expand Up @@ -232,7 +232,7 @@ def test_create(self) -> None:
"timeout": "20",
}
),
) # type: ignore
)
assert check._xpath == "/valid"


Expand Down

0 comments on commit 17fdefe

Please sign in to comment.