From 8a358ced79ff2f7b8fa9ddb7acd69eb425753ea8 Mon Sep 17 00:00:00 2001 From: Stefan Mejlgaard Date: Thu, 3 Apr 2025 13:11:58 +0200 Subject: [PATCH] fix: Fix inventory `base_url` being ignored Issue #268: https://github.com/mkdocstrings/python/issues/268 --- .../python/_internal/config.py | 4 +-- tests/test_handler.py | 35 ++++++++++++++++++- 2 files changed, 36 insertions(+), 3 deletions(-) diff --git a/src/mkdocstrings_handlers/python/_internal/config.py b/src/mkdocstrings_handlers/python/_internal/config.py index 11c77da1..210f8fe2 100644 --- a/src/mkdocstrings_handlers/python/_internal/config.py +++ b/src/mkdocstrings_handlers/python/_internal/config.py @@ -971,7 +971,7 @@ class Inventory: ), ] - base: Annotated[ + base_url: Annotated[ str | None, _Field( parent="inventories", @@ -989,7 +989,7 @@ class Inventory: @property def _config(self) -> dict[str, Any]: - return {"base": self.base, "domains": self.domains} + return {"base_url": self.base_url, "domains": self.domains} # YORE: EOL 3.9: Replace `**_dataclass_options` with `frozen=True, kw_only=True` within line. diff --git a/tests/test_handler.py b/tests/test_handler.py index 5940af5e..f98ce545 100644 --- a/tests/test_handler.py +++ b/tests/test_handler.py @@ -4,11 +4,14 @@ import os import sys +from dataclasses import replace from glob import glob +from io import BytesIO from pathlib import Path from textwrap import dedent from typing import TYPE_CHECKING +import mkdocstrings import pytest from griffe import ( Docstring, @@ -20,7 +23,7 @@ ) from mkdocstrings import CollectionError -from mkdocstrings_handlers.python import PythonConfig, PythonHandler, PythonOptions +from mkdocstrings_handlers.python import Inventory, PythonConfig, PythonHandler, PythonOptions if TYPE_CHECKING: from mkdocstrings import MkdocstringsPlugin @@ -298,3 +301,33 @@ class B(A): ... module, handler.get_options({"inherited_members": True}), ) + + +def test_specifying_inventory_base_url(handler: PythonHandler) -> None: + """Assert that the handler renders inventory URLs using the specified base_url.""" + # Update handler config to include an inventory with a base URL + base_url = "https://docs.com/my_library" + inventory = Inventory(url="https://example.com/objects.inv", base_url=base_url) + handler.config = replace(handler.config, inventories=[inventory]) + + # Mock inventory bytes + item_name = "my_library.my_module.MyClass" + mocked_inventory = mkdocstrings.Inventory() + mocked_inventory.register( + name=item_name, + domain="py", + role="class", + uri=f"api-reference/#{item_name}", + dispname=item_name, + ) + mocked_bytes = BytesIO(mocked_inventory.format_sphinx()) + + # Get inventory URL and config + url, config = handler.get_inventory_urls()[0] + + # Load the mocked inventory + _, item_url = next(handler.load_inventory(mocked_bytes, url, **config)) + + # Assert the URL is based on the provided base URL + msg = "Expected inventory URL to start with base_url" + assert item_url.startswith(base_url), msg