Skip to content

Commit

Permalink
fix(slurmctld,slurmd): correct typing for static checker
Browse files Browse the repository at this point in the history
`ConfigData.get(...)` from `ops` returns a looser type than what's allowed
by pyright if we're passing the result off to `nhc.generate_config(...)`, so
we use `typing.cast(str, ...)` to ensure that the type of the output is just str.

Other changes:

- Removed stale TODO comment from development code.

Signed-off-by: Jason C. Nucciarone <[email protected]>
  • Loading branch information
NucciTheBoss committed Nov 8, 2024
1 parent 07ec196 commit 648e032
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 12 deletions.
2 changes: 1 addition & 1 deletion charms/slurmctld/src/charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ def _on_update_status(self, _: UpdateStatusEvent) -> None:

def _on_show_current_config_action(self, event: ActionEvent) -> None:
"""Show current slurm.conf."""
event.set_results({"slurm.conf": str(self._slurmctld.config.get())})
event.set_results({"slurm.conf": str(self._slurmctld.config.load())})

def _on_slurmrestd_available(self, event: SlurmrestdAvailableEvent) -> None:
"""Check that we have slurm_config when slurmrestd available otherwise defer the event."""
Expand Down
6 changes: 4 additions & 2 deletions charms/slurmd/src/charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"""Slurmd Operator Charm."""

import logging
from typing import Any, Dict
from typing import Any, Dict, cast

from interface_slurmctld import Slurmctld, SlurmctldAvailableEvent
from ops import (
Expand Down Expand Up @@ -95,7 +95,9 @@ def _on_install(self, event: InstallEvent) -> None:

def _on_config_changed(self, _: ConfigChangedEvent) -> None:
"""Handle charm configuration changes."""
if nhc_conf := self.model.config.get("nhc-conf"):
# Casting the type to str is required here because `get` returns a looser
# type than what `nhc.generate_config(...)` allows to be passed.
if nhc_conf := cast(str, self.model.config.get("nhc-conf", "")):
if nhc_conf != self._stored.nhc_conf:
self._stored.nhc_conf = nhc_conf
nhc.generate_config(nhc_conf)
Expand Down
12 changes: 3 additions & 9 deletions charms/slurmd/src/utils/nhc.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import tempfile
import textwrap
from pathlib import Path
from typing import Optional

_logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -82,19 +81,14 @@ def get_config() -> str:
return f"{target} not found."


def generate_config(nhc_config: Optional[str] = None) -> None:
def generate_config(nhc_config: str) -> None:
"""Generate new nhc.conf configuration file.
Args:
nhc_config: Optional NHC configuration to override the default template.
nhc_config: NHC configuration to override default.
"""
target = Path("/etc/nhc/nhc.conf")
if not nhc_config:
template = Path(__file__).absolute().parent / "templates" / "nhc.conf.tmpl"
nhc_config = template.read_text()

try:
target.write_text(nhc_config)
Path("/etc/nhc/nhc.conf").write_text(nhc_config)
except FileNotFoundError as e:
_logger.error(f"error rendering nhc.conf: {e}")
raise
Expand Down

0 comments on commit 648e032

Please sign in to comment.