From ae0df382b4f11d3593b3046a333aa39ec525287e Mon Sep 17 00:00:00 2001 From: Adam Turner <9087854+aa-turner@users.noreply.github.com> Date: Tue, 4 Feb 2025 00:13:10 +0000 Subject: [PATCH 1/4] Set ``__version__`` in the runtime package * Return the version key in ``setup()`` * Add static type annotations to ``setup()`` * Fix the Python version for ``tomli`` --- CONTRIBUTING.rst | 2 +- pyproject.toml | 4 ++-- python_docs_theme/__init__.py | 19 +++++++++++++------ requirements.txt | 2 +- 4 files changed, 17 insertions(+), 10 deletions(-) diff --git a/CONTRIBUTING.rst b/CONTRIBUTING.rst index 2533e96a..c85b77ca 100644 --- a/CONTRIBUTING.rst +++ b/CONTRIBUTING.rst @@ -2,7 +2,7 @@ How to release -------------- - Update ``CHANGELOG.rst`` -- Bump version (YYYY.MM) in ``pyproject.toml`` +- Bump version (YYYY.MM) in ``python_docs_theme/__init__.py`` - Commit - Push to check tests pass on `GitHub Actions `__ diff --git a/pyproject.toml b/pyproject.toml index 1b34d905..9d756d49 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -6,7 +6,6 @@ requires = [ [project] name = "python-docs-theme" -version = "2024.12" description = "The Sphinx theme for the CPython docs and related projects" readme = "README.md" license.file = "LICENSE" @@ -28,10 +27,11 @@ classifiers = [ "Topic :: Documentation", "Topic :: Software Development :: Documentation", ] +dynamic = [ "version" ] + dependencies = [ "sphinx>=3.4", ] - urls.Code = "https://github.com/python/python-docs-theme" urls.Download = "https://pypi.org/project/python-docs-theme/" urls.Homepage = "https://github.com/python/python-docs-theme/" diff --git a/python_docs_theme/__init__.py b/python_docs_theme/__init__.py index 295c1c53..8fe81128 100644 --- a/python_docs_theme/__init__.py +++ b/python_docs_theme/__init__.py @@ -1,15 +1,22 @@ from __future__ import annotations import hashlib -import os from functools import cache from pathlib import Path -from typing import Any +from typing import TYPE_CHECKING + +if TYPE_CHECKING: + from typing import Any + + from sphinx.application import Sphinx + from sphinx.util.typing import ExtensionMetadata import sphinx.application from sphinx.builders.html import StandaloneHTMLBuilder -THEME_PATH = Path(__file__).parent.resolve() +__version__ = "2024.12" + +THEME_PATH = Path(__file__).resolve().parent @cache @@ -52,15 +59,15 @@ def _html_page_context( ) -def setup(app): +def setup(app: Sphinx) -> ExtensionMetadata: app.require_sphinx("3.4") - current_dir = os.path.abspath(os.path.dirname(__file__)) - app.add_html_theme("python_docs_theme", current_dir) + app.add_html_theme("python_docs_theme", str(THEME_PATH)) app.connect("html-page-context", _html_page_context) return { + "version": __version__, "parallel_read_safe": True, "parallel_write_safe": True, } diff --git a/requirements.txt b/requirements.txt index 18e6c17c..ad829d49 100644 --- a/requirements.txt +++ b/requirements.txt @@ -2,4 +2,4 @@ setuptools Babel Jinja2 -tomli; python_version < "3.10" +tomli; python_version < "3.11" From bceb94845fee9038659a9c572e2dc36b31c4ed5c Mon Sep 17 00:00:00 2001 From: Adam Turner <9087854+aa-turner@users.noreply.github.com> Date: Tue, 4 Feb 2025 00:30:59 +0000 Subject: [PATCH 2/4] read __version__ from __init__.py --- babel_runner.py | 23 ++++++++++++++++++++--- pyproject.toml | 1 + 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/babel_runner.py b/babel_runner.py index da4001c7..ee5af161 100755 --- a/babel_runner.py +++ b/babel_runner.py @@ -3,6 +3,7 @@ from __future__ import annotations import argparse +import ast import subprocess from pathlib import Path @@ -17,6 +18,8 @@ ) from ie PROJECT_DIR = Path(__file__).resolve().parent +PYPROJECT_TOML = PROJECT_DIR / "pyproject.toml" +INIT_PY = PROJECT_DIR / "python_docs_theme" / "__init__.py" # Global variables used by pybabel below (paths relative to PROJECT_DIR) DOMAIN = "messages" @@ -29,9 +32,23 @@ def get_project_info() -> dict: """Retrieve project's info to populate the message catalog template""" - with open(Path(PROJECT_DIR / "pyproject.toml"), "rb") as f: - data = tomllib.load(f) - return data["project"] + pyproject_text = PYPROJECT_TOML.read_text(encoding="utf-8") + project_data = tomllib.loads(pyproject_text)["project"] + + # read __version__ from __init__.py + for child in ast.parse(INIT_PY.read_bytes()).body: + if not isinstance(child, ast.Assign): + continue + target = child.targets[0] + if not isinstance(target, ast.Name) or target.id != "__version__": + continue + version_node = child.value + if not isinstance(version_node, ast.Constant): + continue + project_data["version"] = version_node.value + break + + return project_data def extract_messages() -> None: diff --git a/pyproject.toml b/pyproject.toml index 9d756d49..7eb1cc2b 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -32,6 +32,7 @@ dynamic = [ "version" ] dependencies = [ "sphinx>=3.4", ] + urls.Code = "https://github.com/python/python-docs-theme" urls.Download = "https://pypi.org/project/python-docs-theme/" urls.Homepage = "https://github.com/python/python-docs-theme/" From 36b76154688fdc655d624b91267f30addb6f8ca3 Mon Sep 17 00:00:00 2001 From: Adam Turner <9087854+AA-Turner@users.noreply.github.com> Date: Tue, 4 Feb 2025 13:24:52 +0000 Subject: [PATCH 3/4] Update __init__.py Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com> --- python_docs_theme/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python_docs_theme/__init__.py b/python_docs_theme/__init__.py index 8fe81128..63ec5484 100644 --- a/python_docs_theme/__init__.py +++ b/python_docs_theme/__init__.py @@ -3,8 +3,8 @@ import hashlib from functools import cache from pathlib import Path -from typing import TYPE_CHECKING +TYPE_CHECKING = False if TYPE_CHECKING: from typing import Any From 6821b30562488551d93bffe98056ae28acf6c440 Mon Sep 17 00:00:00 2001 From: Adam Turner <9087854+AA-Turner@users.noreply.github.com> Date: Tue, 4 Feb 2025 13:37:02 +0000 Subject: [PATCH 4/4] Update python_docs_theme/__init__.py Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com> --- python_docs_theme/__init__.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/python_docs_theme/__init__.py b/python_docs_theme/__init__.py index 63ec5484..d8dd2c74 100644 --- a/python_docs_theme/__init__.py +++ b/python_docs_theme/__init__.py @@ -4,6 +4,9 @@ from functools import cache from pathlib import Path +import sphinx.application +from sphinx.builders.html import StandaloneHTMLBuilder + TYPE_CHECKING = False if TYPE_CHECKING: from typing import Any @@ -11,9 +14,6 @@ from sphinx.application import Sphinx from sphinx.util.typing import ExtensionMetadata -import sphinx.application -from sphinx.builders.html import StandaloneHTMLBuilder - __version__ = "2024.12" THEME_PATH = Path(__file__).resolve().parent