fix(client_utils): ensure proper type conversion and hints for improved safety

- Standardize `str()` wrapping for color-applied strings in various returns.
- Refine type hints for better code clarity and robustness.
- Add null checks for port inputs to prevent potential errors.
This commit is contained in:
dw-0
2026-01-18 15:58:25 +01:00
parent 123ccde378
commit 6c9a78496a
4 changed files with 11 additions and 11 deletions

View File

@@ -78,10 +78,10 @@ def get_current_client_config() -> str:
installed = [c for c in clients if c.client_config.config_dir.exists()] installed = [c for c in clients if c.client_config.config_dir.exists()]
if not installed: if not installed:
return Color.apply("-", Color.CYAN) return str(Color.apply("-", Color.CYAN))
elif len(installed) == 1: elif len(installed) == 1:
cfg = installed[0].client_config cfg = installed[0].client_config
return Color.apply(cfg.display_name, Color.CYAN) return str(Color.apply(cfg.display_name, Color.CYAN))
# at this point, both client config folders exists, so we need to check # at this point, both client config folders exists, so we need to check
# which are actually included in the printer.cfg of all klipper instances # which are actually included in the printer.cfg of all klipper instances
@@ -100,18 +100,18 @@ def get_current_client_config() -> str:
# if both are included in the same file, we have a potential conflict # if both are included in the same file, we have a potential conflict
if includes_mainsail and includes_fluidd: if includes_mainsail and includes_fluidd:
return Color.apply("Conflict", Color.YELLOW) return str(Color.apply("Conflict", Color.YELLOW))
if not mainsail_includes and not fluidd_includes: if not mainsail_includes and not fluidd_includes:
# there are no includes at all, even though the client config folders exist # there are no includes at all, even though the client config folders exist
return Color.apply("-", Color.CYAN) return str(Color.apply("-", Color.CYAN))
elif len(fluidd_includes) > len(mainsail_includes): elif len(fluidd_includes) > len(mainsail_includes):
# there are more instances that include fluidd than mainsail # there are more instances that include fluidd than mainsail
return Color.apply(fluidd.client_config.display_name, Color.CYAN) return str(Color.apply(fluidd.client_config.display_name, Color.CYAN))
else: else:
# there are the same amount of non-conflicting includes for each config # there are the same amount of non-conflicting includes for each config
# or more instances include mainsail than fluidd # or more instances include mainsail than fluidd
return Color.apply(mainsail.client_config.display_name, Color.CYAN) return str(Color.apply(mainsail.client_config.display_name, Color.CYAN))
def enable_mainsail_remotemode() -> None: def enable_mainsail_remotemode() -> None:
@@ -445,9 +445,9 @@ def get_client_port_selection(
while True: while True:
_type = "Reconfigure" if reconfigure else "Configure" _type = "Reconfigure" if reconfigure else "Configure"
question = f"{_type} {client.display_name} for port" question = f"{_type} {client.display_name} for port"
port_input = get_number_input(question, min_value=80, default=port) port_input: int | None = get_number_input(question, min_value=80, default=port)
if port_input not in ports_in_use: if port_input and port_input not in ports_in_use:
client_settings: WebUiSettings = settings[client.name] client_settings: WebUiSettings = settings[client.name]
client_settings.port = port_input client_settings.port = port_input
settings.save() settings.save()

View File

@@ -97,7 +97,7 @@ class ClientInstallMenu(BaseMenu):
self.message_service.set_message(message) self.message_service.set_message(message)
def _get_current_port(self) -> int: def _get_current_port(self) -> int:
curr_port = get_nginx_listen_port(self.client.nginx_config) curr_port: int | None = get_nginx_listen_port(self.client.nginx_config)
if curr_port is None: if curr_port is None:
# if the port is not found in the config file we use # if the port is not found in the config file we use
# the default port from the kiauh settings as fallback # the default port from the kiauh settings as fallback

View File

@@ -42,7 +42,7 @@ def get_kiauh_version() -> str:
Helper method to get the current KIAUH version by reading the latest tag Helper method to get the current KIAUH version by reading the latest tag
:return: string of the latest tag or a default value if no tags exist :return: string of the latest tag or a default value if no tags exist
""" """
tags = get_local_tags(Path(__file__).parent.parent) tags: List[str] = get_local_tags(Path(__file__).parent.parent)
if tags: if tags:
return tags[-1] return tags[-1]
else: else:

View File

@@ -156,7 +156,7 @@ def format_question(question: str, default=None) -> str:
if default is not None: if default is not None:
formatted_q += f" (default={default})" formatted_q += f" (default={default})"
return Color.apply(f"###### {formatted_q}: ", Color.CYAN) return str(Color.apply(f"###### {formatted_q}: ", Color.CYAN))
def validate_number_input(value: str, min_count: int, max_count: int | None) -> int: def validate_number_input(value: str, min_count: int, max_count: int | None) -> int: