Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

require Return section only if return is not None nor commentary #25008

Merged
merged 45 commits into from
Mar 11, 2019
Merged

require Return section only if return is not None nor commentary #25008

merged 45 commits into from
Mar 11, 2019

Conversation

dluiscosta
Copy link
Contributor

@dluiscosta dluiscosta commented Jan 29, 2019

Updated return lookup at source in validate_docstrings.py:

  • ignore "return None"
  • ignore empty return
  • ignore the word "return" on commentaries

Updated test_validate_docstrings.py:

  • added a test which contains the "returns" listed above and has a valid docstring with no Return section

@pep8speaks
Copy link

pep8speaks commented Jan 29, 2019

Hello @dluiscosta! Thanks for updating the PR.

Cheers ! There are no PEP8 issues in this Pull Request. 🍻

Comment last updated on February 19, 2019 at 06:04 Hours UTC

@dluiscosta
Copy link
Contributor Author

Added another "return" occurrence to the docstring in the test, on the beginning of a line.
It broke my solution.

@codecov
Copy link

codecov bot commented Jan 29, 2019

Codecov Report

Merging #25008 into master will increase coverage by <.01%.
The diff coverage is n/a.

Impacted file tree graph

@@            Coverage Diff             @@
##           master   #25008      +/-   ##
==========================================
+ Coverage   92.38%   92.38%   +<.01%     
==========================================
  Files         166      166              
  Lines       52401    52401              
==========================================
+ Hits        48409    48410       +1     
+ Misses       3992     3991       -1
Flag Coverage Δ
#multiple 90.8% <ø> (ø) ⬆️
#single 42.88% <ø> (-0.01%) ⬇️
Impacted Files Coverage Δ
pandas/util/testing.py 88.13% <0%> (+0.09%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update abf0824...d35ec84. Read the comment docs.

@codecov
Copy link

codecov bot commented Jan 29, 2019

Codecov Report

Merging #25008 into master will decrease coverage by 3.97%.
The diff coverage is n/a.

Impacted file tree graph

@@            Coverage Diff             @@
##           master   #25008      +/-   ##
==========================================
- Coverage   91.73%   87.76%   -3.98%     
==========================================
  Files         173      173              
  Lines       52839    57856    +5017     
==========================================
+ Hits        48472    50775    +2303     
- Misses       4367     7081    +2714
Flag Coverage Δ
#multiple 90.31% <ø> (+0.01%) ⬆️
#single 43% <ø> (+1.28%) ⬆️
Impacted Files Coverage Δ
pandas/io/excel/_util.py 56.83% <0%> (-23.82%) ⬇️
pandas/core/indexes/multi.py 75.43% <0%> (-20.18%) ⬇️
pandas/core/frame.py 78.29% <0%> (-18.59%) ⬇️
pandas/core/indexes/datetimes.py 78.31% <0%> (-17.99%) ⬇️
pandas/core/indexes/category.py 81.52% <0%> (-17.1%) ⬇️
pandas/core/generic.py 77.88% <0%> (-16.29%) ⬇️
pandas/core/arrays/numpy_.py 78.68% <0%> (-14.83%) ⬇️
pandas/core/arrays/timedeltas.py 74.56% <0%> (-13.74%) ⬇️
pandas/io/excel/_base.py 78.88% <0%> (-13.64%) ⬇️
pandas/core/indexes/base.py 85.1% <0%> (-11.46%) ⬇️
... and 11 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update f74aba6...4d6ec7e. Read the comment docs.

@jschendel jschendel added Docs CI Continuous Integration labels Jan 29, 2019
scripts/validate_docstrings.py Outdated Show resolved Hide resolved
scripts/validate_docstrings.py Outdated Show resolved Hide resolved
scripts/tests/test_validate_docstrings.py Outdated Show resolved Hide resolved
scripts/validate_docstrings.py Outdated Show resolved Hide resolved
Copy link
Member

@datapythonista datapythonista left a comment

Choose a reason for hiding this comment

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

I think we should use a more reliable approach, see comments.

scripts/tests/test_validate_docstrings.py Outdated Show resolved Hide resolved
scripts/tests/test_validate_docstrings.py Outdated Show resolved Hide resolved
scripts/validate_docstrings.py Outdated Show resolved Hide resolved
gathered.extend(gather_returns(child))
return gathered
# Walk the tree recursively and gather the return nodes.
return_values = [r.value for r in gather_returns(root)]
Copy link
Contributor Author

Choose a reason for hiding this comment

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

As opposing to using ast.walk() to iterate over all the nodes on the method's AST, here I'm recursively walking the tree myself.

The reason for that is to respect cases like the following:

def A():
    def B():
        return "Hello World!"
    print(B())
    return None

In this case, there is a inner function which returns a string, but the function being evaluated itself always return None, thus shouldn't have a Returns section on the docstring.

If I read all the nodes indiscriminately, as happens with ast.walk(), I can't distinguish returns inside nested functions from returns on the function actually being evaluated. If I search trough it myself, though, I can prune the recursion when I reach a (nested) function node, thus ignoring it's entire subtree and possible return nodes in it.

@dluiscosta
Copy link
Contributor Author

I'd appreciate some help understanding the reason for the current Travis fail. Other than that, I have made the suggested changes and apparently fixed the previous problems, @datapythonista, @WillAyd.

@WillAyd
Copy link
Member

WillAyd commented Feb 9, 2019

Travis failure is unrelated to your change and was resolved in #25225 . If you can merge master and re push should go away

Merge updated master to solve Travis issue.
@dluiscosta
Copy link
Contributor Author

Thanks @WillAyd, solved it.

if tree:
root = tree[0]

def gather_returns(node):
Copy link
Member

Choose a reason for hiding this comment

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

Hmm is there any way to simplify what is going on here? Is there no way to extract the .returns from the AST directly instead of having to recurse everything?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In our case, there isn't.

The AST provides the .walk() method to iterate through nodes. According to the doc, it

is useful if you only want to modify nodes in place and don’t care about the context.

Without context, there is no way to tell if a return node is inside a nested inner function or not, which is needed for one of the requirements raised by @datapythonista.

I explain this a bit further here.

Copy link
Member

@datapythonista datapythonista left a comment

Choose a reason for hiding this comment

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

looks good, just few comments

scripts/validate_docstrings.py Show resolved Hide resolved
scripts/validate_docstrings.py Outdated Show resolved Hide resolved
scripts/validate_docstrings.py Outdated Show resolved Hide resolved
if True:
return
else:
return None
Copy link
Member

Choose a reason for hiding this comment

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

can you add a test for a property that returns something (without the Returns section, which is the correct).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

such a test was already present before this PR:

    def return_not_documented(self):
        """
        Lacks section for Returns
        """
        return "Hello world!"

Copy link
Contributor Author

Choose a reason for hiding this comment

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

it is negative test, not like the one being commented here, so it's inside the BadReturns class

scripts/tests/test_validate_docstrings.py Outdated Show resolved Hide resolved
scripts/validate_docstrings.py Outdated Show resolved Hide resolved
scripts/validate_docstrings.py Show resolved Hide resolved
@dluiscosta
Copy link
Contributor Author

I don't understand why the tests are failing. Locally, everything is fine.

I tried once again to merge master, which had solved a previous problem, but this time it didn't help.

@WillAyd
Copy link
Member

WillAyd commented Feb 19, 2019

@dluiscosta can you merge master? Should resolve Py27 failure

@dluiscosta
Copy link
Contributor Author

Hey @datapythonista, @WillAyd, is there something left to be done here?

Copy link
Member

@WillAyd WillAyd left a comment

Choose a reason for hiding this comment

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

OK by me - @datapythonista

@jreback jreback added this to the 0.25.0 milestone Mar 10, 2019
@jreback
Copy link
Contributor

jreback commented Mar 10, 2019

@TomAugspurger
Copy link
Contributor

Thanks @dluiscosta!

@TomAugspurger TomAugspurger merged commit e2d0ad0 into pandas-dev:master Mar 11, 2019
thoo added a commit to thoo/pandas that referenced this pull request Mar 11, 2019
* upstream/master: (110 commits)
  DOC: hardcode contributors for 0.24.x releases (pandas-dev#25662)
  DOC: restore toctree maxdepth (pandas-dev#25134)
  BUG: Redefine IndexOpsMixin.size, fix pandas-dev#25580. (pandas-dev#25584)
  BUG: to_csv line endings with compression (pandas-dev#25625)
  DOC: file obj for to_csv must be newline='' (pandas-dev#25624)
  Suppress incorrect warning in nargsort for timezone-aware DatetimeIndex (pandas-dev#25629)
  TST: fix incorrect sparse test (now failing on scipy master) (pandas-dev#25653)
  CLN: Removed debugging code (pandas-dev#25647)
  DOC: require Return section only if return is not None nor commentary (pandas-dev#25008)
  DOC:Remove hard-coded examples from _flex_doc_SERIES (pandas-dev#24589) (pandas-dev#25524)
  TST: xref pandas-dev#25630 (pandas-dev#25643)
  BUG: Fix pandas-dev#25481 by fixing the error message in TypeError (pandas-dev#25540)
  Fixturize tests/frame/test_mutate_columns.py (pandas-dev#25642)
  Fixturize tests/frame/test_join.py (pandas-dev#25639)
  Fixturize tests/frame/test_combine_concat.py (pandas-dev#25634)
  Fixturize tests/frame/test_asof.py (pandas-dev#25628)
  BUG: Fix user-facing AssertionError with to_html (pandas-dev#25608) (pandas-dev#25620)
  DOC: resolve all GL03 docstring validation errors (pandas-dev#25525)
  TST: failing wheel building on PY2 and old numpy (pandas-dev#25631)
  DOC: Remove makePanel from docs (pandas-dev#25609) (pandas-dev#25612)
  ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CI Continuous Integration Docs
Projects
None yet
Development

Successfully merging this pull request may close these issues.

DOC: Detect whether a Returns section is needed in validate_docstrings.py
8 participants