terrencepreilly / darglint

A python documentation linter which checks that the docstring description matches the definition.
MIT License
481 stars 41 forks source link

Wrong DAR003 Incorrect indentation in Google Style docstrings #117

Closed staticdev closed 4 years ago

staticdev commented 4 years ago

I am using darlint 1.5.1 with the official Google Style example from https://google.github.io/styleguide/pyguide.html#383-functions-and-methods - fetch_smalltable_rows first example:

from typing import Sequence, Union, Mapping, Tuple
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: Optional; If require_all_keys is 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.
    """

It is giving error DAR003 on Args:

DAR003 Incorrect indentation: ~<

terrencepreilly commented 4 years ago

I think this is actually an error in that example docstring. Each of the docstrings in the document uses a different indentation scheme. This one indents by 4 spaces, except for hanging lines. The next one in the document indents by 2 spaces.

Darglint raises a style error when it doesn't encounter consistent indentation (you can set the level in the config with indentation=2). If you don't care about having consistent indentation for hanging lines, you can always just ignore that particular style error.

Unfortunately, it looks like there's a bug in the ignoring errors, where style errors aren't ignored. (See Issue #118). I'll leave this open until I submit the fix for that error (probably today.)

staticdev commented 4 years ago

@terrencepreilly I was able to ignore it in this commit https://github.com/staticdev/django-pagination-bootstrap/pull/138/files

terrencepreilly commented 4 years ago

Great!

staticdev commented 4 years ago

@terrencepreilly so if it is a problem with example docstring how should I fix this docstring?

I tried indenting with four spaces and still didn't fix:

def paginate(context, window=DEFAULT_WINDOW, hashtag=""):
    """Render the pagination.html template.
    Args:
        context:
          Dictionary-like data structure and must contain the following keys
          paginator - a ``Paginator`` or ``QuerySetPaginator`` object.
          page_obj - the result of calling the page method on the
          aforementioned ``Paginator`` or ``QuerySetPaginator`` object, given
          the current page.
          getvars (optional) - a dictionary of all of the **GET** parameters in the current request.
          This is useful to maintain certain types of state, even when requesting
          a different page.
        window:
          Optional. Defaults to DEFAULT_WINDOW.
        hashtag:
          Optional. Defaults to "".
    Returns:
        A Digg-like display of the available pages, given the current page.
        If there are too many pages to be displayed before and after the current page, then
        elipses will be used to indicate the undisplayed gap between page numbers.
    """
terrencepreilly commented 4 years ago

You're missing a blank line between sections. It should be

def paginate(context, window=DEFAULT_WINDOW, hashtag=""):
    """Render the pagination.html template.

    Args:
        context:
          Dictionary-like data structure and must contain the following keys
          paginator - a ``Paginator`` or ``QuerySetPaginator`` object.
          page_obj - the result of calling the page method on the
          aforementioned ``Paginator`` or ``QuerySetPaginator`` object, given
          the current page.
          getvars (optional) - a dictionary of all of the **GET** parameters in the current request.
          This is useful to maintain certain types of state, even when requesting
          a different page.
        window:
          Optional. Defaults to DEFAULT_WINDOW.
        hashtag:
          Optional. Defaults to "".

    Returns:
        A Digg-like display of the available pages, given the current page.
        If there are too many pages to be displayed before and after the current page, then
        elipses will be used to indicate the undisplayed gap between page numbers.
    """
    ...
staticdev commented 4 years ago

That worked, thanks @terrencepreilly!