Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: include documentation for double-underscore methods when requested #646

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions pdoc/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@
help="The default docstring format. For non-Markdown formats, pdoc will first convert matching syntax elements to "
"Markdown and then process everything as Markdown.",
)
renderopts.add_argument(
"--include-dunder",
action=BooleanOptionalAction,
default=False,
help="Show double-underscore methods that have a docstring.",
)
renderopts.add_argument(
"--include-undocumented",
action=BooleanOptionalAction,
Expand Down Expand Up @@ -182,6 +188,7 @@ def cli(args: list[str] | None = None) -> None:

pdoc.render.configure(
docformat=opts.docformat,
include_dunder=opts.include_dunder,
include_undocumented=opts.include_undocumented,
edit_url_map=dict(x.split("=", 1) for x in opts.edit_url),
favicon=opts.favicon,
Expand Down
2 changes: 2 additions & 0 deletions pdoc/render.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ def configure(
docformat: Literal[
"markdown", "google", "numpy", "restructuredtext" # noqa: F821
] = "restructuredtext",
include_dunder: bool = False,
include_undocumented: bool = True,
edit_url_map: Mapping[str, str] | None = None,
favicon: str | None = None,
Expand Down Expand Up @@ -77,6 +78,7 @@ def configure(
env.loader = FileSystemLoader(searchpath)

env.globals["docformat"] = docformat
env.globals["include_dunder"] = include_dunder
env.globals["include_undocumented"] = include_undocumented
env.globals["edit_url_map"] = edit_url_map or {}
env.globals["math"] = math
Expand Down
5 changes: 4 additions & 1 deletion pdoc/templates/default/module.html.jinja2
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ See https://pdoc.dev/docs/pdoc/render_helpers.html#DefaultMacroExtension for an
{% defaultmacro is_public(doc) %}
{#
This macro is a bit unconventional in that its output is not rendered, but treated as a boolean:
Returning no text is interpreted as false, returning any other text is iterpreted as true.
Returning no text is interpreted as false, returning any other text is interpreted as true.
Implementing this as a macro makes it very easy to override with a custom template, see
https://github.com/mitmproxy/pdoc/tree/main/examples/custom-template.
#}
Expand All @@ -257,6 +257,9 @@ See https://pdoc.dev/docs/pdoc/render_helpers.html#DefaultMacroExtension for an
{% elif not doc.name.startswith("_") %}
{# members not starting with an underscore are considered public by default #}
true
{% elif include_dunder and doc.docstring %}
{# explicitly include documentation for double-underscore methods if available #}
true
{% endif %}
{% enddefaultmacro %}
{# fmt: off #}
Expand Down
6 changes: 6 additions & 0 deletions test/test_snapshot.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,12 @@ def outfile(self, format: str) -> Path:
"include_undocumented": False,
},
),
Snapshot(
"dunder",
render_options={
"include_dunder": True,
},
),
]


Expand Down
205 changes: 205 additions & 0 deletions test/testdata/dunder.html

Large diffs are not rendered by default.

13 changes: 13 additions & 0 deletions test/testdata/dunder.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
"""
Example where double-underscore method should be rendered.
"""


class HasDunderMethods:
def __eq__(self, other):
# Not documented because no docstring.
pass

def __lt__(self, other):
"""Documented because it has docstring."""

5 changes: 5 additions & 0 deletions test/testdata/dunder.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<module dunder # Example where double…
<class dunder.HasDunderMethods
<method def __init__(): ...>
>
>
Loading