8000 Write data parameter docs as regular parameter not as note (v2) by timhoffm · Pull Request #20291 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content

Write data parameter docs as regular parameter not as note (v2) #20291

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
May 27, 2021

Conversation

timhoffm
Copy link
Member

Replaces #19859. This approach uses an explicit template

    data : indexable object, optional
        # data_parameter_message

in the docstring and does not try to guess a reasonable insertion
position based on the docstring content. This is much simpler.

@@ -1084,6 +1086,8 @@ def vlines(self, x, ymin, ymax, colors=None, linestyles='solid',

Other Parameters
----------------
data : indexable object, optional
# data_parameter_message
Copy link
Member

Choose a reason for hiding this comment

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

This does seem easier to me. As-is it looks like a comment rather than something that will get magically filled it. Maybe:

Suggested change
# data_parameter_message
# _data_parameter_placeholder

I guess its not possible to just use our usual doc string interpolation here?

Copy link
Member Author
@timhoffm timhoffm May 25, 2021

Choose a reason for hiding this comment

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

Indeed, the # does not make much sense. I've gone with DATA_PARAMETER_PLACEHOLDER, which should reasonably look out-of-context to indicate it's not literally part of the docstring.

We cannot easily use regular docstring interpolation because the contents depends on the parameters of the @_preprocess_data decorator. I've also refrained from using string formatting, because that may interfere with other characters like { and % which may appear in the plain text.

Replaces matplotlib#19859. This approach uses an explicit template

```
    data : indexable object, optional
        DATA_PARAMETER_PLACEHOLDER
```

in the docstring and does not try to guess a reasonable insertion
position based on the docstring content. This is much simpler.
@timhoffm timhoffm force-pushed the data-kwarg-v2 branch 2 times, most recently from 55a8dde to d11234c Compare May 25, 2021 09:30
if "data : indexable object, optional" not in docstring:
_log.debug("data parameter docstring error: no data parameter")
if 'DATA_PARAMETER_PLACEHOLDER' not in docstring:
_log.debug("data parameter docstring error: missing placeholder")
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we should just throw (possibly via an assert) in that case? this would be consistent with what _preprocess_data does for invalid replace_names.
(In any case, the exception should never reach end users, only CI...)

Copy link
Member Author

Choose a reason for hiding this comment

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

I agree that the message should not reach end users. Particularly, I also don't want the checks to be run when the user imports matplotlib. I figured that tying execution to debug-logging is the easiest approach. Otherwise we'd need special flags or environment variables to check. I'm not worried that a user sees the message if he runs with debug logging. Debug logging outputs various other similarly technical messages.

Copy link
Contributor

Choose a reason for hiding this comment

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

If you worry about performance, I think you can simply do:

replaced = docstring.replace(..., ...)
assert replaced != docstring
return replaced

(without the in checks) as the string comparison should in practice be very fast as the strings won't even have the same length (which is checked first in the C-level unicode_compare_eq). This may well be competitive with the two attribute accesses in _log.level <= logging.DEBUG :-) (well, tbh I haven't checked)

(Then you don't check that there's "data : indexable..." but I think that's fine, especially as CI also checks that the result is proper numpydoc.)

Copy link
Member Author

Choose a reason for hiding this comment

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

I'm a bit hesitant with the assert. If something goes wrong, then matplotlib is not importable anymore.
And I don't think an ill-formatted docstring justifies terminating the library.

Copy link
Contributor

Choose a reason for hiding this comment

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

But this should really not reach the end user; if somehow we miss this on CI then something is likely very wrong with our release process...
In any case I'm not blocking this if others agree with logging instead of asserting.

Copy link
Member Author

Choose a reason for hiding this comment

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

I'm more concerned that someone outside our release process may tinker with the docstrings. E.g. someone could strip all docstrings for "performance reasons". While the specific case of a missing docstring is handled, it would break if all docstrings were replaced by "stripped". - Yes, that's an unlikely scenario. It appears to me that raising ever so slightly but unnecessarily increases the risk of breakage.

Copy link
Contributor

Choose a reason for hiding this comment

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

OK, let's get this in.

Copy link
Member
@jklymak jklymak 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 thats pretty clear now that it is a placeholder...

@anntzer anntzer merged commit d9ecf81 into matplotlib:master May 27, 2021
@timhoffm timhoffm deleted the data-kwarg-v2 branch May 27, 2021 09:11
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.

4 participants
0