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.

Test docstrings for parameters are equal

See original GitHub issue

We recently merged a test that checks consistency between parameters in function signatures and their docstrings using sklearn.utils.testing.check_docstring_parameters. I would like to have a function in sklearn.utils.testing which similarly makes use of numpydoc to check that parts of docstrings are identical among a set of objects. I would expect this helper to eventually be contributed back to numpydoc.

It might look something like:

def assert_consistent_docs(objects,
                           include_params=None, exclude_params=None,
                           include_attribs=None, exclude_attribs=None
                           include_returns=None, exclude_returns=None):
    """

    Checks if types and descriptions of parameters, etc, are identical across
    objects. ``object``s may either be ``NumpyDocString`` instances or objects
    (classes, functions, descriptors) with docstrings that can be parsed as
    numpydoc.

    By default it asserts that any Parameters/Returns/Attributes entries having the
    same name among ``objects`` docstrings also have the same type
    specification and description (ignoring whitespace).

    ``include_*`` and ``exclude_*`` parameters here are mutually exclusive,
    and specify a whitelist or blacklist, respectively, of parameter, attribute or
    return value names. May be '*' to include/exclude all from that section.
    """
    ... do stuff ...

Then we could call it with:

assert_consistent_docs([sklearn.metrics.precision_recall_fscore_support,
                        sklearn.metrics.precision_score,
                        sklearn.metrics.recall_score,
                        sklearn.metrics.f1_score,
                        sklearn.metrics.fbeta_score],
                       exclude_returns='*')

This will ensure (by making a test fail when the condition is not met) that all these related scoring functions have identical parameter descriptions (whitespace excepted) wherever they have identical parameters. (I’ve not actually checked whether this is or should be true of all these metric functions.) Most importantly, having such an assertion means we can rest assured that when we change the documentation of some parameter or return value, we will be forced to do so consistently.

Issue Analytics

  • State:open
  • Created 6 years ago
  • Reactions:1
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
amanp10commented, Dec 6, 2017

I would like to work on this. Will try to get back with a PR in a few days.

0reactions
jnothmancommented, Dec 13, 2017

I don’t think so, as we want to force the user to be explicit about either inclusion or exclusion

Read more comments on GitHub >

github_iconTop Results From Across the Web

doctest — Test interactive Python examples — Python 3.11.1 ...
This section examines in detail how doctest works: which docstrings it looks at, how it finds interactive examples, what execution context it uses,...
Read more >
Python Docstring: Documenting And Introspecting Functions
A docstring generally describes what the object is doing, its parameters, and its return value. Q#3) How do you get a Python Docstring?...
Read more >
How to document a method with parameter(s)? - Stack Overflow
python doc strings are free-form, you can document it in any way you like. Examples: def mymethod(self, foo, bars): """ Does neat stuff!...
Read more >
How To Write Doctests in Python - DigitalOcean
Here, we'll add docstrings for the two arguments that are passed to the function and the value that is returned. The docstring will...
Read more >
Checking the format of docstrings with darglint
Their presence is checked using Checking the presence of docstrings with interrogate but the particular format they are written in is checked using...
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