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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: fix parsing non-trivial package name #247

Merged
merged 3 commits into from
Mar 27, 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 parsing non-trivial package name ([#247](https://github.com/Lightning-AI/utilities/pull/247))


## [0.11.1] - 2024-03-25
Expand Down
2 changes: 1 addition & 1 deletion docs/source/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Lightning-DevToolbox documentation

.. figure:: https://pl-public-data.s3.amazonaws.com/assets_lightning/Lightning.gif
:alt: What is Lightning gif.
:width: 100 %
:width: 80 %

.. toctree::
:maxdepth: 1
Expand Down
8 changes: 8 additions & 0 deletions docs/source/test-page.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
:orphan:

Testing page
============

This is some page serving exclusively for testing purposes.

Link to scikit-learn stable documentation: https://scikit-learn.org/stable/index.html
2 changes: 1 addition & 1 deletion src/lightning_utilities/docs/formatting.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ def adjust_linked_external_docs(
return

# find the expression for package version in {} brackets if any, use re to find it
pkg_ver_all = re.findall(r"{([\w.]+)}", target_link)
pkg_ver_all = re.findall(r"{(.+)}", target_link)
for pkg_ver in pkg_ver_all:
target_link = _update_link_based_imported_package(target_link, pkg_ver, version_digits)

Expand Down
23 changes: 14 additions & 9 deletions tests/unittests/docs/test_formatting.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os.path
import re

import pytest
from lightning_utilities.docs import adjust_linked_external_docs
Expand All @@ -8,25 +9,29 @@
def test_adjust_linked_external_docs(temp_docs):
# take config as it includes API references with `stable`
path_conf = os.path.join(temp_docs, "conf.py")
path_testpage = os.path.join(temp_docs, "test-page.rst")

def _get_line_with_numpy(path_rst: str) -> str:
def _get_line_with_numpy(path_rst: str, pattern: str) -> str:
with open(path_rst, encoding="UTF-8") as fopen:
lines = fopen.readlines()
# find the first line with figure reference
return next(ln for ln in lines if ln.lstrip().startswith('"numpy":'))
return next(ln for ln in lines if pattern in ln)

# validate the initial expectations
line = _get_line_with_numpy(path_conf)
line = _get_line_with_numpy(path_conf, pattern='"numpy":')
assert "https://numpy.org/doc/stable/" in line
line = _get_line_with_numpy(path_testpage, pattern="Link to scikit-learn stable documentation:")
assert "https://scikit-learn.org/stable/" in line

adjust_linked_external_docs(
"https://numpy.org/doc/stable/", "https://numpy.org/doc/{numpy.__version__}/", temp_docs
)

import numpy as np

np_version = np.__version__.split(".")
adjust_linked_external_docs(
"https://scikit-learn.org/stable/", "https://scikit-learn.org/{scikit-learn}/", temp_docs
)

# validate the final state of index page
line = _get_line_with_numpy(path_conf)
assert f"https://numpy.org/doc/{'.'.join(np_version[:2])}/" in line
line = _get_line_with_numpy(path_conf, pattern='"numpy":')
assert re.search(r"https://numpy.org/doc/([1-9]\d*)\.(\d+)/", line)
line = _get_line_with_numpy(path_testpage, pattern="Link to scikit-learn stable documentation:")
assert re.search(r"https://scikit-learn.org/([1-9]\d*)\.(\d+)/", line)
Loading