From da412c63e3ad79608adb95f076b3e8ed8865524d Mon Sep 17 00:00:00 2001 From: David Selassie Date: Wed, 10 Jan 2024 13:04:06 -0800 Subject: [PATCH] Renders nested exceptions in classes It looks like when enumerating through the nested children of a class, the code also needs to list out exceptions. Adds test. Should fix what I found in https://github.com/sphinx-extensions2/sphinx-autodoc2/issues/51 --- src/autodoc2/render/myst_.py | 2 +- src/autodoc2/render/rst_.py | 2 +- tests/test_render.py | 3 +++ tests/test_render/test_basic_myst_.md | 16 ++++++++++++++++ tests/test_render/test_basic_rst_.rst | 11 +++++++++++ 5 files changed, 32 insertions(+), 2 deletions(-) diff --git a/src/autodoc2/render/myst_.py b/src/autodoc2/render/myst_.py index 39b1623..77fc5f5 100644 --- a/src/autodoc2/render/myst_.py +++ b/src/autodoc2/render/myst_.py @@ -259,7 +259,7 @@ def render_class(self, item: ItemData) -> t.Iterable[str]: lines.extend(["```", ""]) for child in self.get_children( - item, {"class", "property", "attribute", "method"} + item, {"class", "property", "attribute", "method", "exception"} ): if ( child["full_name"].endswith(".__init__") diff --git a/src/autodoc2/render/rst_.py b/src/autodoc2/render/rst_.py index 1a196f9..d041088 100644 --- a/src/autodoc2/render/rst_.py +++ b/src/autodoc2/render/rst_.py @@ -239,7 +239,7 @@ def render_class(self, item: ItemData) -> t.Iterable[str]: yield "" for child in self.get_children( - item, {"class", "property", "attribute", "method"} + item, {"class", "property", "attribute", "method", "exception"} ): if ( child["full_name"].endswith(".__init__") diff --git a/tests/test_render.py b/tests/test_render.py index 219699d..ddf1fa3 100644 --- a/tests/test_render.py +++ b/tests/test_render.py @@ -258,6 +258,9 @@ def prop(self) -> alias | None: class Nested: '''This is a nested class.''' + + class NestedEx(Exception): + '''This is a nested exception.''' """ ), "utf-8", diff --git a/tests/test_render/test_basic_myst_.md b/tests/test_render/test_basic_myst_.md index 11f0601..aa05a64 100644 --- a/tests/test_render/test_basic_myst_.md +++ b/tests/test_render/test_basic_myst_.md @@ -129,4 +129,20 @@ package.a ```` +````{py:exception} NestedEx() +:canonical: package.Class.NestedEx + +Bases: {py:obj}`Exception` + +```{autodoc2-docstring} package.Class.NestedEx +``` + +```{rubric} Initialization +``` + +```{autodoc2-docstring} package.Class.NestedEx.__init__ +``` + +```` + ````` diff --git a/tests/test_render/test_basic_rst_.rst b/tests/test_render/test_basic_rst_.rst index e1c7c45..15e3de5 100644 --- a/tests/test_render/test_basic_rst_.rst +++ b/tests/test_render/test_basic_rst_.rst @@ -101,3 +101,14 @@ API :canonical: package.Class.Nested .. autodoc2-docstring:: package.Class.Nested + + .. py:exception:: NestedEx() + :canonical: package.Class.NestedEx + + Bases: :py:obj:`Exception` + + .. autodoc2-docstring:: package.Class.NestedEx + + .. rubric:: Initialization + + .. autodoc2-docstring:: package.Class.NestedEx.__init__