When a function has both a docstring and a trailing docment on its return annotation, MarkdownRenderer.__repr__ appends the docstring directly after the rendered signature.
Because the signature ends with an inline # comment, the docstring becomes part of that comment instead of appearing as the function body.
This is visible through pyskills.doc(), which uses MarkdownRenderer.
Reproduction
from pyskills import doc
def f() -> int: # return value
"docstring"
doc(f)
Actual output:
def f()->int: # return value"""docstring"""
Expected output:
def f()->int: # return value
"""docstring"""
A direct fastcore reproduction is:
from fastcore.docments import MarkdownRenderer
def f() -> int: # return value
"docstring"
repr(MarkdownRenderer(f))
Impact
The rendered result is misleading and is not valid as a representation of the original function body: everything after # return value is part of the comment.
The issue occurs whenever all three conditions are present:
- The function has a docstring.
- The return annotation has a trailing docment.
- The plain-text representation of
MarkdownRenderer is used.
Environment
- fastcore: 2.2.13
- Branch:
main
Likely cause
MarkdownRenderer.__repr__ concatenates the docstring directly onto the rendered DocmentText:
def __repr__(self):
doc = str(self.dm)
if self.docs: doc += f'"""{self.docs}"""'
return doc
When str(self.dm) ends with a return docment, it ends in an inline comment:
Appending the docstring without a newline places it inside that comment.
The renderer should insert a newline and indentation before the docstring, including appropriate indentation for multiline docstrings.
When a function has both a docstring and a trailing docment on its return annotation,
MarkdownRenderer.__repr__appends the docstring directly after the rendered signature.Because the signature ends with an inline
#comment, the docstring becomes part of that comment instead of appearing as the function body.This is visible through
pyskills.doc(), which usesMarkdownRenderer.Reproduction
Actual output:
Expected output:
A direct fastcore reproduction is:
Impact
The rendered result is misleading and is not valid as a representation of the original function body: everything after
# return valueis part of the comment.The issue occurs whenever all three conditions are present:
MarkdownRendereris used.Environment
mainLikely cause
MarkdownRenderer.__repr__concatenates the docstring directly onto the renderedDocmentText:When
str(self.dm)ends with a return docment, it ends in an inline comment:Appending the docstring without a newline places it inside that comment.
The renderer should insert a newline and indentation before the docstring, including appropriate indentation for multiline docstrings.