Skip to content

Recommend Google Style for DocStrings - #88

Merged
mshafer-NI merged 31 commits into
ni:mainfrom
mshafer-NI:docstring-format
Jan 19, 2023
Merged

mshafer-NI merged 31 commits into
ni:mainfrom
mshafer-NI:docstring-format

Conversation

@mshafer-NI

@mshafer-NI mshafer-NI commented Oct 6, 2022 •

Copy link
Copy Markdown
Collaborator

Having any documentation is better than no documentation. Likewise following a format is better than not. However choosing a format can be intractable.

We chose Google style because the majority of people preferred it.

Since type hints exist (and we will be recommending their usage) the type information format of either doesn't concern me much.

NOTE: This PR started off recommending sphinx format - See #42 for history (which will be briefly recapped here)


There are two primary competing standards for Python Docstrings.
  • Sphinx

    An example Sphinx DocString:

    source

    class SimpleBleDevice(object):
    """This is a conceptual class representation of a simple BLE device
    (GATT Server). It is essentially an extended combination of the
    :class:`bluepy.btle.Peripheral` and :class:`bluepy.btle.ScanEntry` classes
    
    :param client: A handle to the :class:`simpleble.SimpleBleClient` client
        object that detected the device
    :type client: class:`simpleble.SimpleBleClient`
    :param addr: Device MAC address, defaults to None
    :type addr: str, optional
    :param addrType: Device address type - one of ADDR_TYPE_PUBLIC or
        ADDR_TYPE_RANDOM, defaults to ADDR_TYPE_PUBLIC
    :type addrType: str, optional
    :param iface: Bluetooth interface number (0 = /dev/hci0) used for the
        connection, defaults to 0
    :type iface: int, optional
    :param data: A list of tuples (adtype, description, value) containing the
        AD type code, human-readable description and value for all available
        advertising data items, defaults to None
    :type data: list, optional
    :param rssi: Received Signal Strength Indication for the last received
        broadcast from the device. This is an integer value measured in dB,
        where 0 dB is the maximum (theoretical) signal strength, and more
        negative numbers indicate a weaker signal, defaults to 0
    :type rssi: int, optional
    :param connectable: `True` if the device supports connections, and `False`
        otherwise (typically used for advertising ‘beacons’).,
        defaults to `False`
    :type connectable: bool, optional
    :param updateCount: Integer count of the number of advertising packets
        received from the device so far, defaults to 0
    :type updateCount: int, optional
    """
    
    def __init__(self, client, addr=None, addrType=None, iface=0,
                data=None, rssi=0, connectable=False, updateCount=0):
        """Constructor method
        """

  • Google

    An example Google Style DocString:

    source

    def fetch_smalltable_rows(table_handle: smalltable.Table,
                              keys: Sequence[Union[bytes, str]],
                              require_all_keys: bool = False,
    ) -> Mapping[bytes, tuple[str, ...]]:
        """Fetches rows from a Smalltable.
    
        Retrieves rows pertaining to the given keys from the Table instance
        represented by table_handle.  String keys will be UTF-8 encoded.
    
        Args:
            table_handle: An open smalltable.Table instance.
            keys: A sequence of strings representing the key of each table
              row to fetch.  String keys will be UTF-8 encoded.
            require_all_keys: If True only rows with values set for all keys will be
              returned.
    
        Returns:
            A dict mapping keys to the corresponding table row data
            fetched. Each row is represented as a tuple of strings. For
            example:
    
            {b'Serak': ('Rigel VII', 'Preparer'),
            b'Zim': ('Irk', 'Invader'),
            b'Lrrr': ('Omicron Persei 8', 'Emperor')}
    
            Returned keys are always bytes.  If a key from the keys argument is
            missing from the dictionary, then that row was not found in the
            table (and require_all_keys must have been False).
    
        Raises:
            IOError: An error occurred accessing the smalltable.
        """

Because there is not a widely adopted "use type-hinted signatures and good names and use the doc-string to document what the method does at a high level" standard, we are choosing to adopt an existing standard that best fits our needs.

Initially, the proposal was to use Sphinx; however, consensus was reached that requiring additional parameter-type information on top of the method/function/class signature and the imperative mood, were both undesirable for maintenance and reading code.

The PR was changed to recommending Google-style doc-strings because it offers fewer constraints on verbage and less duplicate information from the signature.

@github-actions

github-actions Bot commented Oct 6, 2022

Copy link
Copy Markdown
Contributor

Thank you for contributing! 👋

Since this changes the Coding Conventions, I'll @-mention the appropriate NI engineers.
@alejandro5042
@alexweav
@DavidCurtiss
@irwand
@jryckman
@kcuzner
@mshafer-NI
@neilvana
@nethrasuresh
@pakdev
@rtzoeller
@sbethur
@stick152
@Adithyak1998
@innagarc
@ShibaniRout

@mshafer-NI

Copy link
Copy Markdown
Collaborator Author

Tagging our newest Python Working Group Member to review the conventions change as well:
@tkrebes

@kcuzner

kcuzner commented Oct 21, 2022 •

Copy link
Copy Markdown

I want to mention in hardware-land, we've been using restructured test extensively in docstrings (and increasingly; far far more than when the original PR was created) over the past year, along with starting to write some documentation in straight-up rst. I was originally thinking to start rehashing/relitigating the discussion from nearly 2 years ago since I hadn't participated (not having an opinion at the time). However, after googling enough I see that Napoleon can be used to allow google-style docstrings with sphinx and so my only qualm is that if I switch things to google-style docstrings I'm going to have to rewrite some tooling I wrote that parses python docstrings into LabVIEW documentation.

Is it worth mentioning any rationale for this choice in the styleguide, possibly also noting the integrations available for google-style docstrings in sphinx?

@mshafer-NI

Copy link
Copy Markdown
Collaborator Author

I want to mention in hardware-land, we've been using restructured test extensively in docstrings (and increasingly; far far more than when the original PR was created) over the past year, along with starting to write some documentation in straight-up rst. I was originally thinking to start rehashing/relitigating the discussion from nearly 2 years ago since I hadn't participated (not having an opinion at the time). However, after googling enough I see that Napoleon can be used to allow google-style docstrings with sphinx and so my only qualm is that if I switch things to google-style docstrings I'm going to have to rewrite some tooling I wrote that parses python docstrings into LabVIEW documentation.

Is it worth mentioning any rationale for this choice in the styleguide, possibly also noting the integrations available for google-style docstrings in sphinx?

Hmmm, we mostly don’t have rationale in the doc since it is preferable to keep it a “do this”; however, this PR should probably capture more of the rationale.

For Sphinx integration – yeah, we should call that out since that was one of our requirements. I’ll try to update the doc to capture that.

Comment thread docs/Coding-Conventions.md Resolved
@github-actions

Copy link
Copy Markdown
Contributor

Thank you for contributing! 👋

Since this changes the Coding Conventions, I'll @-mention the appropriate NI engineers.
@alejandro5042
@DavidCurtiss
@irwand
@jryckman
@kcuzner
@mshafer-NI
@neilvana
@nethrasuresh
@pakdev
@rtzoeller
@sbethur
@stick152
@tkrebes
@Adithyak1998
@innagarc
@ShibaniRout

@github-actions

Copy link
Copy Markdown
Contributor

Thank you for contributing! 👋

Since this changes the Coding Conventions, I'll @-mention the appropriate NI engineers.
@alejandro5042
@DavidCurtiss
@irwand
@jryckman
@kcuzner
@mshafer-NI
@neilvana
@nethrasuresh
@pakdev
@rtzoeller
@sbethur
@stick152
@tkrebes
@Adithyak1998
@innagarc
@ShibaniRout

Comment thread ni_python_styleguide/config.ini Resolved
@github-actions

github-actions Bot commented Jan 9, 2023

Copy link
Copy Markdown
Contributor

Thank you for contributing! 👋

Since this changes the Coding Conventions, I'll @-mention the appropriate NI engineers.
@alejandro5042
@DavidCurtiss
@irwand
@jryckman
@kcuzner
@mshafer-NI
@neilvana
@nethrasuresh
@pakdev
@rtzoeller
@sbethur
@stick152
@tkrebes
@Adithyak1998
@innagarc
@ShibaniRout

Comment thread docs/Coding-Conventions.md Resolved

@kcuzner kcuzner left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but you can choose any format so long as you are consistent

So long as this stays, I think my concerns are addressed. I've been working on a tool that will not work well with google-style docstrings since it uses docutils to parse things, but I still need to see if I can somehow leverage sphinx napoleon into fixing that.

@mshafer-NI
mshafer-NI merged commit 80fb994 into ni:main Jan 19, 2023
mshafer-NI added a commit that referenced this pull request Oct 10, 2023
* Recommend sphinx as the docstring format

* Update docs/Coding-Conventions.md

Co-authored-by: mshafer-NI <matthew.shafer@ni.com>

* Change the convention to be Google style

* Update docs/Coding-Conventions.md

Co-authored-by: Ryan Zoeller <Ryan.Zoeller@ni.com>

* Allow any convention style

* add org-check to PR notify

* touch conventions file to prompt PR checks

* touch again

* set docstring convention to Google

* add some debug output

* document D412

* rname vars and re-work to bettter show what the error is

* switch to "all" and suprress the codes that we see that don't fit the "suggest Google"

* format

* revert most of the docstring changes

* remove blank lines before closing quotes

* fix branch name in notify action

* add note about using sphinx.ext.napolean to handle docs

* Revert "revert most of the docstring changes"

This reverts commit c09c931.

* more docstrings to active form

* add D406 to excluded rules

* s/sell/sells/g

* add all the codes pydocstyle would ignore if we were explicitly stating Google style

* don't need that twice

Co-authored-by: Joshua Cannon <joshua.cannon@ni.com>
Co-authored-by: Joshua Cannon <joshdcannon@gmail.com>
Co-authored-by: Ryan Zoeller <Ryan.Zoeller@ni.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants