question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Validate PEP-8 in docstring examples

See original GitHub issue

At the moment we are automatically validating PEP-8 in our code (using flake8 . in the CI), and we also have the script scripts/validate_docstrings.py that reports errors for different formatting errors in our docstrings, and it also runs the tests to see if they work and generate the expected input. But we are not validating whether the examples follow PEP-8.

In the next example:

def head():
    """
    Return the first n rows of the Series.

    Examples
    --------
    >>> s=pd.Series([1, 2, 3, 4])
    >>> s.head(2)
    0    1
    1    2
    dtype: int64

Everything will be reported as correct, because the formatting of the docstring is the expected, and when running the examples, they work and they generated the presented output.

According to PEP-8, there should be spaces around the = in an assignment. So, we should have s = pd.Series([1, 2, 3, 4]) instead of s=pd.Series([1, 2, 3, 4]). So, the validation should report it as an error.

What we need to do is:

  • Add a method in scripts/validate_docstrings.py that obtains the code in the examples (and if possible their line in the code to be presented to the user if there are errors). For what I know, the module doctest in the Python standard library provides a way to extract the code.
  • Add another method that given some code, returns possible PEP-8 errors. We currently have pyflakes as a dependency, so using it is probably the best option. But if it’s better to use pycodestyle or something else, that could be an option.
  • Use these methods in the validation, so errors are reported as the rest of the errors from the validation
  • Add unit tests for the implemented features in scripts/tests/test_validate_docstrings.py

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:15 (14 by maintainers)

github_iconTop GitHub Comments

1reaction
FHaasecommented, Oct 18, 2018

As flake8 is already being used, wouldn’t it make sense to use flake8 plugins to check for those things?

0reactions
datapythonistacommented, Nov 9, 2018

Fixed in #23399

Read more comments on GitHub >

github_iconTop Results From Across the Web

PEP 8 – Style Guide for Python Code
This document and PEP 257 (Docstring Conventions) were adapted from Guido's original ... Look at other examples and decide what looks best.
Read more >
How to Write Beautiful Python Code With PEP 8 - Real Python
Learn how to write high-quality, readable code by using the Python style guidelines laid out in PEP 8. Following these guidelines helps you...
Read more >
Checking and Formatting Python Code With PEP 8 Standards
How to check if your Python code is formatted according to the PEP 8 standards? First of all, what is PEP 8? PEP...
Read more >
Introduction — pep8 1.7.1 documentation
pep8 is a tool to check your Python code against some of the style conventions in PEP 8. Features; Disclaimer; Installation; Example usage...
Read more >
pep8 documentation - Read the Docs
pep8 is a tool to check your Python code against some of the style conventions in ... Several docstrings contain examples directly from...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found