Skip to content

BUG: Fix help() formatting for deprecated functions. #8131

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

Merged
merged 1 commit into from
Mar 10, 2019

Conversation

wrwrwr
Copy link
Contributor

@wrwrwr wrwrwr commented Oct 10, 2016

Please see the description in #8058.

The change also remedies the double blank line between the deprecation note and the summary appearing when the summary is not on the same line as the opening quotes.

Trying to handle all cases without resorting to inspect resulted in nearly duplicating cleandoc(), so I've decided to actually call on it.

@pv
Copy link
Member

pv commented Oct 10, 2016 via email

@wrwrwr wrwrwr force-pushed the deprecate-formatting branch from ffe05bf to db6459c Compare October 10, 2016 14:30
@wrwrwr
Copy link
Contributor Author

wrwrwr commented Oct 10, 2016

Thanks, now that I've found compat._inspect copy-and-paste seems reasonable. My understanding is that it's fine to import the standard library inspect in tests, but please correct me if I'm wrong..

For reference, the commits series that introduced compat._inspect.

@rgommers
Copy link
Member

Yes, in tests is fine. Those are not imported when you import numpy.

"""Clean up indentation from docstrings.

Any whitespace that can be uniformly removed from the second line
onwards is removed."""
Copy link
Member

Choose a reason for hiding this comment

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

Hmm, can be uniformly removed isn't very informative as to which whitespace is removed. Also, the closing """ should follow a blank line.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That's a straight copy from https://hg.python.org/cpython/file/tip/Lib/inspect.py#l579. Should we reformat it anyway?

By the way, I wasn't able to reproduce the import inspect slowdown (that's the reason for copying the function), but on the other hand, can't find anything in CPython's logs that would imply that something has changed in this respect.

Copy link
Member

Choose a reason for hiding this comment

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

Sure, cpython isn't always the best for documentation. If inspect imports are no longer a problem we could simplify things in the future. IIRC, @rkern had the concern.

@pv
Copy link
Member

pv commented Oct 14, 2016

$ time python -c 'import numpy, os'
real 0m0.067s
user 0m0.064s
sys 0m0.096s
$ time python -c 'import numpy, inspect'
.
real 0m0.085s
user 0m0.092s
sys 0m0.116s
.
on numpy 1.11.1

@wrwrwr
Copy link
Contributor Author

wrwrwr commented Oct 15, 2016

@pv Thanks, I've allowed myself to turn this into a benchmark in #8159.

@wrwrwr wrwrwr force-pushed the deprecate-formatting branch 2 times, most recently from 1b25efb to aa55941 Compare October 15, 2016 16:43
@wrwrwr
Copy link
Contributor Author

wrwrwr commented Oct 15, 2016

Rebased for another try. With a fresh look I've noticed one more aspect of the issue, namely that cleaning the original docstring upfront may not be such a good idea. For example, imagine a style-enforcement tool that detects whitespace-only lines in docstrings by looking at __doc__s. With the previous approach only cleaned docs of deprecated functions would be available and that could make the tool miss some lines.

Taking scenarios such as the above into account costs ~15 lines of code looking suspiciously similar to cleandoc() (but only indenting the original first line and removing the leading "blank lines").

@wrwrwr wrwrwr force-pushed the deprecate-formatting branch from aa55941 to 95bab2c Compare October 16, 2016 09:45
Copy link
Member

@eric-wieser eric-wieser left a comment

Choose a reason for hiding this comment

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

Fix looks good, but tests seem suspect

@@ -51,6 +78,23 @@ def test_deprecate_fn():
assert_('new_func3' in new_func3.__doc__)


def test_deprecate_help_indentation():
assert_(' ' not in inspect.getdoc(old_func4))
assert_(' ' not in inspect.getdoc(old_func5))
Copy link
Member

Choose a reason for hiding this comment

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

Wait, isn't this expected before "Bizarre indentation"?

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 far as I remember, we are trying to make the indentation in help() the same with and without deprecation. The output of help is processed by cleandoc(), which dedents all lines except the first one by as much as it can.

Copy link
Member

Choose a reason for hiding this comment

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

Got it. Would be nice if the test could be more obviously comparing the deprecated and non-deprecated docstrings, but that's probably not worth the effort to change now.

@mattip
Copy link
Member

mattip commented Feb 14, 2019

@wrwrwr feel like finishing this up?

@wrwrwr wrwrwr force-pushed the deprecate-formatting branch from 95bab2c to 19208f0 Compare February 17, 2019 18:00
Copy link
Member

@eric-wieser eric-wieser 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 a nit left

indent = len(line) - content
margin = min(margin, indent)
if margin == sys.maxsize:
margin = 0
Copy link
Member

Choose a reason for hiding this comment

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

nit: could extra lines 112 through 119 as a helper function _get_indent(lines[1:])

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the review. I've cleaned up the code and the tests a bit further.

@wrwrwr wrwrwr force-pushed the deprecate-formatting branch from 25117d0 to af8dda1 Compare February 17, 2019 21:21
@mattip mattip merged commit bef9271 into numpy:master Mar 10, 2019
@mattip
Copy link
Member

mattip commented Mar 10, 2019

Thanks @wrwrwr

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants