Skip to content

Commit

Permalink
Gracefully handle no objects being rendered
Browse files Browse the repository at this point in the history
Closes #448
  • Loading branch information
AWhetter committed Jul 20, 2024
1 parent d2a9675 commit acad72b
Show file tree
Hide file tree
Showing 9 changed files with 89 additions and 7 deletions.
10 changes: 10 additions & 0 deletions autoapi/_mapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,16 @@ def _output_top_rst(self):
# Render Top Index
top_level_index = os.path.join(self.dir_root, "index.rst")
pages = [obj for obj in self.objects_to_render.values() if obj.display]
if not pages:
msg = (
"No modules were rendered. "
"Do you need to set autoapi_options to render additional objects?"
)
LOGGER.warning(
msg, type="autoapi", subtype="nothing_rendered"
)
return

with open(top_level_index, "wb") as top_level_file:
content = self.jinja_env.get_template("index.rst")
top_level_file.write(content.render(pages=pages).encode("utf-8"))
Expand Down
3 changes: 3 additions & 0 deletions autoapi/extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,9 @@ def build_finished(app, exception):
normalized_root = os.path.normpath(
os.path.join(app.srcdir, app.config.autoapi_root)
)
if not os.path.exists(normalized_root):
return

if app.verbosity > 1:
LOGGER.info(
colorize("bold", "[AutoAPI] ")
Expand Down
1 change: 1 addition & 0 deletions docs/changes/448.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Gracefully handle no objects being rendered
20 changes: 13 additions & 7 deletions docs/reference/config.rst
Original file line number Diff line number Diff line change
Expand Up @@ -273,13 +273,19 @@ Suppressing Warnings
If narrower suppression is wanted, the available subtypes for AutoAPI are:

* python_import_resolution
Used if resolving references to objects in an imported module failed. Potential reasons
include cyclical imports and missing (parent) modules.
* not_readable
Emitted if processing (opening, parsing, ...) an input file failed.
* toc_reference
Used if a reference to an entry in a table of content cannot be resolved.
* ``python_import_resolution``:
Emitted if resolving references to objects in an imported module failed.
Potential reasons include cyclical imports and missing (parent) modules.
* ``not_readable``:
Emitted if processing (opening, parsing, ...) an input file failed.
* ``toc_reference``:
Emitted if a reference to an entry in a table of content cannot be resolved.
* ``nothing_rendered``:
Emitted if nothing was found to be documented.
Potential reasons include no files being found in :confval:`autoapi_dirs`
that match :confval:`autoapi_file_patterns`,
or all discovered modules and objects being excluded from rendering due to
:confval:`autoapi_options` or :ref:`other rendering exclusions <customise-documented-api>`.

So if all AutoAPI warnings concerning unreadable sources and failing Python imports should be
filtered, but all other warnings should not, the option would be
Expand Down
22 changes: 22 additions & 0 deletions tests/python/pynorender/conf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
templates_path = ["_templates"]
source_suffix = ".rst"
master_doc = "index"
project = "pyexample"
copyright = "2015, readthedocs"
author = "readthedocs"
version = "0.1"
release = "0.1"
language = "en"
exclude_patterns = ["_build"]
pygments_style = "sphinx"
todo_include_todos = False
html_theme = "alabaster"
htmlhelp_basename = "pyexampledoc"
extensions = ["sphinx.ext.autodoc", "autoapi.extension"]
autoapi_dirs = ["example"]
autoapi_python_class_content = "both"
autoapi_options = [
"members",
"imported-members",
"inherited-members",
]
7 changes: 7 additions & 0 deletions tests/python/pynorender/example/_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
def func() -> int:
"""Returns five."""
return 5


class Foo:
"""This is a class."""
25 changes: 25 additions & 0 deletions tests/python/pynorender/index.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
.. pyexample documentation master file, created by
sphinx-quickstart on Fri May 29 13:34:37 2015.
You can adapt this file completely to your liking, but it should at least
contain the root `toctree` directive.
Welcome to pyexample's documentation!
=====================================

.. toctree::

autoapi/index

Contents:

.. toctree::
:maxdepth: 2


Indices and tables
==================

* :ref:`genindex`
* :ref:`modindex`
* :ref:`search`

7 changes: 7 additions & 0 deletions tests/python/test_pyintegration.py
Original file line number Diff line number Diff line change
Expand Up @@ -1195,3 +1195,10 @@ def test_line_number_order(self, parse):
method_sphinx_docs = example_file.find(id="example.Foo.method_sphinx_docs")

assert method_tricky.sourceline < method_sphinx_docs.sourceline


def test_nothing_to_render_raises_warning(builder):
with pytest.raises(sphinx.errors.SphinxWarning) as exc_info:
builder("pynorender", warningiserror=True)

assert "No modules were rendered" in str(exc_info.value)
1 change: 1 addition & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ python =
3.13: py313

[testenv]
usedevelop = True
deps =
beautifulsoup4
pytest
Expand Down

0 comments on commit acad72b

Please sign in to comment.