Skip to content

DOC, TST: enable doctests - #12253

Merged
charris merged 8 commits into
numpy:masterfrom
tylerjereddy:enable_doctests
Dec 14, 2018
Merged

charris merged 8 commits into
numpy:masterfrom
tylerjereddy:enable_doctests

Conversation

@tylerjereddy

@tylerjereddy tylerjereddy commented Oct 23, 2018 •

Copy link
Copy Markdown
Contributor

Related to #9415, but using doctests proper--this is pretty painful, although I'm making progress & there are some real issues in the docstrings.

Using this command locally: python runtests.py -- --doctest-modules --ignore=numpy/ma/version.py --ignore=numpy/f2py/__main__.py --ignore=numpy/distutils/msvc9compiler.py --ignore=numpy/core/generate_numpy_api.py --ignore=numpy/core/cversions.py --ignore=numpy/testing -n 3 >& log.txt

To do:

  • resolve remaining doctest errors locally
  • hook up to CI with a 3.x doctest job

Comment thread numpy/core/_add_newdocs.py Resolved
Comment thread numpy/core/_add_newdocs.py Resolved
Comment thread numpy/core/_add_newdocs.py Resolved
Comment thread numpy/core/_add_newdocs.py Resolved
Comment thread numpy/core/_add_newdocs.py Resolved
Comment thread numpy/core/_add_newdocs.py Resolved
Comment thread numpy/core/numeric.py Resolved
@eric-wieser

Copy link
Copy Markdown
Member

I'm not sure I like the use of ellipsis for floats in the docstrings, as they make the reprs non-eval-able - although I don't know what the right answer there is.

Lots of other good stuff here, thanks!

@rgommers

Copy link
Copy Markdown
Member

I'm not sure I like the use of ellipsis for floats in the docstrings, as they make the reprs non-eval-able - although I don't know what the right answer there is.

I agree, that doesn't look quite right. Using the refguide checker as discussed in gh-9415 should fix that, it doesn't require exact repr equality but just that numerical values are within a given tolerance (uses assert_allclose under the hood IIRC). Because of this type of thing, I think that refguide checker is strongly preferred over using doctest.

Lots of other good stuff here, thanks!

+1

@tylerjereddy

Copy link
Copy Markdown
Contributor Author

I also wonder about just creating a lightweight doctest flag for the floating point comparisons rather than copying over the entire refguide_check.py module from SciPy. The process, I think, is to subclass OutputChecker from the standard library, which refguide does too, and use np.allclose as in refguide_check.py as mentioned above.

I guess one issue here is that NumPy can't depend on SciPy, so it would almost be better if the refguide checker base code lived in NumPy instead, and perhaps even better still if there were a general scientific refguide checker maintained externally to both for use in the ecosystem. It is a little ugly to have two copies of refguide_check.py in both NumPy and SciPy, although time investment for alternatives may be higher than the cost of the sync / maintenance burden I suppose.

Comment thread numpy/core/_add_newdocs.py Resolved
@tylerjereddy

Copy link
Copy Markdown
Contributor Author

Also worth noting that add_newdocs.py is sub-optimal for doctest error / trace reporting because of the source / doc dissonance. Historically it seems that the project had some custom doctest directives like #random, but the unit tests for those haven't been running it seems.

Indeed, we also have / have had our own custom ouput checkers, subclassed from doctest, living in numpy/testing/_private/noseclasses.py and NumpyDoctest calls nosetests. There's probably some complicated history there.

@rgommers

rgommers commented Nov 7, 2018

Copy link
Copy Markdown
Member

I also wonder about just creating a lightweight doctest flag for the floating point comparisons rather than copying over the entire refguide_check.py module from SciPy.

I'm afraid you'd be reinventing the wheel mostly. There's a reason for the SciPy tool being complex....

I guess one issue here is that NumPy can't depend on SciPy, so it would almost be better if the refguide checker base code lived in NumPy instead, and perhaps even better still if there were a general scientific refguide checker maintained externally to both for use in the ecosystem. It is a little ugly to have two copies of refguide_check.py in both NumPy and SciPy, although time investment for alternatives may be higher than the cost of the sync / maintenance burden I suppose.

Agreed, ideally we'd have this as a standalone package. Otherwise, syncing changes in script manually is also fine, not too big a deal (we do it with, e.g., runtests.py as well). Overall effort between those is probably similar, since packaging it up requires more docs, and changes to allow individual projects to add skip lists etc.

Thinking about it some more, beyond NumPy/SciPy effort - the package is a good idea. It would save the whole ecosystem a lot of time. For example, the reason the printing changes in NumPy 1.14.x were so painful for everyone was that doctest is terrible. If we'd ever have to make array repr changes again, we probably would only do so if we had this new package.

@ev-br

ev-br commented Nov 7, 2018 •

Copy link
Copy Markdown
Contributor

Agreed a small package living in the numpy org would be best. Allowing for per-project skip lists etc does not souns difficult. Whether the package can be used as an external dependency or will it need to be vendored (like numpydoc in scipy) remains to be seen --- I guess one needs to try adapring it for numpy and see :--).

@tylerjereddy

Copy link
Copy Markdown
Contributor Author

Now working with a crudely-adjusted version of SciPy's refguide_check module & working through the plethora of issues there. I'll have to backtrack a bit to remove my previously-introduced ellipses, so still quite some way from being ready for review.

Added the refguide check tentatively to Azure CI in this branch -- it sould fail with around 4400 lines of problematic output (it was 6000+ earlier today).

One thing I don't get is the magic behind printing array objects resulting in the output of random -- it seems I always need to replace the print(x) with simply x to display an array in doctest context.

Comment thread numpy/core/arrayprint.py Outdated

@tylerjereddy tylerjereddy Nov 9, 2018 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

print() will produce random here and elsewhere when handling an array

Comment thread numpy/lib/_datasource.py Resolved
@tylerjereddy

Copy link
Copy Markdown
Contributor Author
  • refguide_check is now passing in CI for this branch, so that's a good sign
  • I went through and tried to manually remove all cases where ellipses were being used for floating point comparisons (ouch), before we switched to refguide and random directives
  • of course more clean-up is needed with this big of a diff, but this should be looking much more respectable now

@tylerjereddy

Copy link
Copy Markdown
Contributor Author

numpy.core.tests.test_multiarray.TestMatmul stochastic failure on 32-bit?

@charris

charris commented Dec 5, 2018

Copy link
Copy Markdown
Member

That's a strange failure. Hmm.... I've restarted the azure builds.

@charris

charris commented Dec 5, 2018

Copy link
Copy Markdown
Member

The actual numbers look fine and I don't see any invalid values, so I suspect the warning came from somewhere else and somehow wasn't handled until that test. But it is worrisome. Do you know if the tests are threaded?

@charris

charris commented Dec 5, 2018

Copy link
Copy Markdown
Member

What happens if the tests encounter an invalid value in the doctests? In the "official" tests we suppress warnings when they don't matter.

@tylerjereddy

Copy link
Copy Markdown
Contributor Author

I think refguide is pretty careful about catching things. We're only running doctests in one of the CI jobs though, and that wasn't the one that was failing.

I haven't adjusted any linear algebra backend thread count settings, but note that our Windows builds in Azure have only recently started using openblas, and if there are 32/64-bit new openblas version builds we need to start pointing to, that might be a factor I suppose. Maybe I should bump to the appropriate openblas builds from the wheels after recent release / bump.

@charris charris added this to the 1.17.0 release milestone Dec 5, 2018
@charris

charris commented Dec 5, 2018

Copy link
Copy Markdown
Member

I'm a bit wary about trying to get this into 1.16, so giving it a 1.17 milestone so we have time to see and deal with any issues that may arise. It would probably be OK, maybe even desirable, to put in the docstring fixes and just leave the testing turned off until 1.17. Thoughts?

@tylerjereddy

Copy link
Copy Markdown
Contributor Author

Delay on release sounds reasonable I suppose.

Less keen on merging the doc fixes separately, especially if i.e., someone fixes the matmul docstring separate from this PR and then I end up having to resolve merge conflicts on this branch which marks that same docstring as SKIP for now & maybe even discover that their fix isn't quite right, and so on...

If people want to go that route, then I'd strongly suggest making 1.16 docstring-targeted fixes directly in this PR so they are subjected to the refguide CI immediately, etc.

Comment thread tools/refguide_check.py Outdated

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

It is important to check that this list of public NumPy submodules is reasonably complete. I just noticed that lib.recfunctions was previously absent, so refguide_check wasn't executing docstrings in that module, some of which were the subject of reviewer comments (probably diffs originated from before refguide switch when I was using vanilla doctests).

Also, even if I add lib.recfunctions, not all function docstrings therein are executed -- it seems I also need to add functions to __all__ in the module, but I'm hesitant do start doing that more broadly without reviewer approval first. Thoughts on this?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think skipping private functions, which should be everything not in __all__ with, or without the underscore, would probably be OK. If they are not intended to be private then they should be added. That said, lib.recfunctions has always been an oddball, as it is not imported at the top level.

@shoyer Not all functions in lib.recfunctions have dispatchers.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@charris looking through lib.recfunctions the functions without dispatchers seem to not take arrays as inputs (e.g., get_fieldspec). So these properly don't have dispatchers -- there's no array argument on which to call a dispatcher method (we don't have duck-typed dtypes). Let me know if there's anything in particular that you think I missed, though...

@charris

charris commented Dec 5, 2018 •

Copy link
Copy Markdown
Member

I was thinking of merging as is, but with the doctests turned off. You could then open another PR, turn it on, and continue making fixes. The advantage would be similarity between 1.16 and 1.17, the disadvantage would be loss of all the comments, which might be too high a price to pay.

Comment thread numpy/lib/mixins.py Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think we can remove the redundant >>> # here and keep these as normal text.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I think you're looking at an outdated diff -- there's even a badge above indicating as such.

I checked out numpy/lib/mixins from latest master so there shouldn't be a diff from your original code. That said, doctests probably aren't even running for lib.mixins until someone, probably me, adds it to the list of public modules in refguide.

Comment thread numpy/lib/mixins.py Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

can we preserve the line breaks I had before? That helped readability.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The code was reverted to master branch status already based on your previous comments.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Oops -- sorry about these outdated comments! I had drafted them before but not sent them -- and they just got sent along with my latest response to Chunk!

Comment thread tools/refguide_check.py Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@charris looking through lib.recfunctions the functions without dispatchers seem to not take arrays as inputs (e.g., get_fieldspec). So these properly don't have dispatchers -- there's no array argument on which to call a dispatcher method (we don't have duck-typed dtypes). Let me know if there's anything in particular that you think I missed, though...

@charris

charris commented Dec 6, 2018

Copy link
Copy Markdown
Member

So these properly don't have dispatchers --

Oh, my bad...

@mattip

mattip commented Dec 12, 2018

Copy link
Copy Markdown
Member

At some point _pytesttester.py needs to be updated to pass the doctests option through

@eric-wieser eric-wieser left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think there's enough good stuff here that we should go ahead and merge - this diff is too large to catch everything in, and even if there are a few points of disagreement, the docstrings are overall way more accurate.

The cost will at worst be intermittent CI failures, which we can either catch quickly, or resolve temporarily by disabling doctests again while we sort them out.

* ported the refguide_check module from SciPy for usage
in NumPy docstring execution/ verification; added the
refguide_check run to Azure Mac OS CI

* adjusted NumPy docstrings such that refguide_check passes
* restored regression comment in
numpy/core/defchararray.py

* fixed the dimensionality of the z
array in all() docstring in
numpy/core/fromnumeric.py; this isn't
detected because it is in-line with
variable memory addresses which are
tagged as variable for refguide

* byte_bounds() docstring adjusted
to reflect non-variable dtype
after reviewer requested removal
of complex dtype

* restore an original comment
in matmul docstring, as requested
by reviewer
* reviewer requested that the cases where
I switched from free-floating comments to
`>>> # comments` be reverted to free-floating
in docstrings
* added lib.recfunctions to refguide
PUBLIC_SUBMODULES, as the doctests
were otherwise not getting executed

* fixed a failing doctest in
recfunctions after above activation
@tylerjereddy

Copy link
Copy Markdown
Contributor Author

Ok, I rebased and force-pushed. If the CI remains all-green I suppose we could go for merge if others are ok with a sort of "2 steps forward, 1 step backward" approach, with smaller / focused PRs to follow.

I think @stefanv wants a "wish list" for refguide features/ fixes. There has been some discussion of eventually extracting to its own package for the community, but one thing at a time.

The current apparent opt-in instead of opt-out model seems a little annoying. On that note, I do see a few posts floating around online about combining coverage with doctests, one from Raymond Hettinger, and another pytest issue apparently indicating that this does actually work for doctests + coverage, but people aren't sure how the magic works.

@charris
charris merged commit e26c299 into numpy:master Dec 14, 2018
@charris

charris commented Dec 14, 2018

Copy link
Copy Markdown
Member

Thanks Tyler.

@tylerjereddy
tylerjereddy deleted the enable_doctests branch December 15, 2018 00:55
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.

7 participants