Skip to content

DOC: Add >>> import numpy as np stubs everywhere#26814

Merged
mattip merged 21 commits intonumpy:mainfrom
agriyakhetarpal:add-import-numpy-as-np-statement
Jul 24, 2024
Merged

DOC: Add >>> import numpy as np stubs everywhere#26814
mattip merged 21 commits intonumpy:mainfrom
agriyakhetarpal:add-import-numpy-as-np-statement

Conversation

@agriyakhetarpal
Copy link
Contributor

@agriyakhetarpal agriyakhetarpal commented Jun 28, 2024

Description

This PR is a precursor to gh-26745. It adds the >>> import numpy as np statement in the docstrings and documentation-based examples everywhere wherever applicable to enable the use of the NumPy API under the np namespace. This aims to make examples self-contained, which will be helpful towards being able to run them in isolated mini-notebooks using Pyodide as created by the TryExamples directive via the jupyterlite-sphinx extension for Sphinx.

Changes made

  1. The addition of >>> import numpy as np statements in most files (certain files like _npyio_impl.py have been left out, please let me know if that file should be modified – it won't run well in Pyodide because of the lack of a filesystem to access, but the examples can be made self-contained in general and outside of Pyodide)
  2. General extra whitespace removals, changes to punctuation, and a few updates to grammar here and there

Additional context

@rgommers
Copy link
Member

Thanks @agriyakhetarpal. Note that the doc build CI is failing. Apologies in advance: I suggested splitting this off from your previous PR, but won't have time to actually review in detail over the next days.

Request to potential reviewers: this PR has the potential for a lot of bikeshedding. In case you are feeling an inclination to actually start such a process, I strongly recommend to think twice before doing that, and read the linked threads under "additional context" first.

@agriyakhetarpal agriyakhetarpal force-pushed the add-import-numpy-as-np-statement branch from 2ae139c to 347207f Compare June 28, 2024 17:45
@agriyakhetarpal agriyakhetarpal force-pushed the add-import-numpy-as-np-statement branch from de82e9e to 811830b Compare June 28, 2024 18:37
@agriyakhetarpal
Copy link
Contributor Author

Not sure if I understand the "Diverging branches can't be fast-forwarded" error in CircleCI with the 128 exit code. My branch and my commits seem fine, so it's most likely a downtime incident with GitHub.

@agriyakhetarpal
Copy link
Contributor Author

agriyakhetarpal commented Jul 1, 2024

There was an incident for GitHub repo clones on Friday, which seems to have been resolved today, but there's a different error in CircleCI from conf.py with the line: assert 'numpy._core._add_newdocs_scalars' not in sys.modules. I can't reproduce this locally with spin docs --clean – I'll keep investigating. If someone could re-trigger the CircleCI job for this commit, that would be great (or I can push some commits with further changes).

@agriyakhetarpal
Copy link
Contributor Author

The docs build has been fixed in main, I updated my branch and I added a few commits for the user guide. Many of the rest of the pages under the user guide are better poised to be converted to Jupytext-based notebooks as discussed in #26745 (comment) (under the "Key changes" section).

@@ -191,29 +191,31 @@ Inexact types
This means that variables with equal binary values but whose datatypes are of
different precisions may display differently::
Copy link
Member

Choose a reason for hiding this comment

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

Although maybe it's just the use of :: here that's problematic.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, I noticed this issue while working on #26745 in many places. Would it be better to handle such changes here? It would reduce the diff there, but I think there's no way to test the rendering of the examples out without having integrated jupyterlite-sphinx, so we might as well test it there better at the expense of a larger diff.

Copy link
Member

Choose a reason for hiding this comment

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

If the plan is to merge this PR and then that one, I'd prefer to not knowingly make some examples not render very nicely. Just in case they get missed or that PR takes a long time to land for whatever reason. I don't mind making the diff a bit longer to sort out that, I think for this specific example all that's needed is to delete the :: and it will automatically get the nice example markup you changed the docs to use elsewhere in this PR.

Copy link
Member

@ngoldbaum ngoldbaum left a comment

Choose a reason for hiding this comment

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

I really like the new example blocks that show up if you adjust the indentation for long examples! I think there's just one where it's not rendering correctly, see inline comment.

Overall I really like this change and distributed over the entire community I think this will save a lot of human time where currently people need to add the import after copy/pasting code from the docs and seeing an error.

@ngoldbaum
Copy link
Member

image

Compare with: https://numpy.org/doc/stable/reference/random/index.html

@agriyakhetarpal
Copy link
Contributor Author

Compare with: numpy.org/doc/stable/reference/random/index.html

I restored this change to the original in 25f9a99. It's not neat, but adding the TryExamples directive will fix this indentation problem.

@mattip
Copy link
Member

mattip commented Jul 24, 2024

There is a merge conflict, I guess that is to be expected in a PR this big. Perhaps we could push this over the finish line, and fix any lingering indentation problems in follow-on PRs?

@agriyakhetarpal
Copy link
Contributor Author

Thanks, @mattip – I resolved the merge conflicts, and this is ready for review again. I, too, suggest fixing indentation problems in follow-on PRs.

@mattip mattip merged commit 128490c into numpy:main Jul 24, 2024
@mattip
Copy link
Member

mattip commented Jul 24, 2024

Thanks @agriyakhetarpal

@mattip
Copy link
Member

mattip commented Jul 24, 2024

I wonder if there is a regex we could use on the rendered documentation to find indentation problems? Looking at the random example with extra indentation it seems the problem is that a <div class="doctest highlight-default notranslate"> ... </div> code block is contained inside a <blockquote>...</blockquote> block. Can we search for excessive use of blockquote?

@agriyakhetarpal agriyakhetarpal deleted the add-import-numpy-as-np-statement branch July 24, 2024 16:05
@agriyakhetarpal
Copy link
Contributor Author

This is a good idea, I can look into doing this. With this PR merged, I'll resume my work on gh-26745 and fix any cases of improper indentation.

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