Skip to content

Commit

Permalink
Set __version__ in the runtime package (#222)
Browse files Browse the repository at this point in the history
* Set ``__version__`` in the runtime package
* Return the version key in ``setup()``
* Add static type annotations to ``setup()``
* Fix the Python version for ``tomli``
* read ``__version__`` from ``__init__.py``

Co-authored-by: Hugo van Kemenade <[email protected]>
  • Loading branch information
AA-Turner and hugovk authored Feb 4, 2025
1 parent b6a1724 commit 4823212
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 12 deletions.
2 changes: 1 addition & 1 deletion CONTRIBUTING.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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 <https://github.com/python/python-docs-theme/actions>`__
Expand Down
23 changes: 20 additions & 3 deletions babel_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from __future__ import annotations

import argparse
import ast
import subprocess
from pathlib import Path

Expand All @@ -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"
Expand All @@ -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:
Expand Down
3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -28,6 +27,8 @@ classifiers = [
"Topic :: Documentation",
"Topic :: Software Development :: Documentation",
]
dynamic = [ "version" ]

dependencies = [
"sphinx>=3.4",
]
Expand Down
19 changes: 13 additions & 6 deletions python_docs_theme/__init__.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
from __future__ import annotations

import hashlib
import os
from functools import cache
from pathlib import Path
from typing import Any

import sphinx.application
from sphinx.builders.html import StandaloneHTMLBuilder

THEME_PATH = Path(__file__).parent.resolve()
TYPE_CHECKING = False
if TYPE_CHECKING:
from typing import Any

from sphinx.application import Sphinx
from sphinx.util.typing import ExtensionMetadata

__version__ = "2024.12"

THEME_PATH = Path(__file__).resolve().parent


@cache
Expand Down Expand Up @@ -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,
}
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
setuptools
Babel
Jinja2
tomli; python_version < "3.10"
tomli; python_version < "3.11"

0 comments on commit 4823212

Please sign in to comment.