-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1152 from procrastinate-org/sphinx-autodoc
- Loading branch information
Showing
14 changed files
with
129 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# Document my tasks with Sphinx & Autodoc | ||
|
||
If you use Sphinx's `autodoc` extension to document your project, you might | ||
have noticed that your tasks are absent from the documentation. This is because | ||
when you apply the `@app.task` decorator, you're actually replacing the | ||
function with a Procrastinate Task object, which `autodoc` doesn't know how to | ||
process. | ||
|
||
Procrastinate provides a small Sphinx extension to fix that. You may want to | ||
ensure procrastinate is installed with the `sphinx` extra in the environment | ||
where you build your doc. This is not mandatory, as it only adds sphinx itself | ||
as a dependency, but if the extension ever needs other dependencies in the | ||
future, they will be installed through the `sphinx` extra as well. | ||
|
||
```bash | ||
$ pip install procrastinate[sphinx] | ||
``` | ||
|
||
Then, add the following to your `conf.py`: | ||
|
||
```python | ||
extensions = [ | ||
# ... | ||
"procrastinate.contrib.sphinx", | ||
# ... | ||
] | ||
``` | ||
|
||
That's it. Your tasks will now be picked up by `autodoc` and included in your | ||
documentation. |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
from __future__ import annotations | ||
|
||
from typing import Any | ||
|
||
from sphinx.application import Sphinx | ||
from sphinx.ext.autodoc import FunctionDocumenter | ||
|
||
from procrastinate import tasks | ||
|
||
|
||
class ProcrastinateTaskDocumenter(FunctionDocumenter): | ||
objtype = "procrastinate_task" | ||
directivetype = "function" | ||
member_order = 40 | ||
|
||
@classmethod | ||
def can_document_member( | ||
cls, | ||
member: Any, | ||
membername: str, | ||
isattr: bool, | ||
parent: Any, | ||
) -> bool: | ||
return isinstance(member, tasks.Task) | ||
|
||
|
||
def setup(app: Sphinx): | ||
app.setup_extension("sphinx.ext.autodoc") # Require autodoc extension | ||
|
||
app.add_autodocumenter(ProcrastinateTaskDocumenter) | ||
|
||
return { | ||
"version": "1", | ||
"parallel_read_safe": True, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
from __future__ import annotations | ||
|
||
extensions = [ | ||
"sphinx.ext.autodoc", | ||
"procrastinate.contrib.sphinx", | ||
] | ||
|
||
buildername = "html" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
Tasks | ||
===== | ||
|
||
.. automodule:: procrastinate_demos.demo_async.tasks | ||
:members: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
from __future__ import annotations | ||
|
||
import pytest | ||
from sphinx.testing.path import path | ||
|
||
|
||
@pytest.fixture | ||
def rootdir(): | ||
return path(__file__).parent | ||
|
||
|
||
@pytest.fixture | ||
def sphinx_app(app_params, make_app): | ||
""" | ||
Provides the 'sphinx.application.Sphinx' object | ||
""" | ||
args, kwargs = app_params | ||
return make_app(*args, **kwargs) | ||
|
||
|
||
@pytest.mark.sphinx(buildername="html", testroot="root") | ||
def test_autodoc_task(sphinx_app): | ||
sphinx_app.build() | ||
content = (sphinx_app.outdir / "index.html").read_text() | ||
# Check that the docstring of one of the task appears in the generated documentation | ||
assert "Sum two numbers." in content |