Skip to content

MarkdownRenderer.__repr__ places the docstring inside a return-docment comment #911

Description

@civvic

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:

  1. The function has a docstring.
  2. The return annotation has a trailing docment.
  3. 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:

)->int: # return value

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions