From 54e7a639efb8088e18b122adbe675091b2e51b04 Mon Sep 17 00:00:00 2001 From: Mariam Ahhttouche Date: Tue, 17 Feb 2026 15:17:02 +0100 Subject: [PATCH] uv_python module: refactor code --- plugins/modules/uv_python.py | 23 ++++++++++--------- .../targets/uv_python/tasks/main.yaml | 2 +- 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/plugins/modules/uv_python.py b/plugins/modules/uv_python.py index f92a15e5e4..e1916d7f94 100644 --- a/plugins/modules/uv_python.py +++ b/plugins/modules/uv_python.py @@ -87,17 +87,20 @@ class UV: """ rc, out, _ = self._find_python("--show-version") if rc == 0: - return False, out.split()[0], False + return False, out.split()[0] if self.module.check_mode: _, versions_available, _ = self._list_python() # when uv does not find any available patch version the install command will fail - if not json.loads(versions_available): - return False, "", True - return True, self.python_version_str, False + try: + if not json.loads(versions_available): + self.module.fail_json(msg=(f"Version {self.python_version_str} is not available.")) + except json.decoder.JSONDecodeError as e: + self.module.fail_json(msg=f"Failed to parse 'uv python list' output with error {str(e)}") + return True, self.python_version_str cmd = [self.module.get_bin_path("uv", required=True), self.subcommand, "install", self.python_version_str] self.module.run_command(cmd, check_rc=True) - return True, self.python_version_str, False + return True, self.python_version_str def uninstall_python(self): """ @@ -127,6 +130,8 @@ class UV: """ rc, out, _ = self._find_python("--show-version") latest_version = self._get_latest_patch_release() + if not latest_version: + self.module.fail_json(msg=(f"Version {self.python_version_str} is not available.")) if rc == 0 and out.split()[0] == latest_version: return False, latest_version if self.module.check_mode: @@ -174,11 +179,7 @@ class UV: versions = [StrictVersion(result["version"]) for result in results] latest_version = max(versions).__str__() except ValueError: - self.module.fail_json( - msg=( - f"Version {self.python_version_str} is not available." - ) - ) + pass return latest_version @@ -201,7 +202,7 @@ def main(): try: uv = UV(module) if state == "present": - result["changed"], result["msg"], result["failed"] = uv.install_python() + result["changed"], result["msg"] = uv.install_python() elif state == "absent": result["changed"], result["msg"] = uv.uninstall_python() elif state == "latest": diff --git a/tests/integration/targets/uv_python/tasks/main.yaml b/tests/integration/targets/uv_python/tasks/main.yaml index d009ab6446..7b569e5f0f 100644 --- a/tests/integration/targets/uv_python/tasks/main.yaml +++ b/tests/integration/targets/uv_python/tasks/main.yaml @@ -212,7 +212,7 @@ that: - unexisting_python_check_mode.changed is false - unexisting_python_check_mode.failed is true - - 'unexisting_python_check_mode.msg == ""' + - '"Version 3.12.13 is not available." in unexisting_python_check_mode.msg' - name: Install unexisting python version uv_python: