Skip to content

Keeping original signatures for functions/methods #759

Closed

Description

@shimizukawa

I'm using Sphinx's autodoc feature to document my API.

Example:

#!python

DEFAULT_OPTION = 'default'
def do_something(msg, option=DEFAULT_OPTION):
    print msg

The generated documentation now shows the following signature:

do_something(msg, option='default')

It would be nice if there was a way to tell Sphinx to keep the name of the constant value, i.e.

do_something(msg, option=DEFAULT_OPTION)

At the moment the only alternative is to write all signature by hand again.


Activity

  1. martinpengellyphillips commented on Apr 17, 2015

    @martinpengellyphillips

    +1

  2. hypnocat commented on Apr 29, 2015

    @hypnocat

    @shimizukawa,@martinpengellyphillips you may try my fork with this feature.
    Install sphinx, and add to conf.py
    autodoc_dumb_docstring = True
    then rebuild docs. Feel free to msg me.

  3. mikofski commented on Sep 23, 2015

    @mikofski

    Another alternative that would be nice and might work easily is to allow the user to specify the value of an argument instead of the entire signature.

    • Unspecified arguments would be parsed by sphinx as usually.
    • Only the specified arguments would be annotated differently.
    • Omitting :annotation: would remove myarg from the signature so that it didn't show up at all as an interesting for secret optional args.
    • Specifying blank :annotation: would omit the default altogether but the keyword argument would still show up, just with no default value.
    • If myarg is not in the list of arguments parsed from the signature, then a warning is raised and the directive does nothing.
    • Markup roles like :class:, :data:, etc. can be used to link to the specified default.
    • If myarg is not a keyword argument, then should it warn or should it specify the default in defiance of all logic? probably warn.

    For example, given the following mymodule.py file:

    """a module with special treatment of arguments"""
    
    DEFAULT = "a very long value that I don't want to display in my function, class and method signatures"
    """a description of the default"""
    
    def myfunc(args, myarg=DEFAULT, kwargs=None):
        """
        a function with an ugly default arg value
    
        :param args: some arguments
        :param myarg: a keyword arg with a default specified by a module constant
        :param kwargs: some keyword arguments
        """
        pass
    
    class MyClass():
        """a class with an ugly default arg value in its constructor"""
        def __init__(self, args, myarg=DEFAULT, kwargs=None):
            pass
        def mymeth(self, args, myarg=DEFAULT, kwargs=None):
            """a method with an ugly default arg value"""
            pass
    

    use mymodule.rst file with the following:

    .. automodule:: mymodule
    
    .. autodata:: DEFAULT
       :annotation: a default value
    
    The value of ``myarg`` in this function is replaced by :data:`~mymodule.DEFAULT`
    
    .. autofunction:: myfunc
       .. argument:: myarg
          :annotation: :data:`~mymodule.DEFAULT`
    
    The value of ``myarg`` in this class constructor is not shown
    
    .. autoClass:: MyClass
       :members:
       .. argument:: myarg
          :annotation:
    
    The value of ``myarg`` in this class method is hidden
    
    .. automethod:: MyClass.mymeth
       .. argument:: myarg
    

    would output the following:

    mymodule
    a module with special treatment of arguments

    mymodule.DEFAULT = 'a default value'
    a description of the default

    The value of myarg in this function is replaced by :data:~mymodule.DEFAULT

    mymodule.myfunc(args, myarg=DEFAULT, kwargs=None)
    a function with an ugly default arg value

    Parameters

    • args - some arguments
    • myarg - a keyword arg with a default specified by a module constant
    • kwargs - some keyword arguments

    The value of myarg in this class constructor is not shown

    mymodule.MyClass(args, myarg=, kwargs=None)
    a class with an ugly default arg value in its constructor

    The value of myarg in this class method is hidden

    mymodule.MyClass.mymeth(args, kwargs=None)
    a method with an ugly default arg value

  4. modified the milestones: 3.0.0, 3.1.0 on Mar 14, 2020
  5. modified the milestones: 3.1.0, 3.2.0 on May 30, 2020
  6. modified the milestones: 3.2.0, 3.3.0 on Aug 7, 2020
  7. 2 remaining items

  8. modified the milestones: 3.4.0, 3.5.0 on Dec 19, 2020
  9. added 2 commits that reference this issue on Jan 27, 2021
    a6e9251
    f2ec653
  10. modified the milestones: 3.5.0, 4.0.0 on Feb 4, 2021
  11. added 5 commits that reference this issue on Mar 6, 2021
    8bf96bc
    71d6b34
    add2f9e
    1ea11b1
    c85d269
  12. tk0miya commented on Mar 6, 2021

    @tk0miya
    Member

    Will be fixed by #8771. Closing.

  13. added a commit that references this issue on Mar 11, 2021
    3134085
  14. locked as resolved and limited conversation to collaborators on Jul 13, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    extensions:autodoctype:enhancement

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions