Skip to content

Commit 3097fd9

Browse files
therveci.datadog-api-spec
andauthored
Make better docstrings (#1012)
* Improve docstring rendering Render docstrings from markdown to rest, and change sphinx output to include init docs. * More docs changes * pre-commit fixes * Try to improve sphinx cache * Fix restore keys * Use repo commit as key * Try to download site * Doesn't work * Rebase * Remove duplicated model docs * Escape line breaks * Remove nested blocks * Rebase * Rebase Co-authored-by: ci.datadog-api-spec <[email protected]>
1 parent 42cd2ad commit 3097fd9

File tree

257 files changed

+1421
-2209
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

257 files changed

+1421
-2209
lines changed

.generator/poetry.lock

Lines changed: 47 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.generator/pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ jinja2 = "3.0.3"
1414
pytest = "^7.1.1"
1515
pytest-bdd = "^5.0.0"
1616
python-dateutil = "2.8.2"
17+
m2r2 = "0.3.2"
1718

1819
[tool.poetry.dev-dependencies]
1920

.generator/src/generator/cli.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ def cli(specs, output):
3636
env.filters["parameters"] = openapi.parameters
3737
env.filters["return_type"] = openapi.return_type
3838
env.filters["safe_snake_case"] = openapi.safe_snake_case
39+
env.filters["docstring"] = formatter.docstring
3940

4041
env.globals["enumerate"] = enumerate
4142
env.globals["package"] = PACKAGE_NAME
@@ -112,12 +113,14 @@ def cli(specs, output):
112113
with models_path.open("w") as fp:
113114
fp.write(models_j2.render(models=sorted(models)))
114115

116+
tags_by_name = {tag["name"]: tag for tag in spec["tags"]}
117+
115118
for name, operations in apis.items():
116119
filename = openapi.safe_snake_case(name) + "_api.py"
117120
api_path = package / "api" / filename
118121
api_path.parent.mkdir(parents=True, exist_ok=True)
119122
with api_path.open("w") as fp:
120-
fp.write(api_j2.render(name=name, operations=operations))
123+
fp.write(api_j2.render(name=name, operations=operations, description=tags_by_name[name]["description"]))
121124

122125
api_init_path = package / "api" / "__init__.py"
123126
with api_init_path.open("w") as fp:

.generator/src/generator/formatter.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
import keyword
33
import re
44

5+
import m2r2
6+
57
KEYWORDS = set(keyword.kwlist)
68
KEYWORDS.add("property")
79

@@ -53,3 +55,17 @@ def get_name(schema):
5355

5456
def attribute_path(attribute):
5557
return ".".join(attribute_name(a) for a in attribute.split("."))
58+
59+
60+
class CustomRenderer(m2r2.RestRenderer):
61+
def double_emphasis(self, text):
62+
if "``" in text:
63+
text = text.replace("\ ``", "").replace("``\ ", "")
64+
return super().double_emphasis(text)
65+
66+
def header(self, text, level, raw=None):
67+
return "\n{}\n".format(self.double_emphasis(text))
68+
69+
70+
def docstring(text):
71+
return m2r2.convert(text.replace("\\n", "\\\\n"), renderer=CustomRenderer())[1:-1]

.generator/src/generator/templates/api.j2

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ from {{ package }}.{{ version }}.model.{{ model|safe_snake_case }} import {{ mod
1616

1717
{% set classname = name.replace(" ", "") + "Api" %}
1818
class {{ classname }}:
19-
19+
"""
20+
{{ description|docstring|indent(4) }}
21+
"""
2022
def __init__(self, api_client=None):
2123
if api_client is None:
2224
api_client = ApiClient()
@@ -135,7 +137,7 @@ class {{ classname }}:
135137
def {{ operation.operationId|safe_snake_case }}(self, {% for name, parameter in operation|parameters if parameter.required %}{{name|attribute_name}}, {% endfor %}**kwargs):
136138
"""{{ operation.summary|indent(8) }}.
137139
{% if operation.description %}
138-
{{ operation.description.replace("\\", "\\\\")|indent(8) }}
140+
{{ operation.description|docstring|indent(8) }}
139141
{% endif %}
140142
This method makes a synchronous HTTP request by default. To make an
141143
asynchronous HTTP request, please pass async_req=True.
@@ -147,12 +149,12 @@ class {{ classname }}:
147149

148150
{%- for name, parameter in operation|parameters if parameter.required %}
149151
{%- if parameter.description %}
150-
:param {{ name|attribute_name }}: {{ parameter.description|indent(12) }}{% if parameter.default %} Defaults to {{ parameter.default }}.{% endif %}{% endif %}
152+
:param {{ name|attribute_name }}: {{ parameter.description|docstring|indent(12) }}{% if parameter.default %} Defaults to {{ parameter.default }}.{% endif %}{% endif %}
151153
:type {{ name|attribute_name }}: {{ get_type_for_parameter(parameter) }}
152154
{%- endfor %}
153155
{%- for name, parameter in operation|parameters if not parameter.required %}
154156
{%- if parameter.description %}
155-
:param {{ name|attribute_name }}: {{ parameter.description|indent(12) }}{%- if parameter.default %} If omitted the server will use the default value of {{ parameter.default }}.{% endif %}{% endif %}
157+
:param {{ name|attribute_name }}: {{ parameter.description|docstring|indent(12) }}{%- if parameter.default %} If omitted the server will use the default value of {{ parameter.default }}.{% endif %}{% endif %}
156158
:type {{ name|attribute_name }}: {{ get_type_for_parameter(parameter) }}, optional
157159
{%- endfor %}
158160
:param _return_http_data_only: Response data without head status
@@ -197,12 +199,12 @@ class {{ classname }}:
197199
{# keep new line #}
198200
{%- for name, parameter in operation|parameters if parameter.required %}
199201
{%- if parameter.description %}
200-
:param {{ name|attribute_name }}: {{ parameter.description|indent(12) }}{% if parameter.default %} Defaults to {{ parameter.default }}.{% endif %}{% endif %}
202+
:param {{ name|attribute_name }}: {{ parameter.description|docstring|indent(12) }}{% if parameter.default %} Defaults to {{ parameter.default }}.{% endif %}{% endif %}
201203
:type {{ name|attribute_name }}: {{ get_type_for_parameter(parameter) }}
202204
{%- endfor %}
203205
{%- for name, parameter in operation|parameters if not parameter.required %}
204206
{%- if parameter.description %}
205-
:param {{ name|attribute_name }}: {{ parameter.description|indent(12) }}{%- if parameter.default %} If omitted the server will use the default value of {{ parameter.default }}.{% endif %}{% endif %}
207+
:param {{ name|attribute_name }}: {{ parameter.description|docstring|indent(12) }}{%- if parameter.default %} If omitted the server will use the default value of {{ parameter.default }}.{% endif %}{% endif %}
206208
:type {{ name|attribute_name }}: {{ get_type_for_parameter(parameter) }}, optional
207209
{%- endfor %}
208210
:param _request_timeout: Timeout setting for this request. If one

.generator/src/generator/templates/model_generic.j2

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,10 +110,10 @@ class {{ name }}(ModelNormal):
110110
{%- if attr in model.get("required", []) and (definition.get("default") == None or definition.enum) %} {{ attr|attribute_name }},{% endif %}
111111
{%- endfor %} *args, **kwargs):
112112
"""
113-
{{ model.description.replace("\\", "\\\\")|indent(8) }}
113+
{{ model.description|docstring|indent(8) }}
114114
{%- for attr, definition in model.get("properties", {}).items() %}
115115
{# keep new line #}
116-
:param {{ attr|attribute_name }}: {{ definition.description|indent(12) }}
116+
:param {{ attr|attribute_name }}: {{ definition.description|docstring|indent(12) }}
117117
:type {{ attr|attribute_name }}: {{ get_type_for_attribute(model, attr, current_name=name) }}{% if definition.nullable %}, none_type{% endif %}{% if attr not in model.get("required", []) %}, optional{% endif %}
118118
{%- endfor %}
119119
"""

.generator/src/generator/templates/model_oneof.j2

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class {{ name }}(ModelComposed):
1717

1818
def __init__(self, *args, **kwargs):
1919
"""
20-
{{ model.description|indent(8) }}
20+
{{ model.description|docstring|indent(8) }}
2121
{%- for attr, definition, schema in get_oneof_parameters(model) %}
2222
{# keep new line #}
2323
:param {{ attr|attribute_name }}: {{ definition.description|indent(12) }}

.generator/src/generator/templates/model_simple.j2

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,13 @@ class {{ name }}(ModelSimple):
6464

6565
def __init__(self, *args, **kwargs):
6666
"""
67-
{{ model.description|indent(8) }}
67+
{{ model.description|docstring|indent(8) }}
6868

6969
Note that value can be passed either in args or in kwargs, but not in both.
70-
71-
:param value: {{ model.description }}{%- if "default" in model and model.default != None %} If omitted defaults to "{{ model.default }}".{%- endif %}
70+
{# empty line #}
71+
{%- if "default" in model and model.default != None %}
72+
:param value: If omitted defaults to "{{ model.default }}".
73+
{%- endif %}
7274
{%- if ref %}
7375
:type value: [{{ ref }}]
7476
{%- else %}

.github/workflows/docs.yml

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,21 @@ jobs:
2929
- name: Install tox
3030
run: pip install tox
3131

32+
- name: Set spec commit
33+
id: set_commit
34+
run: |
35+
content=`cat .apigentools-info`
36+
content="${content//'%'/'%25'}"
37+
content="${content//$'\n'/'%0A'}"
38+
content="${content//$'\r'/'%0D'}"
39+
echo "::set-output name=spec_json::$content"
40+
3241
- name: Cache sphinx
33-
uses: actions/cache@v2
42+
uses: actions/cache@v3
3443
with:
3544
path: docs/.sphinx
36-
key: sphinx
45+
key: sphinx-${{ fromJson(steps.set_commit.outputs.spec_json).spec_versions.v2.spec_repo_commit }}
46+
restore-keys: sphinx-
3747

3848
- name: Build documentation
3949
run: tox -e docs

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ repos:
2424
name: Generate API docs
2525
stages: [manual]
2626
language: python
27-
entry: sphinx-apidoc --output-dir docs --force --no-toc --templatedir docs/_templates src/datadog_api_client *apis *models *version.py
27+
entry: bash -c "SPHINX_APIDOC_OPTIONS=members,show-inheritance sphinx-apidoc --output-dir docs --force --no-toc --templatedir docs/_templates src/datadog_api_client *apis *models *version.py"
2828
pass_filenames: false
2929
additional_dependencies:
3030
- Sphinx

0 commit comments

Comments
 (0)