Skip to content
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

[Doc] Use autodoc_mock_imports to mock deps for sphinx builds #38817

Merged

Conversation

peytondmurray
Copy link
Contributor

@peytondmurray peytondmurray commented Aug 24, 2023

Why are these changes needed?

This PR is the first in a series needed to update Sphinx to the latest version. In this PR, we

  • Remove a lot of external dependencies that aren't necessary for building the docs. The one exception to this is tune-sklearn, a project owned by ray-project which actually does have documentation hosted on the Ray docs site.
  • External dependencies are now mocked out using Sphinx's autodoc_mock_imports mechanism, which is used by both autodoc and autosummary. The old module mocking mechanism has been removed in favor of this. The one exception to this is packaging.version.Version: Ray currently uses Version to modify some behaviors depending on the version of certain dependencies that the user has installed. For example, in python/ray/train/torch/train_loop_utils.py:
import torch
from packaging.version import Version

if Version(torch.__version__) < Version("1.11.0"):
    FullyShardedDataParallel = None
else:
    from torch.distributed.fsdp import FullyShardedDataParallel

During the Sphinx build, if torch is mocked out, torch.__version__ is not a string, it's a Mock object, which is not a valid argument for packaging.version.Version - the build will fail. Although packaging is an external dependency and therefore should be mocked out using autodoc_mock_modules, Sphinx itself depends on it, and during the documentation build step the module is already loaded by the time conf.py gets executed, and therefore can't be mocked out in the usual way. I've added a dummy class which correctly handles Mock objects that get passed to it during documentation builds. I've also added a check for any module specified in autodoc_mock_modules to ensure that mock targets aren't yet loaded when conf.py is executed, which hopefully saves the next person to touch this a lot of pain.

Related issue number

Partially addresses #37944.

Checks

  • I've signed off every commit(by using the -s flag, i.e., git commit -s) in this PR.
  • I've run scripts/format.sh to lint the changes in this PR.
  • I've included any doc changes needed for https://docs.ray.io/en/master/.
    • I've added any new APIs to the API Reference. For example, if I added a
      method in Tune, I've added it in doc/source/tune/api/ under the
      corresponding .rst file.
  • I've made sure the tests are passing. Note that there might be a few flaky tests, see the recent failures at https://flakey-tests.ray.io/
  • Testing Strategy
    • Unit tests
    • Release tests
    • This PR is not tested :(

@peytondmurray peytondmurray force-pushed the docs-autodoc-mock-modules branch 2 times, most recently from 32b584a to 0e90f69 Compare August 24, 2023 17:40
@peytondmurray peytondmurray force-pushed the docs-autodoc-mock-modules branch from 0e90f69 to 75c215d Compare August 24, 2023 19:13
@peytondmurray peytondmurray changed the title [WIP] [Doc] Use autodoc_mock_modules to mock deps for sphinx builds [WIP] [Doc] Use autodoc_mock_imports to mock deps for sphinx builds Aug 24, 2023
@peytondmurray peytondmurray force-pushed the docs-autodoc-mock-modules branch from 75c215d to 42f6c3b Compare August 24, 2023 19:46
@peytondmurray
Copy link
Contributor Author

There are some differences between this PR and the current master build. Normally this would be a bad thing because we aren't changing anything user-facing here, but so far I think this only solves bugs in the docs. For example, switching to use autodoc_mock_imports fixed these type names. Here's the master build:

rllib-models-master

And here's this branch:

rllib-models-PR

I'm going to spend a bit longer scanning the differences to see if there's anything bad here. @angelinalg if you have time, I'd appreciate feedback as well.

@peytondmurray
Copy link
Contributor Author

Here's another example of an issue on master that gets fixed here:

image

and on the branch:

image

@peytondmurray peytondmurray force-pushed the docs-autodoc-mock-modules branch 5 times, most recently from 401e96b to 65bd69f Compare August 25, 2023 07:26
Signed-off-by: pdmurray <peynmurray@gmail.com>
@peytondmurray peytondmurray force-pushed the docs-autodoc-mock-modules branch 3 times, most recently from 82c5704 to 33cd701 Compare August 29, 2023 00:08
@maxpumperla
Copy link
Contributor

@peytondmurray this looks great, thanks so much! we should merge this asap, once we removed the rtd build issues.

@peytondmurray peytondmurray force-pushed the docs-autodoc-mock-modules branch 2 times, most recently from 5b6c060 to 22e4ce6 Compare August 29, 2023 17:09
Signed-off-by: pdmurray <peynmurray@gmail.com>
@peytondmurray peytondmurray force-pushed the docs-autodoc-mock-modules branch from 22e4ce6 to c4a5eaf Compare August 29, 2023 18:11
Signed-off-by: pdmurray <peynmurray@gmail.com>
@peytondmurray
Copy link
Contributor Author

Okay, the RTD build is now working - I had to make sure not to import ray in conf.py because you can't autodoc_mock_imports on anything that is already initialized. The build now works!

Signed-off-by: pdmurray <peynmurray@gmail.com>
@peytondmurray
Copy link
Contributor Author

After a discussion with @maxpumperla I'm marking this as ready for review. We still are doing ad-hoc mocking of compiled ray modules, which still display as <MagicMock ...> in the docs, but all of the modules that have been moved into autodoc_mock_imports have improved the docs already.

Future effort: sphinx-doc/sphinx#4413 should allow us to put ray._raylet inside autodoc_mock_imports without mocking ray itself - need to investigate this going forward.

@peytondmurray peytondmurray changed the title [WIP] [Doc] Use autodoc_mock_imports to mock deps for sphinx builds [Doc] Use autodoc_mock_imports to mock deps for sphinx builds Aug 30, 2023
@peytondmurray peytondmurray added the docs An issue or change related to documentation label Aug 30, 2023
@peytondmurray peytondmurray marked this pull request as ready for review August 30, 2023 17:01
@peytondmurray peytondmurray requested a review from a team as a code owner August 30, 2023 17:01
Copy link
Contributor

@maxpumperla maxpumperla left a comment

Choose a reason for hiding this comment

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

LGTM, approving pending a green CI run and the comment below.

Copy link
Member

@bveeramani bveeramani left a comment

Choose a reason for hiding this comment

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

Nice!

@@ -65,7 +65,7 @@ make linkcheck
To run tests for examples shipping with docstrings in Python files, run the following command:

```shell
RAY_MOCK_MODULES=0 make doctest
make doctest
Copy link
Member

Choose a reason for hiding this comment

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

We should remove this section or replace it with the information in How to write code snippets. We don't use make doctest anymore.

@bveeramani bveeramani added the tests-ok The tagger certifies test failures are unrelated and assumes personal liability. label Sep 5, 2023
@bveeramani bveeramani merged commit 74e97de into ray-project:master Sep 5, 2023
@peytondmurray peytondmurray deleted the docs-autodoc-mock-modules branch September 6, 2023 21:58
GeneDer pushed a commit to GeneDer/ray that referenced this pull request Sep 8, 2023
…oject#38817)

This PR is the first in a series needed to update Sphinx to the latest version. In this PR, we

- Remove a lot of external dependencies that aren't necessary for building the docs. The one exception to this is tune-sklearn, a project owned by ray-project which actually does have documentation hosted on the Ray docs site.
- External dependencies are now mocked out using Sphinx's autodoc_mock_imports mechanism, which is used by both autodoc and autosummary. The old module mocking mechanism has been removed in favor of this. The one exception to this is packaging.version.Version: Ray currently uses Version to modify some behaviors depending on the version of certain dependencies that the user has installed.

---------

Signed-off-by: pdmurray <peynmurray@gmail.com>
Co-authored-by: Max Pumperla <max.pumperla@googlemail.com>
Signed-off-by: Gene Su <e870252314@gmail.com>
GeneDer added a commit that referenced this pull request Sep 9, 2023
#39485)

This PR is the first in a series needed to update Sphinx to the latest version. In this PR, we

- Remove a lot of external dependencies that aren't necessary for building the docs. The one exception to this is tune-sklearn, a project owned by ray-project which actually does have documentation hosted on the Ray docs site.
- External dependencies are now mocked out using Sphinx's autodoc_mock_imports mechanism, which is used by both autodoc and autosummary. The old module mocking mechanism has been removed in favor of this. The one exception to this is packaging.version.Version: Ray currently uses Version to modify some behaviors depending on the version of certain dependencies that the user has installed.

---------

Signed-off-by: pdmurray <peynmurray@gmail.com>
Signed-off-by: Gene Su <e870252314@gmail.com>
Co-authored-by: Peyton Murray <peynmurray@gmail.com>
Co-authored-by: Max Pumperla <max.pumperla@googlemail.com>
jimthompson5802 pushed a commit to jimthompson5802/ray that referenced this pull request Sep 12, 2023
…oject#38817)

This PR is the first in a series needed to update Sphinx to the latest version. In this PR, we

- Remove a lot of external dependencies that aren't necessary for building the docs. The one exception to this is tune-sklearn, a project owned by ray-project which actually does have documentation hosted on the Ray docs site.
- External dependencies are now mocked out using Sphinx's autodoc_mock_imports mechanism, which is used by both autodoc and autosummary. The old module mocking mechanism has been removed in favor of this. The one exception to this is packaging.version.Version: Ray currently uses Version to modify some behaviors depending on the version of certain dependencies that the user has installed.

---------

Signed-off-by: pdmurray <peynmurray@gmail.com>
Co-authored-by: Max Pumperla <max.pumperla@googlemail.com>
Signed-off-by: Jim Thompson <jimthompson5802@gmail.com>
vymao pushed a commit to vymao/ray that referenced this pull request Oct 11, 2023
…oject#38817)

This PR is the first in a series needed to update Sphinx to the latest version. In this PR, we

- Remove a lot of external dependencies that aren't necessary for building the docs. The one exception to this is tune-sklearn, a project owned by ray-project which actually does have documentation hosted on the Ray docs site.
- External dependencies are now mocked out using Sphinx's autodoc_mock_imports mechanism, which is used by both autodoc and autosummary. The old module mocking mechanism has been removed in favor of this. The one exception to this is packaging.version.Version: Ray currently uses Version to modify some behaviors depending on the version of certain dependencies that the user has installed.

---------

Signed-off-by: pdmurray <peynmurray@gmail.com>
Co-authored-by: Max Pumperla <max.pumperla@googlemail.com>
Signed-off-by: Victor <vctr.y.m@example.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs An issue or change related to documentation tests-ok The tagger certifies test failures are unrelated and assumes personal liability.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants