Skip to content

Commit bbd85a9

Browse files
authored
fix: Fix Sphinx inventory generation
Issue #265: #265 PR #267: #267
1 parent 14ed959 commit bbd85a9

6 files changed

Lines changed: 34 additions & 34 deletions

File tree

src/mkdocstrings/inventory.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,5 +90,4 @@ def format_sphinx(self) -> bytes:
9090
)
9191

9292
lines = [item.format_sphinx().encode("utf8") for item in self.values()]
93-
# we use compression level 0 to make the file readable by humans
94-
return header + zlib.compress(b"\n".join(lines), 0)
93+
return header + zlib.compress(b"\n".join(lines) + b"\n", 9)

src/mkdocstrings/templates/python/material/function.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
{% endif %}
1818

1919
{% filter heading(heading_level,
20-
role="func",
20+
role="function",
2121
id=html_id,
2222
class="doc doc-heading",
2323
toc_label=function.name ~ "()") %}
@@ -36,7 +36,7 @@
3636
{% else %}
3737
{% if config.show_root_toc_entry %}
3838
{% filter heading(heading_level,
39-
role="func",
39+
role="function",
4040
id=html_id,
4141
toc_label=function.path,
4242
hidden=True) %}

src/mkdocstrings/templates/python/material/method.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
{% endif %}
1818

1919
{% filter heading(heading_level,
20-
role="meth",
20+
role="method",
2121
id=html_id,
2222
class="doc doc-heading",
2323
toc_label=method.name ~ "()") %}
@@ -36,7 +36,7 @@
3636
{% else %}
3737
{% if config.show_root_toc_entry %}
3838
{% filter heading(heading_level,
39-
role="meth",
39+
role="method",
4040
id=html_id,
4141
toc_label=method.path,
4242
hidden=True) %}

src/mkdocstrings/templates/python/material/module.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
{% endif %}
1818

1919
{% filter heading(heading_level,
20-
role="mod",
20+
role="module",
2121
id=html_id,
2222
class="doc doc-heading",
2323
toc_label=module.name) %}
@@ -33,7 +33,7 @@
3333
{% else %}
3434
{% if config.show_root_toc_entry %}
3535
{% filter heading(heading_level,
36-
role="mod",
36+
role="module",
3737
id=html_id,
3838
toc_label=module.path,
3939
hidden=True) %}

tests/test_inventory.py

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,51 @@
11
"""Tests for the inventory module."""
22

3+
import sys
34
from io import BytesIO
45
from os.path import join
5-
from pathlib import Path
66

77
import pytest
8+
from mkdocs.commands.build import build
9+
from mkdocs.config import load_config
810

911
from mkdocstrings.inventory import Inventory, InventoryItem
1012

11-
sphinx_inventory = pytest.importorskip("sphinx.util.inventory", reason="Sphinx is not installed")
12-
MKDOCSTRINGS_OBJECTS_INV = Path("site/objects.inv")
13+
sphinx = pytest.importorskip("sphinx.util.inventory", reason="Sphinx is not installed")
1314

1415

1516
@pytest.mark.parametrize(
16-
"inventory",
17+
"our_inv",
1718
[
1819
Inventory(),
1920
Inventory([InventoryItem(name="object_path", domain="py", role="obj", uri="page_url")]),
2021
Inventory([InventoryItem(name="object_path", domain="py", role="obj", uri="page_url#object_path")]),
2122
Inventory([InventoryItem(name="object_path", domain="py", role="obj", uri="page_url#other_anchor")]),
2223
],
2324
)
24-
def test_sphinx_load_inventory_file(inventory):
25+
def test_sphinx_load_inventory_file(our_inv):
2526
"""Perform the 'live' inventory load test."""
26-
buffer = BytesIO(inventory.format_sphinx())
27-
sphinx_inventory.InventoryFile.load(buffer, "", join)
27+
buffer = BytesIO(our_inv.format_sphinx())
28+
sphinx_inv = sphinx.InventoryFile.load(buffer, "", join)
2829

30+
sphinx_inv_length = sum(len(sphinx_inv[key]) for key in sphinx_inv)
31+
assert sphinx_inv_length == len(our_inv.values())
2932

30-
@pytest.mark.skipif(not MKDOCSTRINGS_OBJECTS_INV.exists(), reason="site/objects.inv does not exist")
33+
for item in our_inv.values():
34+
assert item.name in sphinx_inv[f"{item.domain}:{item.role}"]
35+
36+
37+
@pytest.mark.skipif(sys.version_info < (3, 7), reason="using plugins that require Python 3.7")
3138
def test_sphinx_load_mkdocstrings_inventory_file():
3239
"""Perform the 'live' inventory load test on mkdocstrings own inventory."""
33-
with MKDOCSTRINGS_OBJECTS_INV.open("rb") as fp:
34-
sphinx_inventory.InventoryFile.load(fp, "", join)
40+
mkdocs_config = load_config()
41+
build(mkdocs_config)
42+
own_inv = mkdocs_config["plugins"]["mkdocstrings"].handlers.inventory
43+
44+
with open("site/objects.inv", "rb") as fp:
45+
sphinx_inv = sphinx.InventoryFile.load(fp, "", join)
46+
47+
sphinx_inv_length = sum(len(sphinx_inv[key]) for key in sphinx_inv)
48+
assert sphinx_inv_length == len(own_inv.values())
49+
50+
for item in own_inv.values():
51+
assert item.name in sphinx_inv[f"{item.domain}:{item.role}"]

tests/test_plugin.py

Lines changed: 0 additions & 16 deletions
This file was deleted.

0 commit comments

Comments
 (0)