Skip to content

Commit 8c1a765

Browse files
author
Mariam Ahhttouche
committed
Apply PR feedback and refactor code
1 parent d52fdc4 commit 8c1a765

File tree

1 file changed

+13
-32
lines changed

1 file changed

+13
-32
lines changed

plugins/modules/uv_python.py

Lines changed: 13 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ def _ensure_min_uv_version(self):
146146
required_version=MINIMUM_UV_VERSION,
147147
)
148148

149-
def install_python(self):
149+
def install_python(self) -> tuple[bool, str, str, int, list, list]:
150150
"""
151151
Runs command 'uv python install X.Y.Z' which installs specified python version.
152152
If patch version is not specified uv installs latest available patch version.
@@ -158,10 +158,6 @@ def install_python(self):
158158
- command's return code
159159
- list of installed versions
160160
- list of installation paths for each installed version
161-
Raises:
162-
AnsibleModuleFailJson:
163-
If the install command exits with a non-zero return code.
164-
If specified version is not available for download.
165161
"""
166162
find_rc, existing_version, ignored_err = self._find_python("--show-version")
167163
if find_rc == 0:
@@ -177,7 +173,7 @@ def install_python(self):
177173
latest_version, path = self._get_latest_patch_release("--only-installed", "--managed-python")
178174
return True, out, err, rc, [latest_version], [path]
179175

180-
def uninstall_python(self):
176+
def uninstall_python(self) -> tuple[bool, str, str, int, list, list]:
181177
"""
182178
Runs command 'uv python uninstall X.Y.Z' which removes specified python version from environment.
183179
If patch version is not specified all correspending installed patch versions are removed.
@@ -189,9 +185,6 @@ def uninstall_python(self):
189185
- command's return code
190186
- list of uninstalled versions
191187
- list of previous installation paths for each uninstalled version
192-
Raises:
193-
AnsibleModuleFailJson:
194-
If the uninstall command exits with a non-zero return code.
195188
"""
196189
installed_versions, install_paths = self._get_installed_versions("--managed-python")
197190
if not installed_versions:
@@ -201,7 +194,7 @@ def uninstall_python(self):
201194
rc, out, err = self._exec(self.python_version_str, "uninstall", check_rc=True)
202195
return True, out, err, rc, installed_versions, install_paths
203196

204-
def upgrade_python(self):
197+
def upgrade_python(self) -> tuple[bool, str, str, int, list, list]:
205198
"""
206199
Runs command 'uv python install X.Y.Z' with latest patch version available.
207200
Returns:
@@ -212,10 +205,6 @@ def upgrade_python(self):
212205
- command's return code
213206
- list of installed versions
214207
- list of installation paths for each installed version
215-
Raises:
216-
AnsibleModuleFailJson:
217-
If the install command exits with a non-zero return code.
218-
If resolved patch version is not available for download.
219208
"""
220209
rc, installed_version_str, ignored_err = self._find_python("--show-version")
221210
installed_version = self._parse_version(installed_version_str)
@@ -233,7 +222,7 @@ def upgrade_python(self):
233222
latest_version_str, latest_path = self._get_latest_patch_release("--only-installed", "--managed-python")
234223
return True, out, err, rc, [latest_version_str], [latest_path]
235224

236-
def _exec(self, python_version, command, *args, check_rc=False):
225+
def _exec(self, python_version, command, *args, check_rc=False) -> tuple[int, str, str]:
237226
"""
238227
Execute a uv python subcommand.
239228
Args:
@@ -244,15 +233,12 @@ def _exec(self, python_version, command, *args, check_rc=False):
244233
Returns:
245234
tuple[int, str, str]:
246235
A tuple containing (rc, stdout, stderr).
247-
Raises:
248-
AnsibleModuleFailJson:
249-
If check_rc is True and the command exits with a non-zero return code.
250236
"""
251237
cmd = [self.bin_path, "python", command, python_version, "--color", "never", *args]
252238
rc, out, err = self.module.run_command(cmd, check_rc=check_rc)
253239
return rc, out, err
254240

255-
def _find_python(self, *args, check_rc=False):
241+
def _find_python(self, *args, check_rc=False) -> tuple[int, str, str]:
256242
"""
257243
Runs command 'uv python find' which returns path of installed patch releases for a given python version.
258244
If multiple patch versions are installed, "uv python find" returns the one used by default
@@ -263,38 +249,33 @@ def _find_python(self, *args, check_rc=False):
263249
Returns:
264250
tuple[int, str, str]:
265251
A tuple containing (rc, stdout, stderr).
266-
Raises:
267-
AnsibleModuleFailJson:
268-
If check_rc is True and the command exits with a non-zero return code.
269252
"""
270253
rc, out, err = self._exec(self.python_version_str, "find", *args, check_rc=check_rc)
271254
if rc == 0:
272255
out = out.strip()
273256
return rc, out, err
274257

275-
def _list_python(self, *args, check_rc=False):
258+
def _list_python(self, *args, check_rc=False) -> tuple[int, list, str]:
276259
"""
277260
Runs command 'uv python list' (which returns list of installed patch releases for a given python version).
278261
Official documentation https://docs.astral.sh/uv/reference/cli/#uv-python-list
279262
Args:
280263
*args: Additional positional arguments passed to _exec.
281264
check_rc (bool): Whether to fail if the command exits with non-zero return code.
282265
Returns:
283-
tuple[int, str, str]:
266+
tuple[int, list, str]
284267
A tuple containing (rc, stdout, stderr).
285-
Raises:
286-
AnsibleModuleFailJson:
287-
If check_rc is True and the command exits with a non-zero return code.
288268
"""
289269
rc, out, err = self._exec(self.python_version_str, "list", "--output-format", "json", *args, check_rc=check_rc)
270+
pythons_installed = []
290271
try:
291-
out = json.loads(out)
272+
pythons_installed = json.loads(out)
292273
except json.decoder.JSONDecodeError:
293274
# This happens when no version is found
294275
pass
295-
return rc, out, err
276+
return rc, pythons_installed, err
296277

297-
def _get_latest_patch_release(self, *args):
278+
def _get_latest_patch_release(self, *args) -> tuple[str, str]:
298279
"""
299280
Returns latest available patch release for a given python version.
300281
Args:
@@ -314,7 +295,7 @@ def _get_latest_patch_release(self, *args):
314295
path = version.get("path", "")
315296
return latest_version, path
316297

317-
def _get_installed_versions(self, *args):
298+
def _get_installed_versions(self, *args) -> tuple[list, list]:
318299
"""
319300
Returns installed patch releases for a given python version.
320301
Args:
@@ -326,7 +307,7 @@ def _get_installed_versions(self, *args):
326307
"""
327308
ignored_rc, results, ignored_err = self._list_python("--only-installed", *args)
328309
if results:
329-
return [result["version"] for result in results], [result["path"] for result in results]
310+
return [result.get("version") for result in results], [result.get("path") for result in results]
330311
return [], []
331312

332313
@staticmethod

0 commit comments

Comments
 (0)