Skip to content

Docstring flavors #153

Closed
Closed

Description

@cortesi

I'm opening this ticket to unify discussion of docstring flavors. We have a wide range of requests for these, including:

I'm going to close all these tickets so that we can explore which direction to take in one place. When I first took on pdoc, I thought we could get away with supporting one and only one format. Having read carefully through all of the related tickets, I've now softened that a bit, and I think that there's an argument for being pragmatic.

My feeling is that we should clearly cater for doctests and pre-formatted docstrings, and choose a more flexible markdown renderer that includes Github-style tables and the like. Equally, I feel we should clearly not include support for reStructuredText, which is way too large and complicated. I'm undecided about things like Google style and EpyText, which are intermediate.

It's possible that we could use a Github-style type marker for this:

"""raw
   
    This is a pre-formatted docstring.
                 Indentation will be preserved.
"""

And:

"""google
A Google-style docstring.

Arguments:
     argname (type): Description.
     secondarg (type : default): Description.

Returns:
    boolean: True on success.
"""

The downside of this is that it introduces a weirdness to docstrings that make them less nice for both humans in code editors and other documentation tools. Another possibility (fronted in some PRs) is to control this unilaterally for an entire module with a command-line flag. This has the issue that the format is not specified in the code (where I feel it should be), and that many modules will be heterogenous and require support for multiple formats. Thoughts?

Activity

  1. peterjc commented on Jun 4, 2018

    @peterjc

    In epydoc you could set this in the code at file level with __docformat__, see rejected PEP258 and other documentation:

    https://www.python.org/dev/peps/pep-0258/#choice-of-docstring-format

    Reusing this convention seems far preferable to inventing another.

  2. kernc commented on Jun 4, 2018

    @kernc
    Contributor

    Could we have __docformat__ rather a key in __pdoc__? The pep was rejected and the idea of namespaces predates it anyway.

    As oposed to __docformat__ being a string, needing of us to implement/wrap all sorts of formatters, it could also be a Callable[[str], str]?

  3. armant commented on Sep 26, 2018

    @armant

    Controlling with a command-line flag seems sensible and pragmatic to me. If I rebase and resubmit #101, will it be considered?

  4. armant commented on Oct 10, 2018

    @armant

    Bumping this

  5. mhils commented on Nov 7, 2018

    @mhils
    Member

    From what I've read so far I think there'd be no strong objections to just start with a universal command-line flag (for example, --docformat).
    If we then find that to be insufficient and would like to add support for heterogenous modules, we can add support for __docformat__, which would override the command line flag.

    Thoughts?

  6. H4dr1en commented on Feb 14, 2020

    @H4dr1en

    What is the current status on docstrings? As of 0.7.4, None (Google, numpy, reST) are supported by pdoc, which is very frustrating.

  7. peterjc commented on Feb 14, 2020

    @peterjc

    If you want to add a command line option, using ----docformat as suggested above would match the convention from epydoc (where the files themselves did not contain __docformat__), see:

    http://epydoc.sourceforge.net/manual-usage.html

    For large inhomogeneous projects being able to set the docstring format on a file by file level is especially useful (converting a lot of documentation at once would be hard to coordinate). But being pragmatic, in my experience a global setting would work fine for most projects.

  8. deleted a comment from peterjc on Jan 19, 2021
  9. mhils commented on Jan 24, 2021

    @mhils
    Member

    Hey folks! Better late than never, I'm happy to announce that the latest pdoc release now supports Google-style and numpydoc docstrings. 😃

    We're using the --docformat and __docformat__ convention mentioned above.

  10. ostrokach commented on Jan 26, 2021

    @ostrokach

    This still does not seem to work in my case, both using pdoc 3.0.0 installed from PyPI and the master branch?

    For example, the following code snippet:

    def function_with_types_in_docstring(param1, param2):
        """Example function with types documented in the docstring.
    
        `PEP 484`_ type annotations are supported. If attribute, parameter, and
        return types are annotated according to `PEP 484`_, they do not need to be
        included in the docstring:
    
        Args:
            param1 (int): The first parameter.
            param2 (str): The second parameter.
    
        Returns:
            bool: The return value. True for success, False otherwise.
    
        .. _PEP 484:
            https://www.python.org/dev/peps/pep-0484/
        """
        return param1 + param2

    gives the following output:

    image

    I am invoking pdoc using the following command:

    pdoc --docformat google -o docs example/
  11. ostrokach commented on Jan 26, 2021

    @ostrokach

    Oh, but it does work if I put __docformat__ = "google" at the top of the file! So I guess I will start a separate issue.

    Thanks!

  12. mhils commented on Jan 26, 2021

    @mhils
    Member

    @ostrokach: looks like I butchered this last minute, thanks for the heads-up. Expect a fixed release in a few minutes.

  13. mhils commented on Jan 26, 2021

    @mhils
    Member

    3.0.1 is out, thanks again! 😃

  14. ostrokach commented on Jan 26, 2021

    @ostrokach

    Can confirm that it works, thank you!

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions