AcademySoftwareFoundation / rez

An integrated package configuration, build and deployment system for software
https://rez.readthedocs.io
Apache License 2.0
951 stars 339 forks source link

consolidate docstring style #51

Open chadrik opened 10 years ago

chadrik commented 10 years ago

There are a couple of different docstring formats being used in rez right now, we should decide on a convention and stick to it.

First, we need to decide what documentation generator we will use as that will dictate the available styles. I think that sphinx is the obvious choice, because it is the only actively maintained documentation generator for python out there. I've used epydoc, and it was easy to use, but it was already pretty outdated 5 years ago, so I would not bet on that horse now. Doxygen was designed for C++ and its python support is just a hack on top of that. IIRC, it could not do python introspection on live python objects like sphinx and epydoc, it could just parse python code.

So, if sphinx is the choice we have 3 styles to choose from: native, numpy, and google. here's a sphinx extension that adds supports for the latter 2: http://sphinx-doc.org/latest/ext/napoleon.html

Here they are for your simple viewing pleasure:

sphinx

def func(arg1, arg2):
    """Summary line.

    Extended description of function.

    :param arg1: Description of arg1
    :type arg1: int
    :param arg2: Description of arg2
    :type arg2: str
    :returns: Description of return value
    :rtype: bool

    """
    return True

numpy

def func(arg1, arg2):
    """Summary line.

    Extended description of function.

    Parameters
    ----------
    arg1 : int
        Description of arg1
    arg2 : str
        Description of arg2

    Returns
    -------
    bool
        Description of return value

    """
    return True

google

def func(arg1, arg2):
    """Summary line.

    Extended description of function.

    Args:
        arg1 (int): Description of arg1
        arg2 (str): Description of arg2

    Returns:
        bool: Description of return value

    """
    return True

A note on sphinx:

If you have not used sphinx before, but you have used other documentation generators like epydoc, it might be confusing at first. It is important to know that sphinx is designed in layers: the primary layer is a pure restructuredText parser, which translates an rst file into rich output like html, usually one html file per rst file. Hence, most of the sphinx documentation covers writing restructuredText documents from scratch and has nothing to say about generating API documentation from source code. That is handled by a set of extensions (namely, the autodoc extension) which generate rst documents from python modules and their docstrings, which are then converted into rich output as a second pass.

nerdvegas commented 10 years ago

I don't mind if the doc type is doxygen or sphinx - what I care about is compact comments. For that reason, I really dislike the numpy style. My preference is for google-style, sans empty lines even better.

A

On Wed, Mar 5, 2014 at 10:07 AM, Chad Dombrova notifications@github.comwrote:

There are a couple of different docstring formats being used in rez right now, we should decide on a convention and stick to it.

First, we need to decide what documentation generator we will use as that will dictate the available styles. I think that sphinx is the obvious choice, because it is the only actively maintained documentation generator for python out there. I've used epydoc, and it was easy to use, but it was already pretty outdated 5 years ago, so I would not bet on that horse now. Doxygen was designed for C++ and its python support is just a hack on top of that. IIRC, it could not do python introspection on live python objects like sphinx and epydoc, it could just parse python code.

So, if sphinx is the choice we have 3 styles to choose from: native, numpy, and google. here's a sphinx extension that adds supports for the latter 2: http://sphinx-doc.org/latest/ext/napoleon.html

Here they are for your simple viewing pleasure:

sphinx

def func(arg1, arg2): """Summary line. Extended description of function. :param arg1: Description of arg1 :type arg1: int :param arg2: Description of arg2 :type arg2: str :returns: Description of return value :rtype: bool """ return True

numpy

def func(arg1, arg2): """Summary line. Extended description of function. Parameters ---------- arg1 : int Description of arg1 arg2 : str Description of arg2 Returns ------- bool Description of return value """ return True

google

def func(arg1, arg2): """Summary line. Extended description of function. Args: arg1 (int): Description of arg1 arg2 (str): Description of arg2 Returns: bool: Description of return value """ return True

A note on sphinx:

If you have not used sphinx before, but you have used other documentation generators like epydoc, it might be confusing at first. It is important to know that sphinx is designed in layers: the primary layer is a pure restructuredText parser, which translates an rst file into rich output like html, usually one html file per rst file. Hence, most of the sphinx documentation covers writing restructuredText documents from scratch and has nothing to say about generating API documentation from source code. That is handled by a set of extensions (namely, the autodoc extension) which generate rst documents from python modules and their docstrings, which are then converted into rich output as a second pass.

Reply to this email directly or view it on GitHubhttps://github.com/nerdvegas/rez/issues/51 .

chadrik commented 10 years ago

google style is fine with me, but the empty lines are required by the restructuredText parser (except for the last one).

nerdvegas commented 10 years ago

Ok, then shall we agree on Googly Sphynx, with no trailing empty line? If so I will start using this on new comments, and gradually switch existing doxygen styled comments over.

On Wed, Mar 5, 2014 at 3:26 PM, Chad Dombrova notifications@github.comwrote:

google style is fine with me, but the empty lines are required by the restructuredText parser (except for the last one).

Reply to this email directly or view it on GitHubhttps://github.com/nerdvegas/rez/issues/51#issuecomment-36807692 .

chadrik commented 10 years ago

sounds good.

mstreatfield commented 10 years ago

If there are no objections, I'll look at getting it hooked up to readthedocs.org so they are easily accessible etc.

On 6 March 2014 13:24, Chad Dombrova notifications@github.com wrote:

sounds good.

Reply to this email directly or view it on GitHubhttps://github.com/nerdvegas/rez/issues/51#issuecomment-36818641 .

chadrik commented 10 years ago

I renamed this ticket from 'decide on...' to 'consolidate...' so that we can use it to track the progress of cleaning up our docstrings.

chadrik commented 10 years ago

Here are some more complete examples of google-style docstrings: http://sphinxcontrib-napoleon.readthedocs.org/en/latest/example_google.html

bfloch commented 5 years ago

If we still want to conform to this I've run a docstring checker called darglint over master. If we get to the point where all issues are resolved (and the tool turns out to be reliable) we could make it part of the checks for PRs.

Error Codes

I101: The docstring is missing a parameter in the definition.
I102: The docstring contains a parameter not in function.
I103: The docstring parameter type doesn't match function.
I201: The docstring is missing a return from definition.
I202: The docstring has a return not in definition.
I203: The docstring parameter type doesn't match function.
I301: The docstring is missing a yield present in definition.
I302: The docstring has a yield not in definition.
I401: The docstring is missing an exception raised.
I402: The docstring describes an exception not explicitly raised.
I501: The docstring describes a variable which is not defined.
S001: Describes that something went wrong in parsing the docstring.
S002: An argument/exception lacks a description.

The error code scheme is based on the errors from the pycodestyle package. The first letter corresponds to the broad class of error:

I (Interface): Incorrect or incomplete documentation.
S (Style): Errors with documentation style/syntax.

The number in the hundreds narrows the error by location in the docstring:

100: Args section
200: Returns section
300: Yields section
400: Raises section

Report at 62d05f3609e5fe0fd903fdb770abbba89a637dca

It may not be perfect, so expect false positives but for what its worth:

maxnbk commented 5 years ago

I think we should ignore contents of ./rez/vendor, to not make updating of vendored libs even more of a chore.

JeanChristopheMorinPerso commented 5 years ago

I'd go with Pylint (which comes with builtin support for docstring checks, although disabled by default) and would allow us to do general code linting. Pylint supports Sphinx, Google and Numpy dosctrs.

And I agree, vendored libs should be ignored.

nerdvegas commented 5 years ago

Would be good to add linting to CI, as well as black (when we get to that).

On Tue, 8 Oct. 2019, 12:12 Jean-Christophe Morin, notifications@github.com wrote:

I'd go with Pylint (which comes with builtin support for docstring checks, although disabled by default) and would allow us to do general code linting. Pylint supports Sphinx, Google and Numpy dosctrs.

And I agree, vendored libs should be ignored.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/nerdvegas/rez/issues/51?email_source=notifications&email_token=AAMOUSXWKAESPI7OUTFBZNDQNPNAHA5CNFSM4AM4MDB2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEASJWMY#issuecomment-539269939, or mute the thread https://github.com/notifications/unsubscribe-auth/AAMOUSVRHKLXDDPRNUIIATTQNPNAHANCNFSM4AM4MDBQ .