From 493d2704c3cdf3227b396ad53abf7650e9ee0af8 Mon Sep 17 00:00:00 2001 From: Maximilian Hils Date: Tue, 21 Mar 2023 19:07:27 +0100 Subject: [PATCH 1/4] fix #519 --- CHANGELOG.md | 1 + pdoc/extract.py | 2 + pdoc/templates/default/module.html.jinja2 | 2 +- test/testdata/misc.html | 102 ++++++++++++++-------- test/testdata/misc.py | 5 ++ test/testdata/misc.txt | 3 + 6 files changed, 79 insertions(+), 36 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8257728d..47d681cf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ - Add additional Jinja2 blocks to allow a more fine-grained customization of the menu. ([#521](https://github.com/mitmproxy/pdoc/pull/521), @mikkelakromann) + - Fix a crash in pdoc 13.0.0 when `__init__.py` is passed as a file to pdoc. ## 2023-02-19: pdoc 13.0.0 diff --git a/pdoc/extract.py b/pdoc/extract.py index 9fc0e511..28d60538 100644 --- a/pdoc/extract.py +++ b/pdoc/extract.py @@ -130,6 +130,8 @@ def parse_spec(spec: Path | str) -> str: ) if isinstance(spec, Path): + if spec.name == "__init__.py": + spec = spec.parent if (spec.parent / "__init__.py").exists(): return parse_spec(spec.resolve().parent) + f".{spec.stem}" parent_dir = str(spec.parent) diff --git a/pdoc/templates/default/module.html.jinja2 b/pdoc/templates/default/module.html.jinja2 index d35d6da6..b699cf72 100644 --- a/pdoc/templates/default/module.html.jinja2 +++ b/pdoc/templates/default/module.html.jinja2 @@ -238,7 +238,7 @@ See https://pdoc.dev/docs/pdoc/render_helpers.html#DefaultMacroExtension for an 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. #} - {% if doc.name == "__init__" and (doc.signature_without_self.parameters or doc.docstring) %} + {% if doc.name == "__init__" and (doc.docstring or (doc.kind == "function" and doc.signature_without_self.parameters)) %} {# show constructors that have a docstring or at least one extra argument #} true {% elif doc.name == "__doc__" %} diff --git a/test/testdata/misc.html b/test/testdata/misc.html index 23019c86..29b439ae 100644 --- a/test/testdata/misc.html +++ b/test/testdata/misc.html @@ -260,6 +260,12 @@

API Documentation

+
  • + __init__ + + +
  • @@ -681,41 +687,46 @@

    398 """Test for broken links for inherited methods, https://github.com/mitmproxy/pdoc/issues/490""" 399 400 -401__all__ = [ -402 "Issue226", -403 "var_with_default_obj", -404 "var_with_default_func", -405 "func_with_defaults", -406 "ClassmethodLink", -407 "GenericParent", -408 "NonGenericChild", -409 "Child", -410 "only_annotated", -411 "_Private", -412 "LambdaAttr", -413 "foo", -414 "bar", -415 "baz", -416 "qux", -417 "Indented", -418 "fun_with_protected_decorator", -419 "unhashable", -420 "AbstractClass", -421 "add_four", -422 "add_five", -423 "add_six", -424 "linkify_links", -425 "Issue352a", -426 "Issue352b", -427 "CustomCall", -428 "Headings", -429 "repr_not_syntax_highlightable", -430 "ClassDecorator", -431 "another_decorated_function", -432 "SubclassRef", -433 "ClassAsAttribute", -434 "scheduler", -435] +401class __init__: +402 """https://github.com/mitmproxy/pdoc/issues/519""" +403 +404 +405__all__ = [ +406 "Issue226", +407 "var_with_default_obj", +408 "var_with_default_func", +409 "func_with_defaults", +410 "ClassmethodLink", +411 "GenericParent", +412 "NonGenericChild", +413 "Child", +414 "only_annotated", +415 "_Private", +416 "LambdaAttr", +417 "foo", +418 "bar", +419 "baz", +420 "qux", +421 "Indented", +422 "fun_with_protected_decorator", +423 "unhashable", +424 "AbstractClass", +425 "add_four", +426 "add_five", +427 "add_six", +428 "linkify_links", +429 "Issue352a", +430 "Issue352b", +431 "CustomCall", +432 "Headings", +433 "repr_not_syntax_highlightable", +434 "ClassDecorator", +435 "another_decorated_function", +436 "SubclassRef", +437 "ClassAsAttribute", +438 "scheduler", +439 "__init__", +440] @@ -2181,6 +2192,27 @@

    Inherited Members
    +
    + +
    + + class + __init__: + + + +
    + +
    402class __init__:
    +403    """https://github.com/mitmproxy/pdoc/issues/519"""
    +
    + + + + + +
    \ No newline at end of file diff --git a/test/testdata/misc.py b/test/testdata/misc.py index 8a1dfc72..92174bb0 100644 --- a/test/testdata/misc.py +++ b/test/testdata/misc.py @@ -398,6 +398,10 @@ class scheduler(sched.scheduler): """Test for broken links for inherited methods, https://github.com/mitmproxy/pdoc/issues/490""" +class __init__: + """https://github.com/mitmproxy/pdoc/issues/519""" + + __all__ = [ "Issue226", "var_with_default_obj", @@ -432,4 +436,5 @@ class scheduler(sched.scheduler): "SubclassRef", "ClassAsAttribute", "scheduler", + "__init__", ] diff --git a/test/testdata/misc.txt b/test/testdata/misc.txt index c2501832..85b53152 100644 --- a/test/testdata/misc.txt +++ b/test/testdata/misc.txt @@ -114,4 +114,7 @@ > + + > > \ No newline at end of file From 792b0743a396eb982d70a76dded74eca8db152b3 Mon Sep 17 00:00:00 2001 From: "autofix-ci[bot]" <114827586+autofix-ci[bot]@users.noreply.github.com> Date: Tue, 21 Mar 2023 18:08:31 +0000 Subject: [PATCH 2/4] [autofix.ci] apply automated fixes --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 47d681cf..2c510e8b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ - Add additional Jinja2 blocks to allow a more fine-grained customization of the menu. ([#521](https://github.com/mitmproxy/pdoc/pull/521), @mikkelakromann) - Fix a crash in pdoc 13.0.0 when `__init__.py` is passed as a file to pdoc. + ([#522](https://github.com/mitmproxy/pdoc/pull/522), @mhils) ## 2023-02-19: pdoc 13.0.0 From def637585137d5808965c9e1838b16eb5a96c6e9 Mon Sep 17 00:00:00 2001 From: Maximilian Hils Date: Tue, 21 Mar 2023 19:16:40 +0100 Subject: [PATCH 3/4] tests++ --- test/test_extract.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/test/test_extract.py b/test/test_extract.py index ea239743..d9d99827 100644 --- a/test/test_extract.py +++ b/test/test_extract.py @@ -81,6 +81,13 @@ def test_parse_spec(monkeypatch): assert str(here / "testdata") in sys.path sys.path = p + assert ( + parse_spec(here / "testdata" / "demopackage" / "__init__.py") + == "demopackage" + ) + assert str(here / "testdata") in sys.path + sys.path = p + def test_parse_spec_mod_and_dir(tmp_path, monkeypatch): """Test that we display a warning when both a module and a local directory exist with the provided name.""" From 1bea1e11dfdc741079f4eb416d559e09de329075 Mon Sep 17 00:00:00 2001 From: "autofix-ci[bot]" <114827586+autofix-ci[bot]@users.noreply.github.com> Date: Tue, 21 Mar 2023 18:18:21 +0000 Subject: [PATCH 4/4] [autofix.ci] apply automated fixes --- test/test_extract.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/test/test_extract.py b/test/test_extract.py index d9d99827..0ecd1501 100644 --- a/test/test_extract.py +++ b/test/test_extract.py @@ -82,8 +82,7 @@ def test_parse_spec(monkeypatch): sys.path = p assert ( - parse_spec(here / "testdata" / "demopackage" / "__init__.py") - == "demopackage" + parse_spec(here / "testdata" / "demopackage" / "__init__.py") == "demopackage" ) assert str(here / "testdata") in sys.path sys.path = p