-
-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathtest_handler.py
More file actions
410 lines (356 loc) · 13.1 KB
/
Copy pathtest_handler.py
File metadata and controls
410 lines (356 loc) · 13.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
"""Tests for the `handler` module."""
from __future__ import annotations
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 bs4
import mkdocstrings
import pytest
from griffe import (
Docstring,
DocstringSectionExamples,
DocstringSectionKind,
Module,
temporary_inspected_module,
temporary_visited_module,
)
from mkdocstrings import CollectionError
from mkdocstrings_handlers.python import Inventory, PythonConfig, PythonHandler, PythonOptions
if TYPE_CHECKING:
from mkdocstrings import MkdocstringsPlugin
def test_collect_missing_module(handler: PythonHandler) -> None:
"""Assert error is raised for missing modules."""
with pytest.raises(CollectionError):
handler.collect("aaaaaaaa", PythonOptions())
def test_collect_missing_module_item(handler: PythonHandler) -> None:
"""Assert error is raised for missing items within existing modules."""
with pytest.raises(CollectionError):
handler.collect("mkdocstrings.aaaaaaaa", PythonOptions())
def test_collect_module(handler: PythonHandler) -> None:
"""Assert existing module can be collected."""
assert handler.collect("mkdocstrings", PythonOptions())
def test_collect_with_null_parser(handler: PythonHandler) -> None:
"""Assert we can pass `None` as parser when collecting."""
assert handler.collect("mkdocstrings", PythonOptions(docstring_style=None))
@pytest.mark.parametrize(
"handler",
[
{"theme": "mkdocs"},
{"theme": "readthedocs"},
{"theme": {"name": "material"}},
],
indirect=["handler"],
)
def test_render_docstring_examples_section(handler: PythonHandler) -> None:
"""Assert docstrings' examples section can be rendered.
Parameters:
handler: A handler instance (parametrized).
"""
section = DocstringSectionExamples(
value=[
(DocstringSectionKind.text, "This is an example."),
(DocstringSectionKind.examples, ">>> print('Hello')\nHello"),
],
)
template = handler.env.get_template("docstring/examples.html.jinja")
rendered = template.render(section=section, locale="en")
template.render(section=section, locale="not_existing")
assert "<p>This is an example.</p>" in rendered
assert "print" in rendered
assert "Hello" in rendered
def test_expand_globs(tmp_path: Path, plugin: MkdocstringsPlugin) -> None:
"""Assert globs are correctly expanded.
Parameters:
tmp_path: Pytext fixture that creates a temporary directory.
"""
globbed_names = (
"expanded_a",
"expanded_b",
"other_expanded_c",
"other_expanded_d",
)
globbed_paths = [tmp_path.joinpath(globbed_name) for globbed_name in globbed_names]
for path in globbed_paths:
path.touch()
plugin.handlers._tool_config.config_file_path = str(tmp_path.joinpath("mkdocs.yml"))
handler: PythonHandler = plugin.handlers.get_handler("python", {"paths": ["*exp*"]}) # type: ignore[assignment]
for path in globbed_paths:
assert str(path) in handler._paths
def test_expand_globs_without_changing_directory(plugin: MkdocstringsPlugin) -> None:
"""Assert globs are correctly expanded when we are already in the right directory."""
plugin.handlers._tool_config.config_file_path = "mkdocs.yml"
handler: PythonHandler = plugin.handlers.get_handler("python", {"paths": ["*.md"]}) # type: ignore[assignment]
for path in list(glob(os.path.abspath(".") + "/*.md")):
assert path in handler._paths
@pytest.mark.parametrize(
("expect_change", "extension"),
[
(True, "extension.py"),
(True, "extension.py:SomeExtension"),
(True, "path/to/extension.py"),
(True, "path/to/extension.py:SomeExtension"),
(True, {"extension.py": {"option": "value"}}),
(True, {"extension.py:SomeExtension": {"option": "value"}}),
(True, {"path/to/extension.py": {"option": "value"}}),
(True, {"path/to/extension.py:SomeExtension": {"option": "value"}}),
# True because OS path normalization.
(True, "/absolute/path/to/extension.py"),
(True, "/absolute/path/to/extension.py:SomeExtension"),
(True, {"/absolute/path/to/extension.py": {"option": "value"}}),
(True, {"/absolute/path/to/extension.py:SomeExtension": {"option": "value"}}),
(False, "dot.notation.path.to.extension"),
(False, "dot.notation.path.to.pyextension"),
(False, {"dot.notation.path.to.extension": {"option": "value"}}),
(False, {"dot.notation.path.to.pyextension": {"option": "value"}}),
],
)
def test_extension_paths(
tmp_path: Path,
expect_change: bool,
extension: str | dict,
plugin: MkdocstringsPlugin,
) -> None:
"""Assert extension paths are resolved relative to config file."""
plugin.handlers._tool_config.config_file_path = str(tmp_path.joinpath("mkdocs.yml"))
handler: PythonHandler = plugin.handlers.get_handler("python") # type: ignore[assignment]
normalized = handler.normalize_extension_paths([extension])[0]
if expect_change:
if isinstance(normalized, str) and isinstance(extension, str):
assert normalized == str(tmp_path.joinpath(extension))
elif isinstance(normalized, dict) and isinstance(extension, dict):
pth, options = next(iter(extension.items()))
assert normalized == {str(tmp_path.joinpath(pth)): options}
else:
raise ValueError("Normalization must not change extension items type")
else:
assert normalized == extension
def test_rendering_object_source_without_lineno(handler: PythonHandler) -> None:
"""Test rendering objects without a line number."""
code = dedent(
"""
'''Module docstring.'''
class Class:
'''Class docstring.'''
def function(self):
'''Function docstring.'''
attribute = 0
'''Attribute docstring.'''
""",
)
with temporary_visited_module(code) as module:
module["Class"].lineno = None
module["Class.function"].lineno = None
module["attribute"].lineno = None
assert handler.render(module, PythonOptions(show_source=True))
def test_give_precedence_to_user_paths() -> None:
"""Assert user paths take precedence over default paths."""
last_sys_path = sys.path[-1]
handler = PythonHandler(
theme="material",
custom_templates=None,
base_dir=Path("."),
config=PythonConfig.from_data(paths=[last_sys_path]),
mdx=[],
mdx_config={},
)
assert handler._paths[0] == last_sys_path
@pytest.mark.parametrize(
("section", "code"),
[
(
"Attributes",
"""
class A:
'''Summary.
Attributes:
x: X.
y: Y.
'''
x: int = 0
'''X.'''
y: int = 0
'''Y.'''
""",
),
(
"Methods",
"""
class A:
'''Summary.
Methods:
x: X.
y: Y.
'''
def x(self): ...
'''X.'''
def y(self): ...
'''Y.'''
""",
),
(
"Functions",
"""
'''Summary.
Functions:
x: X.
y: Y.
'''
def x(): ...
'''X.'''
def y(): ...
'''Y.'''
""",
),
(
"Classes",
"""
'''Summary.
Classes:
A: A.
B: B.
'''
class A: ...
'''A.'''
class B: ...
'''B.'''
""",
),
(
"Modules",
"""
'''Summary.
Modules:
a: A.
b: B.
'''
""",
),
],
)
def test_deduplicate_summary_sections(handler: PythonHandler, section: str, code: str) -> None:
"""Assert summary sections are deduplicated."""
summary_section = section.lower()
summary_section = "functions" if summary_section == "methods" else summary_section
with temporary_visited_module(code, docstring_parser="google") as module:
if summary_section == "modules":
module.set_member("a", Module("A", docstring=Docstring("A.")))
module.set_member("b", Module("B", docstring=Docstring("B.")))
html = handler.render(
module,
handler.get_options(
{
"summary": {summary_section: True},
"show_source": False,
"show_submodules": True,
},
),
)
assert html.count(f"{section}:") == 1
def test_inheriting_self_from_parent_class(handler: PythonHandler) -> None:
"""Inspect self only once when inheriting it from parent class."""
with temporary_inspected_module(
"""
class A: ...
class B(A): ...
A.B = B
""",
) as module:
# Assert no recusrion error.
handler.render(
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
def _source_labels(html: str) -> list[Path]:
soup = bs4.BeautifulSoup(html, features="html.parser")
labels = []
for summary in soup.find_all("summary"):
if "Source code in" in summary.get_text():
code_tag = summary.find("code")
assert code_tag is not None
labels.append(Path(code_tag.get_text(strip=True)))
return labels
def _write_site_packages_package(tmp_path: Path, *, single_module: bool) -> None:
"""Lay out the issue-333 scenario: a package installed in a virtual environment inside the project.
The environment's `site-packages` directory is relative to the current
working directory, so the package's `relative_filepath` is relative too,
slipping past `is_absolute()` checks.
"""
code = """
class Model:
'''Model docstring.'''
def __init__(self) -> None:
'''Init docstring.'''
self.model_attribute = 0
def method(self) -> None:
'''Method docstring.'''
"""
site = tmp_path / "site-packages"
module_path = site / "pkg.py" if single_module else site / "pkg" / "__init__.py"
module_path.parent.mkdir(parents=True)
module_path.write_text(dedent(code), encoding="utf-8")
@pytest.mark.parametrize(
"handler",
[
{"theme": "readthedocs"},
{"theme": {"name": "material"}},
],
indirect=["handler"],
)
@pytest.mark.parametrize(
("single_module", "extra_options", "expected_label"),
[
pytest.param(False, {"merge_init_into_class": True}, Path("pkg", "__init__.py"), id="merged-init"),
pytest.param(False, {}, Path("pkg", "__init__.py"), id="class-and-methods"),
pytest.param(True, {"merge_init_into_class": True}, Path("pkg.py"), id="single-module"),
],
)
def test_no_environment_path_in_source_labels(
*,
tmp_path: Path,
monkeypatch: pytest.MonkeyPatch,
handler: PythonHandler,
single_module: bool,
extra_options: dict,
expected_label: Path,
) -> None:
"""Assert source labels never show an environment path."""
_write_site_packages_package(tmp_path, single_module=single_module)
monkeypatch.chdir(tmp_path)
# `collect()` reads the search paths lazily from this attribute.
handler._paths = [str(tmp_path / "site-packages")]
options = handler.get_options({"show_source": True, **extra_options})
html = handler.render(handler.collect("pkg.Model", options), options)
labels = _source_labels(html)
assert labels
assert set(labels) == {expected_label}
# The source bodies themselves are still rendered.
assert "model_attribute" in html