Skip to content

Commit 088430a

Browse files
committed
do not shorten identifiers in docstrings, fix mitmproxy#739
1 parent ab317b4 commit 088430a

File tree

6 files changed

+38
-27
lines changed

6 files changed

+38
-27
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
## Unreleased: pdoc next
66

7+
- Do not shorten `current_module.func` to `func` in docstrings when linking.
8+
This prevents logical errors in code examples with imports.
79
- Add support for Python 3.13.
810
- pdoc now strips `collections.abc.` from type annotations to make them more concise.
911
([#736](https://github.com/mitmproxy/pdoc/pull/736), @mhils)

pdoc/render_helpers.py

+15-6
Original file line numberDiff line numberDiff line change
@@ -307,11 +307,17 @@ def module_candidates(identifier: str, current_module: str) -> Iterable[str]:
307307

308308

309309
@pass_context
310-
def linkify(context: Context, code: str, namespace: str = "") -> str:
310+
def linkify(
311+
context: Context, code: str, namespace: str = "", shorten: bool = True
312+
) -> str:
311313
"""
312314
Link all identifiers in a block of text. Identifiers referencing unknown modules or modules that
313315
are not rendered at the moment will be ignored.
314316
A piece of text is considered to be an identifier if it either contains a `.` or is surrounded by `<code>` tags.
317+
318+
If `shorten` is True, replace identifiers with short forms where possible.
319+
For example, replace "current_module.Foo" with "Foo". This is useful for annotations
320+
(which are verbose), but undesired for docstrings (where we want to preserve intent).
315321
"""
316322

317323
def linkify_repl(m: re.Match):
@@ -381,12 +387,15 @@ def linkify_repl(m: re.Match):
381387
and (target_object is doc.obj or target_object is None)
382388
and context["is_public"](doc).strip()
383389
):
384-
if module == mod:
385-
url_text = qualname
390+
if shorten:
391+
if module == mod:
392+
url_text = qualname
393+
else:
394+
url_text = doc.fullname
395+
if plain_text.endswith("()"):
396+
url_text += "()"
386397
else:
387-
url_text = doc.fullname
388-
if plain_text.endswith("()"):
389-
url_text += "()"
398+
url_text = plain_text
390399
return f'<a href="{relative_link(context["module"].modulename, doc.modulename)}#{qualname}">{url_text}</a>'
391400

392401
# No matches found.

pdoc/templates/default/module.html.jinja2

+1-1
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ See https://pdoc.dev/docs/pdoc/render_helpers.html#DefaultMacroExtension for an
209209
{% enddefaultmacro %}
210210
{% defaultmacro docstring(var) %}
211211
{% if var.docstring %}
212-
<div class="docstring">{{ var.docstring | replace("@public", "") | to_markdown | to_html | linkify(namespace=var.qualname) }}</div>
212+
<div class="docstring">{{ var.docstring | replace("@public", "") | to_markdown | to_html | linkify(namespace=var.qualname, shorten=False) }}</div>
213213
{% endif %}
214214
{% enddefaultmacro %}
215215
{% defaultmacro nav_members(members) %}

test/testdata/demopackage.html

+4-4
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ <h1 class="modulename">
263263
</span></pre></div>
264264

265265

266-
<div class="docstring"><p>This class is defined in <a href="demopackage/child_c.html">demopackage.child_c</a> and inherits from <a href="#B">B</a>.</p>
266+
<div class="docstring"><p>This class is defined in <a href="demopackage/child_c.html">demopackage.child_c</a> and inherits from <a href="#B">.child_b.B</a>.</p>
267267
</div>
268268

269269

@@ -331,9 +331,9 @@ <h5>Inherited Members</h5>
331331
<p>We want to make sure that these links render:</p>
332332

333333
<ul>
334-
<li><a href="#G">G</a></li>
335-
<li><a href="#G">G</a></li>
336-
<li><a href="#G">G</a></li>
334+
<li><a href="#G">demopackage.G</a></li>
335+
<li><a href="#G">demopackage.subpackage.G</a></li>
336+
<li><a href="#G">demopackage.subpackage.child_g.G</a></li>
337337
</ul>
338338
</div>
339339

test/testdata/demopackage_dir.html

+12-12
Original file line numberDiff line numberDiff line change
@@ -816,7 +816,7 @@ <h1 class=&quot;modulename&quot;>
816816
</span></pre></div>
817817
818818
819-
<div class=&quot;docstring&quot;><p>This class is defined in <a href=&quot;demopackage/child_c.html&quot;>demopackage.child_c</a> and inherits from <a href=&quot;#B&quot;>B</a>.</p>
819+
<div class=&quot;docstring&quot;><p>This class is defined in <a href=&quot;demopackage/child_c.html&quot;>demopackage.child_c</a> and inherits from <a href=&quot;#B&quot;>.child_b.B</a>.</p>
820820
</div>
821821
822822
@@ -884,9 +884,9 @@ <h5>Inherited Members</h5>
884884
<p>We want to make sure that these links render:</p>
885885
886886
<ul>
887-
<li><a href=&quot;#G&quot;>G</a></li>
888-
<li><a href=&quot;#G&quot;>G</a></li>
889-
<li><a href=&quot;#G&quot;>G</a></li>
887+
<li><a href=&quot;#G&quot;>demopackage.G</a></li>
888+
<li><a href=&quot;#G&quot;>demopackage.subpackage.G</a></li>
889+
<li><a href=&quot;#G&quot;>demopackage.subpackage.child_g.G</a></li>
890890
</ul>
891891
</div>
892892
@@ -1247,14 +1247,14 @@ <h1 class=&quot;modulename&quot;>
12471247
12481248
<div class=&quot;docstring&quot;><p>A sub-package.</p>
12491249
1250-
<p>It imports and re-exposes <a href=&quot;#B&quot;>B</a>, and links to <code><a href=&quot;../demopackage.html#C&quot;>demopackage.C</a></code> .</p>
1250+
<p>It imports and re-exposes <a href=&quot;#B&quot;>..child_b.B</a>, and links to <code><a href=&quot;../demopackage.html#C&quot;>..C</a></code> .</p>
12511251
1252-
<p>It also exposes <a href=&quot;#G&quot;>G</a>. These links should all point to the local instance:</p>
1252+
<p>It also exposes <a href=&quot;#G&quot;>.child_g.G</a>. These links should all point to the local instance:</p>
12531253
12541254
<ul>
1255-
<li><a href=&quot;#G&quot;>G</a></li>
1256-
<li><a href=&quot;#G&quot;>G</a></li>
1257-
<li><a href=&quot;#G&quot;>G</a></li>
1255+
<li><a href=&quot;#G&quot;>demopackage.G</a></li>
1256+
<li><a href=&quot;#G&quot;>demopackage.subpackage.G</a></li>
1257+
<li><a href=&quot;#G&quot;>demopackage.subpackage.child_g.G</a></li>
12581258
</ul>
12591259
</div>
12601260
@@ -1699,8 +1699,8 @@ <h1 class=&quot;modulename&quot;>
16991699
17001700
<ul>
17011701
<li><a href=&quot;../demopackage.html#G&quot;>demopackage.G</a></li>
1702-
<li><a href=&quot;../demopackage.html#G&quot;>demopackage.G</a></li>
1703-
<li><a href=&quot;../demopackage.html#G&quot;>demopackage.G</a></li>
1702+
<li><a href=&quot;../demopackage.html#G&quot;>demopackage.subpackage.G</a></li>
1703+
<li><a href=&quot;../demopackage.html#G&quot;>demopackage.subpackage.child_g.G</a></li>
17041704
</ul>
17051705
</div>
17061706
@@ -2044,7 +2044,7 @@ <h1 class=&quot;modulename&quot;>
20442044
</span></pre></div>
20452045
20462046
2047-
<div class=&quot;docstring&quot;><p>This class is defined in <a href=&quot;&quot;>demopackage.child_c</a> and inherits from <a href=&quot;../demopackage.html#B&quot;>demopackage.B</a>.</p>
2047+
<div class=&quot;docstring&quot;><p>This class is defined in <a href=&quot;&quot;>demopackage.child_c</a> and inherits from <a href=&quot;../demopackage.html#B&quot;>.child_b.B</a>.</p>
20482048
</div>
20492049
20502050

test/testdata/top_level_reimports.html

+4-4
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,10 @@ <h1 class="modulename">
6363
<div class="docstring"><p>Test that objects re-exposed from an internal module are properly interlinked if they appear on the same page.</p>
6464

6565
<ul>
66-
<li><a href="#baz">baz</a></li>
67-
<li><a href="#baz">baz()</a></li>
68-
<li><code><a href="#baz">baz</a></code></li>
69-
<li><code><a href="#baz">baz()</a></code></li>
66+
<li><a href="#baz">top_level_reimports._internal.baz</a></li>
67+
<li><a href="#baz">top_level_reimports._internal.baz()</a></li>
68+
<li><code><a href="#baz">top_level_reimports._internal.baz</a></code></li>
69+
<li><code><a href="#baz">top_level_reimports._internal.baz()</a></code></li>
7070
</ul>
7171
</div>
7272

0 commit comments

Comments
 (0)