Skip to content

show_doc renders classes with def prefix instead of class #829

Description

@amaiya

show_doc renders classes with def prefix instead of class

Description

When using show_doc() (from nbdev) on a Python class, the generated documentation incorrectly shows the class signature with def instead of class.

This problem can be seen in the fastcore documentation itself:

https://fastcore.fast.ai/meta.html#fixsigmeta

...which incorrectly displays

def FixSigMeta(
    args:VAR_POSITIONAL, kwargs:VAR_KEYWORD
):

To Reproduce

from nbdev.showdoc import show_doc

class MyClass:
    def __init__(self, param1: str, param2: int = 10):
        """A simple class"""
        self.param1 = param1
        self.param2 = param2

show_doc(MyClass)

Expected Output

class MyClass(
    param1:str, param2:int=10
)

Actual Output

def MyClass(
    param1:str, param2:int=10
)

Root Cause

In fastcore/docments.py around line 375, the code determines the prefix for the signature:

prefix = 'async def' if inspect.iscoroutinefunction(o) else 'def'

This checks for async functions but always defaults to 'def' for everything else, including classes. It never checks inspect.isclass(o) to use 'class' as the prefix.

Suggested Fix

if inspect.isclass(o):
    prefix = 'class'
elif inspect.iscoroutinefunction(o):
    prefix = 'async def'
else:
    prefix = 'def'

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions