Skip to content

Commit 9b7162a

Browse files
committed
fix: MdRenderer.signature() now operates on Doc
1 parent 6ee9064 commit 9b7162a

File tree

2 files changed

+29
-43
lines changed

2 files changed

+29
-43
lines changed

quartodoc/renderers/md_renderer.py

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

3030

31-
def with_doc_options(f):
32-
"""Decorator to add docstring options to a renderer.
33-
34-
Currently, the renderer renders signatures directly from griffe objects,
35-
so this decorator is used, to temporarily set some things from layout.Doc
36-
as renderer settings.
37-
38-
(Not ideal but works for now.)
39-
"""
40-
41-
@wraps(f)
42-
def wrapper(self, el, *args, **kwargs):
43-
orig = self.display_name
44-
self.display_name = el.signature_path
45-
res = f(self, el, *args, **kwargs)
46-
47-
self.display_name = orig
48-
49-
return res
50-
51-
return wrapper
52-
53-
5431
class MdRenderer(Renderer):
5532
"""Render docstrings to markdown.
5633
@@ -152,6 +129,22 @@ def render_annotation(self, el: "str | expr.Name | expr.Expression | None"):
152129

153130
# signature method --------------------------------------------------------
154131

132+
@dispatch
133+
def signature(self, el: layout.Doc):
134+
orig = self.display_name
135+
136+
# set signature path, generate signature, then set back
137+
# TODO: this is for backwards compatibility with the old approach
138+
# of only defining signature over griffe objects, which projects
139+
# like shiny currently extend
140+
self.display_name = el.signature_path
141+
res = self.signature(el.obj)
142+
self.display_name = orig
143+
144+
return res
145+
146+
147+
155148
@dispatch
156149
def signature(self, el: dc.Alias, source: Optional[dc.Alias] = None):
157150
"""Return a string representation of an object's signature."""
@@ -270,7 +263,6 @@ def render(self, el: layout.Doc):
270263
raise NotImplementedError(f"Unsupported Doc type: {type(el)}")
271264

272265
@dispatch
273-
@with_doc_options
274266
def render(self, el: Union[layout.DocClass, layout.DocModule]):
275267
title = self.render_header(el)
276268

@@ -335,30 +327,30 @@ def render(self, el: Union[layout.DocClass, layout.DocModule]):
335327
[self.render(x) for x in raw_meths if isinstance(x, layout.Doc)]
336328
)
337329

330+
331+
str_sig = self.signature(el)
332+
sig_part = [str_sig] if self.show_signature else []
333+
338334
body = self.render(el.obj)
339-
return "\n\n".join([title, body, *attr_docs, *class_docs, *meth_docs])
340335

341-
@dispatch
342-
@with_doc_options
343-
def render(self, el: layout.DocFunction):
344-
title = self.render_header(el)
345336

346-
return "\n\n".join([title, self.render(el.obj)])
337+
return "\n\n".join([title, *sig_part, body, *attr_docs, *class_docs, *meth_docs])
347338

348339
@dispatch
349-
@with_doc_options
350-
def render(self, el: layout.DocAttribute):
340+
def render(self, el: Union[layout.DocFunction, layout.DocAttribute]):
351341
title = self.render_header(el)
352-
return "\n\n".join([title, self.render(el.obj)])
342+
343+
str_sig = self.signature(el)
344+
sig_part = [str_sig] if self.show_signature else []
345+
346+
return "\n\n".join([title, *sig_part, self.render(el.obj)])
353347

354348
# render griffe objects ===================================================
355349

356350
@dispatch
357351
def render(self, el: Union[dc.Object, dc.Alias]):
358352
"""Render high level objects representing functions, classes, etc.."""
359353

360-
str_sig = self.signature(el)
361-
362354
str_body = []
363355
if el.docstring is None:
364356
pass
@@ -374,10 +366,7 @@ def render(self, el: Union[dc.Object, dc.Alias]):
374366
else:
375367
str_body.append(body)
376368

377-
if self.show_signature:
378-
parts = [str_sig, *str_body]
379-
else:
380-
parts = [*str_body]
369+
parts = [*str_body]
381370

382371
return "\n\n".join(parts)
383372

quartodoc/tests/test_basic.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,7 @@ def test_render_attribute():
4949
# TODO: snapshot tests
5050
a = get_object("quartodoc", "tests.example_attribute.a")
5151

52-
assert (
53-
MdRenderer().render(a)
54-
== "`tests.example_attribute.a`\n\nI am an attribute docstring"
55-
)
52+
assert MdRenderer().render(a) == "I am an attribute docstring"
5653

5754

5855
def test_get_object_dynamic_module_root():

0 commit comments

Comments
 (0)