Skip to content

Commit baac169

Browse files
committed
fix tests
1 parent 70b02be commit baac169

File tree

4 files changed

+70
-56
lines changed

4 files changed

+70
-56
lines changed

.flake8

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,5 @@ ignore =
88
W503,
99
# redefinition of unused function name
1010
F811
11+
exclude =
12+
test_*

quartodoc/renderers/md_renderer.py

Lines changed: 50 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ def _has_attr_section(el: dc.Docstring | None):
2727
return any([isinstance(x, ds.DocstringSectionAttributes) for x in el.parsed])
2828

2929

30-
3130
class MdRenderer(Renderer):
3231
"""Render docstrings to markdown.
3332
@@ -66,8 +65,7 @@ def __init__(
6665
show_signature_annotations: bool = False,
6766
display_name: str = "relative",
6867
hook_pre=None,
69-
use_interlinks = False,
70-
68+
use_interlinks=False,
7169
):
7270
self.header_level = header_level
7371
self.show_signature = show_signature
@@ -79,14 +77,13 @@ def __init__(
7977
self.crnt_header_level = self.header_level
8078

8179
@contextmanager
82-
def _increment_header(self, n = 1):
80+
def _increment_header(self, n=1):
8381
self.crnt_header_level += n
8482
try:
8583
yield
86-
finally:
84+
finally:
8785
self.crnt_header_level -= n
8886

89-
9087
def _fetch_object_dispname(self, el: "dc.Alias | dc.Object"):
9188
# TODO: copied from Builder, should move into util function
9289
if self.display_name == "name":
@@ -101,13 +98,12 @@ def _fetch_object_dispname(self, el: "dc.Alias | dc.Object"):
10198
return el.canonical_path
10299

103100
raise ValueError(f"Unsupported display_name: `{self.display_name}`")
104-
101+
105102
def _render_table(self, rows, headers):
106103
table = tabulate(rows, headers=headers, tablefmt="github")
107104

108105
return table
109106

110-
111107
# render_annotation method --------------------------------------------------------
112108

113109
@dispatch
@@ -119,11 +115,11 @@ def render_annotation(self, el: str) -> str:
119115
An object representing a type annotation.
120116
"""
121117
return el
122-
118+
123119
@dispatch
124120
def render_annotation(self, el: None) -> str:
125121
return ""
126-
122+
127123
@dispatch
128124
def render_annotation(self, el: expr.Name) -> str:
129125
# TODO: maybe there is a way to get tabulate to handle this?
@@ -142,18 +138,21 @@ def signature(self, el: dc.Alias, source: Optional[dc.Alias] = None):
142138
return self.signature(el.target, el)
143139

144140
@dispatch
145-
def signature(self, el: Union[dc.Class, dc.Function], source: Optional[dc.Alias] = None):
141+
def signature(
142+
self, el: Union[dc.Class, dc.Function], source: Optional[dc.Alias] = None
143+
):
146144
name = self._fetch_object_dispname(source or el)
147145
pars = self.render(el.parameters)
148146

149147
return f"`{name}({pars})`"
150148

151149
@dispatch
152-
def signature(self, el: Union[dc.Module, dc.Attribute], source: Optional[dc.Alias] = None):
150+
def signature(
151+
self, el: Union[dc.Module, dc.Attribute], source: Optional[dc.Alias] = None
152+
):
153153
name = self._fetch_object_dispname(source or el)
154154
return f"`{name}`"
155155

156-
157156
@dispatch
158157
def render_header(self, el: layout.Doc):
159158
"""Render the header of a docstring, including any anchors."""
@@ -164,7 +163,6 @@ def render_header(self, el: layout.Doc):
164163
_anchor = f"{{ #{el.obj.path} }}"
165164
return f"{'#' * self.crnt_header_level} {_str_dispname} {_anchor}"
166165

167-
168166
# render method -----------------------------------------------------------
169167

170168
@dispatch
@@ -199,7 +197,7 @@ def render(self, el: layout.Section):
199197
body = list(map(self.render, el.contents))
200198

201199
return "\n\n".join([section_top, *body])
202-
200+
203201
@dispatch
204202
def render(self, el: layout.Interlaced):
205203
# render a sequence of objects with like-sections together.
@@ -221,7 +219,6 @@ def render(self, el: layout.Interlaced):
221219
if first_doc.obj.docstring is None:
222220
raise ValueError("The first element of Interlaced must have a docstring.")
223221

224-
225222
str_title = self.render_header(first_doc)
226223
str_sig = "\n\n".join(map(self.signature, objs))
227224
str_body = []
@@ -265,7 +262,6 @@ def render(self, el: Union[layout.DocClass, layout.DocModule]):
265262
raw_meths = [x for x in el.members if x.obj.is_function]
266263
raw_classes = [x for x in el.members if x.obj.is_class]
267264

268-
269265
header = "| Name | Description |\n| --- | --- |"
270266

271267
# attribute summary table ----
@@ -274,16 +270,16 @@ def render(self, el: Union[layout.DocClass, layout.DocModule]):
274270
# TODO: for now, we skip making an attribute table on classes, unless
275271
# they contain an attributes section in the docstring
276272
if (
277-
raw_attrs
278-
and not _has_attr_section(el.obj.docstring)
279-
# TODO: what should backwards compat be?
280-
# and not isinstance(el, layout.DocClass)
281-
):
273+
raw_attrs
274+
and not _has_attr_section(el.obj.docstring)
275+
# TODO: what should backwards compat be?
276+
# and not isinstance(el, layout.DocClass)
277+
):
282278

283279
_attrs_table = "\n".join(map(self.summarize, raw_attrs))
284280
attrs = f"{sub_header} Attributes\n\n{header}\n{_attrs_table}"
285281
attr_docs.append(attrs)
286-
282+
287283
# classes summary table ----
288284
if raw_classes:
289285
_summary_table = "\n".join(map(self.summarize, raw_classes))
@@ -293,22 +289,29 @@ def render(self, el: Union[layout.DocClass, layout.DocModule]):
293289

294290
n_incr = 1 if el.flat else 2
295291
with self._increment_header(n_incr):
296-
class_docs.extend([self.render(x) for x in raw_classes if isinstance(x, layout.Doc)])
292+
class_docs.extend(
293+
[
294+
self.render(x)
295+
for x in raw_classes
296+
if isinstance(x, layout.Doc)
297+
]
298+
)
297299

298300
# method summary table ----
299301
if raw_meths:
300302
_summary_table = "\n".join(map(self.summarize, raw_meths))
301303
section_name = (
302-
"Methods" if isinstance(el, layout.DocClass)
303-
else "Functions"
304+
"Methods" if isinstance(el, layout.DocClass) else "Functions"
304305
)
305306
objs = f"{sub_header} {section_name}\n\n{header}\n{_summary_table}"
306307
meth_docs.append(objs)
307308

308309
# TODO use context manager, or context variable?
309310
n_incr = 1 if el.flat else 2
310311
with self._increment_header(n_incr):
311-
meth_docs.extend([self.render(x) for x in raw_meths if isinstance(x, layout.Doc)])
312+
meth_docs.extend(
313+
[self.render(x) for x in raw_meths if isinstance(x, layout.Doc)]
314+
)
312315

313316
body = self.render(el.obj)
314317
return "\n\n".join([title, body, *attr_docs, *class_docs, *meth_docs])
@@ -366,19 +369,24 @@ def render(self, el: dc.Parameters):
366369

367370
# index for final positionly only args (via /)
368371
try:
369-
pos_only = max([ii for ii, el in enumerate(el) if el.kind == dc.ParameterKind.positional_only])
372+
pos_only = max(
373+
[
374+
ii
375+
for ii, el in enumerate(el)
376+
if el.kind == dc.ParameterKind.positional_only
377+
]
378+
)
370379
except ValueError:
371380
pos_only = None
372381

373-
374382
pars = list(map(self.render, el))
375383

376384
# insert a single `*,` argument to represent the shift to kw only arguments,
377385
# only if the shift to kw_only was not triggered by *args (var_positional)
378386
if (
379387
kw_only is not None
380388
and kw_only > 0
381-
and el[kw_only-1].kind != dc.ParameterKind.var_positional
389+
and el[kw_only - 1].kind != dc.ParameterKind.var_positional
382390
):
383391
pars.insert(kw_only, sanitize("*"))
384392

@@ -461,7 +469,7 @@ def render(self, el: ds.DocstringAttribute):
461469
row = [
462470
sanitize(el.name),
463471
self.render_annotation(el.annotation),
464-
sanitize(el.description or "", allow_markdown=True)
472+
sanitize(el.description or "", allow_markdown=True),
465473
]
466474
return row
467475

@@ -530,7 +538,6 @@ def render(self, el: Union[ds.DocstringReturn, ds.DocstringRaise]):
530538
def render(self, el):
531539
raise NotImplementedError(f"{type(el)}")
532540

533-
534541
# Summarize ===============================================================
535542
# this method returns a summary description, such as a table summarizing a
536543
# layout.Section, or a row in the table for layout.Page or layout.DocFunction.
@@ -569,14 +576,16 @@ def summarize(self, el: layout.Section):
569576

570577
str_func_table = "\n".join([thead, *rendered])
571578
return f"{header}\n\n{str_func_table}"
572-
579+
573580
return header
574581

575582
@dispatch
576583
def summarize(self, el: layout.Page):
577584
if el.summary is not None:
578585
# TODO: assumes that files end with .qmd
579-
return self._summary_row(f"[{el.summary.name}]({el.path}.qmd)", el.summary.desc)
586+
return self._summary_row(
587+
f"[{el.summary.name}]({el.path}.qmd)", el.summary.desc
588+
)
580589

581590
if len(el.contents) > 1 and not el.flatten:
582591
raise ValueError(
@@ -591,16 +600,18 @@ def summarize(self, el: layout.Page):
591600
@dispatch
592601
def summarize(self, el: layout.MemberPage):
593602
# TODO: model should validate these only have a single entry
594-
return self.summarize(el.contents[0], el.path, shorten = True)
595-
603+
return self.summarize(el.contents[0], el.path, shorten=True)
604+
596605
@dispatch
597606
def summarize(self, el: layout.Interlaced, *args, **kwargs):
598607
rows = [self.summarize(doc, *args, **kwargs) for doc in el.contents]
599608

600609
return "\n".join(rows)
601610

602611
@dispatch
603-
def summarize(self, el: layout.Doc, path: Optional[str] = None, shorten: bool = False):
612+
def summarize(
613+
self, el: layout.Doc, path: Optional[str] = None, shorten: bool = False
614+
):
604615
if path is None:
605616
link = f"[{el.name}](#{el.anchor})"
606617
else:
@@ -612,8 +623,8 @@ def summarize(self, el: layout.Doc, path: Optional[str] = None, shorten: bool =
612623

613624
@dispatch
614625
def summarize(self, el: layout.Link):
615-
description = self.summarize(el.obj)
616-
return self._summary_row(f"[](`{el.name}`)", description)
626+
description = self.summarize(el.obj)
627+
return self._summary_row(f"[](`{el.name}`)", description)
617628

618629
@dispatch
619630
def summarize(self, obj: Union[dc.Object, dc.Alias]) -> str:

quartodoc/tests/__snapshots__/test_renderers.ambr

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@
1212

1313
## Parameters
1414

15-
| Name | Type | Description | Default |
16-
|--------|--------|----------------------|------------|
17-
| `x` | str | Uses signature type. | _required_ |
18-
| `y` | int | Uses manual type. | _required_ |
15+
| Name | Type | Description | Default |
16+
|--------|--------------|----------------------|------------|
17+
| `x` | [str](`str`) | Uses signature type. | _required_ |
18+
| `y` | [int](`int`) | Uses manual type. | _required_ |
1919

2020
## Attributes
2121

@@ -51,10 +51,10 @@
5151

5252
## Parameters
5353

54-
| Name | Type | Description | Default |
55-
|--------|--------|----------------------|------------|
56-
| `x` | str | Uses signature type. | _required_ |
57-
| `y` | int | Uses manual type. | _required_ |
54+
| Name | Type | Description | Default |
55+
|--------|--------------|----------------------|------------|
56+
| `x` | [str](`str`) | Uses signature type. | _required_ |
57+
| `y` | [int](`int`) | Uses manual type. | _required_ |
5858

5959
## Attributes
6060

@@ -87,11 +87,11 @@
8787

8888
## Attributes
8989

90-
| Name | Type | Description |
91-
|--------|--------|---------------------|
92-
| x | str | Uses signature type |
93-
| y | int | Uses manual type |
94-
| z | float | Defined in init |
90+
| Name | Type | Description |
91+
|--------|------------------|---------------------|
92+
| x | [str](`str`) | Uses signature type |
93+
| y | [int](`int`) | Uses manual type |
94+
| z | [float](`float`) | Defined in init |
9595
'''
9696
# ---
9797
# name: test_render_doc_module[embedded]

quartodoc/tests/test_renderers.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ def test_render_param_kwargs_annotated():
2525
res = renderer.render(f.parameters)
2626

2727
assert (
28-
res
29-
== "a: \[int\](`int`), b: \[int\](`int`) = 1, *args: \[list\](`list`)\[\[str\](`str`)\], c: \[int\](`int`), d: \[int\](`int`), **kwargs: \[dict\](`dict`)\[\[str\](`str`), \[str\](`str`)\]"
28+
res # noqa: W605
29+
== "a: \[int\](`int`), b: \[int\](`int`) = 1, *args: \[list\](`list`)\[\[str\](`str`)\], c: \[int\](`int`), d: \[int\](`int`), **kwargs: \[dict\](`dict`)\[\[str\](`str`), \[str\](`str`)\]" # noqa: W605
3030
)
3131

3232

@@ -94,13 +94,14 @@ def test_render_doc_attribute(renderer):
9494
attr = ds.DocstringAttribute(
9595
name="abc",
9696
description="xyz",
97-
annotation=exp.Expression(exp.Name("Optional", full="Optional"), "[", "]"),
97+
annotation=exp.Expression(exp.Name("Optional[]", full="Optional")),
9898
value=1,
9999
)
100100

101101
res = renderer.render(attr)
102+
print(res)
102103

103-
assert res == ["abc", r"Optional\[\]", "xyz"]
104+
assert res == ["abc", "[Optional\\[\\]](`Optional`)", "xyz"] # noqa
104105

105106

106107
@pytest.mark.parametrize("children", ["embedded", "flat"])

0 commit comments

Comments
 (0)