Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: fix/use PyPI versions for pinning #243

Merged
merged 2 commits into from
Mar 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Fixed

-
- docs: fix/use PyPI versions for pinning links ([#243](https://github.com/Lightning-AI/utilities/pull/243))


## [0.11.0] - 2024-03-18
Expand Down
34 changes: 29 additions & 5 deletions src/lightning_utilities/docs/formatting.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import os
import re
import sys
from typing import Iterable, Optional, Tuple, Union
from typing import Iterable, List, Optional, Tuple, Union


def _transform_changelog(path_in: str, path_out: str) -> None:
Expand Down Expand Up @@ -69,6 +69,27 @@ def find_source() -> Tuple[str, int, int]:
return f"https://github.com/{github_user}/{github_repo}/blob/{filename}"


def _load_pypi_versions(package_name: str) -> List[str]:
"""Load the versions of the package from PyPI.

>>> _load_pypi_versions("numpy") # doctest: +ELLIPSIS
['0.9.6', '0.9.8', '1.0', ...]
>>> _load_pypi_versions("scikit-learn") # doctest: +ELLIPSIS
['0.9', '0.10', '0.11', '0.12', ...]

"""
from distutils.version import LooseVersion

import requests

url = f"https://pypi.org/pypi/{package_name}/json"
data = requests.get(url, timeout=10).json()
versions = data["releases"].keys()
# filter all version which include only numbers and dots
versions = {k for k in versions if re.match(r"^\d+(\.\d+)*$", k)}
return sorted(versions, key=LooseVersion)


def _update_link_based_imported_package(link: str, pkg_ver: str, version_digits: Optional[int]) -> str:
"""Adjust the linked external docs to be local.

Expand All @@ -79,10 +100,13 @@ def _update_link_based_imported_package(link: str, pkg_ver: str, version_digits:

"""
pkg_att = pkg_ver.split(".")
# load the package with all additional sub-modules
module = importlib.import_module(".".join(pkg_att[:-1]))
# load the attribute
ver = getattr(module, pkg_att[-1])
try:
ver = _load_pypi_versions(pkg_att[0])[-1]
except Exception:
# load the package with all additional sub-modules
module = importlib.import_module(".".join(pkg_att[:-1]))
# load the attribute
ver = getattr(module, pkg_att[0])
# drop any additional context after `+`
ver = ver.split("+")[0]
# crop the version to the number of digits
Expand Down
Loading