pdoc3 / pdoc3/pdoc

Use updated argspecs of functions wrapped by signature-changing decorators

Open
#326 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement
Dominant language
Python
Stars
1.2k
Forks
146
PR merge metrics
No merged PRs in 30d

Description

Actual behavior

When applying decorators that change the argument spec, internally applying functools.wraps to the replacement causes the corresponding docs to show the arguments of the inner wrapped function, rather than the wrapper.

With explicit type hinting, the types of the inner function arguments and return value are similarly used in place of the wrapper's new types.

Full example

Consider a decorator like the following, an alternate form of map():

def mapped(fn):
    """Map pairs of input numbers to an output number."""
    def inner(pairs):
        for x, y in pairs:
            yield fn(x, y)
    return inner

@mapped
def add(x, y):
    """Sum two numbers."""
    return x + y

if __name__ == "__main__":
    inputs = [(1, 2), (3, 4)]
    for output in add(inputs): ...

This produces documentation with the correct arguments, but the documentation for add comes from inner instead, which has no docstring:

Functions

add(pairs)

mapped(fn)
Map pairs of input numbers to an output number.

Now, if we decorate inner with functools.wraps:

from functools import wraps

def mapped(fn):
    @wraps(fn)
    def inner(pairs):
        for x, y in pairs:
            yield fn(x, y)
    return inner

...then we get the inner function's docstring as desired, but also its argument spec:

Functions

add(x, y)
Sum two numbers.

Expected behavior

When applying decorators that change the argument spec, using functools.wraps on the replacement should produce docs that show the arguments of the wrapper function, as that's effectively the public API.

The downside is that the inner docstring and the rewritten argspec could be inconsistent, though in general I'd expect the docstring to describe the public, outer version of the function if it's being decorated directly (i.e. @ syntax). Making this configurable at the project level might be desirable in any case.

Additional info
  • pdoc version: 0.9.2

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

No implementation file or test is named. Start by tracing how pdoc handles decorated functions, functools.wraps, and argument specifications, using the issue's mapped() example as a regression case. Done means generated documentation keeps the wrapper's public arguments while retaining the wrapped function's documentation and type behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
documentation
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.