Skip to content

Commit

Permalink
docs: fix/use PyPI versions for pinning (#243)
Browse files Browse the repository at this point in the history
* docs: fix/use PyPI versions for pinning
* chlog & fixing
  • Loading branch information
Borda authored Mar 22, 2024
1 parent eb78566 commit a2c03eb
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 6 deletions.
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

0 comments on commit a2c03eb

Please sign in to comment.