Skip to content

Commit f8bc6e8

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

File tree

1 file changed

+17
-36
lines changed

1 file changed

+17
-36
lines changed

plugins/modules/uv_python.py

Lines changed: 17 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
- |
3030
Not all canonical Python versions are supported in this release. Valid version numbers consist of two or three dot-separated numeric components,
3131
with an optional 'pre-release' tag on the end such as V(3.12), V(3.12.3), V(3.15.0a5).
32-
- Advanced uv selectors such as C(>=3.12,<3.13) or C(cpython@3.12) are not supported in this release.
32+
- Advanced uv selectors such as V(>=3.12,<3.13) or V(cpython@3.12) are not supported in this release.
3333
- When you specify only a major.minor version, behavior depends on the O(state) parameter.
3434
type: str
3535
required: true
@@ -39,18 +39,18 @@
3939
- |
4040
V(present) ensures the specified version is installed.
4141
If you specify a full patch version (for example O(version=3.12.3)), that exact version will be installed if not already present.
42-
If you only specify a minor version (for example C(3.12)), the latest available patch version for that minor release is installed only
42+
If you only specify a minor version (for example V(3.12)), the latest available patch version for that minor release is installed only
4343
if no patch version for that minor release is currently installed (including patch versions not managed by C(uv)).
4444
RV(python_versions) and RV(python_paths) lengths are always equal to one for this state.
4545
- |
4646
V(absent) ensures the specified version is removed.
4747
If you specify a full patch version, only that exact patch version is removed.
48-
If you only specify a minor version (for example C(3.12)), all installed patch versions for that minor release are removed.
48+
If you only specify a minor version (for example V(3.12)), all installed patch versions for that minor release are removed.
4949
If you specify a version that is not installed, no changes are made.
5050
RV(python_versions) and RV(python_paths) lengths can be higher or equal to one in this state.
5151
- |
5252
V(latest) ensures the latest available patch version for the specified version is installed.
53-
If you only specify a minor version (for example C(3.12)), the latest available patch version for that minor release is always installed.
53+
If you only specify a minor version (for example V(3.12)), the latest available patch version for that minor release is always installed.
5454
If another patch version is already installed but is not the latest, the latest patch version is installed.
5555
The latest patch version installed depends on the C(uv) version, since available Python versions are frozen per C(uv) release.
5656
RV(python_versions) and RV(python_paths) lengths are always equal to one in this state.
@@ -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)