Skip to content

Sphinx 7.2.5 breaks with PyO3: "modules may only be initialized once per interpreter process" #11662

Closed

Description

@pbybee

Describe the bug

New error message with this patch release. 7.2.4 succeeds in building the same documents.
PyO3 modules may only be initialized once per interpreter process

Full traceback
Traceback (most recent call last):
  File ".venv/lib/python3.10/site-packages/sphinx/cmd/build.py", line 298, in build_main
    app.build(args.force_all, args.filenames)
  File ".venv/lib/python3.10/site-packages/sphinx/application.py", line 351, in build
    self.builder.build_all()
  File ".venv/lib/python3.10/site-packages/sphinx/builders/__init__.py", line 252, in build_all
    self.build(None, summary=__('all source files'), method='all')
  File ".venv/lib/python3.10/site-packages/sphinx/builders/__init__.py", line 312, in build
    with logging.pending_warnings():
  File "/usr/lib/python3.10/contextlib.py", line 142, in __exit__
    next(self.gen)
  File ".venv/lib/python3.10/site-packages/sphinx/util/logging.py", line 222, in pending_warnings
    memhandler.flushTo(logger)
  File ".venv/lib/python3.10/site-packages/sphinx/util/logging.py", line 187, in flushTo
    logger.handle(record)
  File "/usr/lib/python3.10/logging/__init__.py", line 1634, in handle
    self.callHandlers(record)
  File "/usr/lib/python3.10/logging/__init__.py", line 1696, in callHandlers
    hdlr.handle(record)
  File "/usr/lib/python3.10/logging/__init__.py", line 964, in handle
    rv = self.filter(record)
  File "/usr/lib/python3.10/logging/__init__.py", line 821, in filter
    result = f.filter(record)
  File ".venv/lib/python3.10/site-packages/sphinx/util/logging.py", line 427, in filter
    raise exc
sphinx.errors.SphinxWarning: autodoc: failed to import module 'test' from module 'test'; the following exception was raised:
PyO3 modules may only be initialized once per interpreter process

How to Reproduce

install latest sphinx and run
sphinx-build -WaE docs/project docs/project/_build/html

Environment Information

Platform:              linux; (Linux-5.4.0-149-generic-x86_64-with-glibc2.31)
Python version:        3.10.12 (main, Jun  7 2023, 12:45:35) [GCC 9.4.0])
Python implementation: CPython
Sphinx version:        7.2.5
Docutils version:      0.18.1
Jinja2 version:        3.1.2
Pygments version:      2.16.1

Sphinx extensions

myst-parser==2.0.0

Additional context

No response

Activity

  1. AA-Turner commented on Aug 30, 2023

    @AA-Turner
    Member

    @pbybee Please provide a full reproducer. PyO3 is for rust projects, which you haven't mentioned in your description. Does the error appear with Sphinx 7.2.4?

    A

  2. changed the title [-]patch release 7.2.5 breaks document builds with PyO3 modules may only be initialized once per interpreter process[/-] [+]Sphinx 7.2.5 breaks with PyO3: "modules may only be initialized once per interpreter process"[/+] on Aug 30, 2023
  3. pbybee commented on Aug 30, 2023

    @pbybee
    Author

    No 7.2.4 builds correctly. We are not using PyO3 in our project. Perhaps it's a dependancy of sphinx that revved. I'll dig a little more as I don't have good repro steps at the moment without digging through the sphinx build process.

  4. AA-Turner commented on Aug 30, 2023

    @AA-Turner
    Member

    If you're able to bisect, try before/after this commit: 8248be3

  5. AA-Turner commented on Aug 30, 2023

    @AA-Turner
    Member

    Perhaps it's a dependancy of sphinx that revved.

    Very unlikely, more likely is that a dependency of the object you're documenting uses PyO3.

  6. picnixz commented on Aug 31, 2023

    @picnixz
    Member

    Can you check if the bad module contains a TYPE_CHECKING block and if any of the imports in this block contains some PyO3 object somewhere (or depends on it)? Could you also give us the dependencies of your project?

    As Adam pointed out in another issue where the feature of reloading a module with TYPE_CHECKING enabled makes jax fails, can you check the bad module with pyanalyze?

    @AA-Turner I am wondering, but for projects that would fail like that, could we perhaps make the TYPE_CHECKING feature optional? (it's probably impossible to make it optional per module due to dependencies that we would then need to build in advance).

  7. picnixz commented on Aug 31, 2023

    @picnixz
    Member

    Ok I know what happened.

    • Assume that you have a module using PyO3 say yay and a bad module boo
    • Assume you have a module a resp. b) importing yay (resp. yay and boo) in a TYPE_CHECKING block.
    • Import a: success for yay, failure for boo. We remove yay from sys.modules (the commit Adam referred to).
    • Import b: failure for yay even if sys.modules does not contain yay.

    Reason: PyO3 seems to import modules but does not seem to "unimport" them if they are deleted from sys.modules. As such, it's probably impossible for us to do anything about that.

    EDIT: I concluded as such by creating a module using PyO3, importing it, deleting it from.sys.modules, and re-importing it again and here is my configuration:

    Platform:              linux; (Linux-5.10.136-android12-9-26258365-abA336BXXS7CWG1-aarch64-with-libc)
    Python version:        3.11.4 (main, Jul  2 2023, 11:17:00) [Clang 14.0.7 (https://android.googlesource.com/toolchain/llvm-project 4c603efb0)
    Python implementation: CPython
    Sphinx version:        7.2.4
    Docutils version:      0.18.1
    Jinja2 version:        3.1.2
    Pygments version:      2.16.1
    
  8. adityagoel4512 commented on Aug 31, 2023

    @adityagoel4512

    This may not apply exclusively to PyO3. We are seeing a similar issue with importing the onnx package (which is built using Pybind11).

    WARNING: autodoc: failed to import module 'onnx.testing' from module <module-name>; the following exception was raised:
    Traceback (most recent call last):
      File "/opt/conda/envs/env-name/lib/python3.9/site-packages/sphinx/ext/autodoc/importer.py", line 65, in import_module
        return importlib.import_module(modname)
      File "/opt/conda/envs/env-name/lib/python3.9/importlib/__init__.py", line 127, in import_module
        return _bootstrap._gcd_import(name[level:], package, level)
    
    

    This error is not present in 7.2.4.

  9. picnixz commented on Aug 31, 2023

    @picnixz
    Member

    Actually, it's probably something related to how extension modules are imported. It appears that deleting them from sys.modules is not sufficient. By the way, what is the full traceback error? you are missing the exception message (or there is none?)

  10. jaraco commented on Aug 31, 2023

    @jaraco

    I've also encountered this issue in jaraco/skeleton#88 and I've produced a minimal reproducer in https://github.com/jaraco/jaraco.mongodb/tree/triage/36-minimal. Run with tox -e docs.

    In this case, the offending import is import inflect, which has a dependency on pydantic, which has a rust extension.

  11. picnixz commented on Aug 31, 2023

    @picnixz
    Member

    Mmh thank you. As I only have my phone for investing this, it will be hard. In the meantime, maybe the issue could be directly related to PyO3? Or, alternatively, for extension modules, maybe we could skip their removal, though they would still be leftovers modules... So I don't really know what to do (except pinning the version prior to 7.1.x. I wouldn't recommend 7.2.x as this is kind of subject to some unstability).

  12. jaraco commented on Aug 31, 2023

    @jaraco

    If you're able to bisect, try before/after this commit: 8248be3

    I installed the commit prior to that one and the issue remains.

    Correction - after ensuring that I'm forcing the reinstall, I have confirmed that commit is implicated:

     jaraco.mongodb triage/36-minimal @ .tox/docs/bin/pip install --force --quiet 'git+https://github.com/sphinx-doc/sphinx@8248be3'
      WARNING: Did not find branch or tag 'e494baa', assuming revision or ref.
     jaraco.mongodb triage/36-minimal @ tox -e docs
    .pkg: _optional_hooks> python /Users/jaraco/.local/pipx/venvs/tox/lib/python3.11/site-packages/pyproject_api/_backend.py True setuptools.build_meta
    .pkg: get_requires_for_build_editable> python /Users/jaraco/.local/pipx/venvs/tox/lib/python3.11/site-packages/pyproject_api/_backend.py True setuptools.build_meta
    .pkg: build_editable> python /Users/jaraco/.local/pipx/venvs/tox/lib/python3.11/site-packages/pyproject_api/_backend.py True setuptools.build_meta
    docs: install_package> python -I -m pip install --force-reinstall --no-deps /Users/jaraco/code/jaraco/jaraco.mongodb/.tox/.tmp/package/39/jaraco.mongodb-0.0.0-0.editable-py3-none-any.whl
    docs: commands[0] /Users/jaraco/code/jaraco/jaraco.mongodb/docs> python -m sphinx -W --keep-going . /Users/jaraco/code/jaraco/jaraco.mongodb/build/html
    Running Sphinx v7.2.5+/f90b3be
    loading pickled environment... done
    building [mo]: targets for 0 po files that are out of date
    writing output... 
    building [html]: targets for 1 source files that are out of date
    updating environment: 0 added, 1 changed, 0 removed
    reading sources... [100%] index
    WARNING: autodoc: failed to import module 'oplog' from module 'jaraco.mongodb'; the following exception was raised:
    PyO3 modules may only be initialized once per interpreter process
    looking for now-outdated files... none found
    pickling environment... done
    checking consistency... done
    preparing documents... done
    copying assets... copying static files... done
    copying extra files... done
    done
    writing output... [100%] index
    generating indices... genindex done
    writing additional pages... search done
    dumping search index in English (code: en)... done
    dumping object inventory... done
    build finished with problems, 1 warning.
    docs: exit 1 (0.63 seconds) /Users/jaraco/code/jaraco/jaraco.mongodb/docs> python -m sphinx -W --keep-going . /Users/jaraco/code/jaraco/jaraco.mongodb/build/html pid=49155
    .pkg: _exit> python /Users/jaraco/.local/pipx/venvs/tox/lib/python3.11/site-packages/pyproject_api/_backend.py True setuptools.build_meta
      docs: FAIL code 1 (1.15=setup[0.52]+cmd[0.63] seconds)
      evaluation failed :( (1.20 seconds)
     jaraco.mongodb triage/36-minimal @ .tox/docs/bin/pip install --quiet --force 'git+https://github.com/sphinx-doc/sphinx@8248be3~1'
      WARNING: Did not find branch or tag '8248be3~1', assuming revision or ref.
     jaraco.mongodb triage/36-minimal @ tox -e docs
    .pkg: _optional_hooks> python /Users/jaraco/.local/pipx/venvs/tox/lib/python3.11/site-packages/pyproject_api/_backend.py True setuptools.build_meta
    .pkg: get_requires_for_build_editable> python /Users/jaraco/.local/pipx/venvs/tox/lib/python3.11/site-packages/pyproject_api/_backend.py True setuptools.build_meta
    .pkg: build_editable> python /Users/jaraco/.local/pipx/venvs/tox/lib/python3.11/site-packages/pyproject_api/_backend.py True setuptools.build_meta
    docs: install_package> python -I -m pip install --force-reinstall --no-deps /Users/jaraco/code/jaraco/jaraco.mongodb/.tox/.tmp/package/41/jaraco.mongodb-0.0.0-0.editable-py3-none-any.whl
    docs: commands[0] /Users/jaraco/code/jaraco/jaraco.mongodb/docs> python -m sphinx -W --keep-going . /Users/jaraco/code/jaraco/jaraco.mongodb/build/html
    Running Sphinx v7.2.5+/f90b3be
    loading pickled environment... done
    building [mo]: targets for 0 po files that are out of date
    writing output... 
    building [html]: targets for 1 source files that are out of date
    updating environment: 0 added, 0 changed, 0 removed
    reading sources... 
    looking for now-outdated files... none found
    preparing documents... done
    copying assets... copying static files... done
    copying extra files... done
    done
    writing output... [100%] index
    generating indices... genindex py-modindex done
    writing additional pages... search done
    dumping search index in English (code: en)... done
    dumping object inventory... done
    build succeeded.
    
    The HTML pages are in ../build/html.
    .pkg: _exit> python /Users/jaraco/.local/pipx/venvs/tox/lib/python3.11/site-packages/pyproject_api/_backend.py True setuptools.build_meta
      docs: OK (1.16=setup[0.54]+cmd[0.62] seconds)
      congratulations :) (1.22 seconds)
    
  13. AA-Turner commented on Sep 1, 2023

    @AA-Turner
    Member

    cc @godlygeek as the author of #11645 if you've any ideas re PyO3/extension module unloading in general.

  14. AA-Turner commented on Sep 1, 2023

    @AA-Turner
    Member

    Another simple reproducer:

    import importlib, sys
    orig_modules = frozenset(sys.modules)
    module = importlib.import_module('pydantic')
    for m in [m for m in sys.modules if m not in orig_modules]:
        sys.modules.pop(m)
    module = importlib.import_module('pydantic')
    # ImportError: PyO3 modules may only be initialized once per interpreter process
  15. AA-Turner commented on Sep 1, 2023

    @AA-Turner
    Member

    See also https://github.com/PyO3/pyo3/blob/v0.19.2/guide/src/migration.md#each-pymodule-can-now-only-be-initialized-once-per-process

    cc: @davidhewitt -- sorry for the ping, but I wondered if you'd have any advice here? The problem is Sphinx tries to import modules twice (with TYPE_CHECKING set to True and then False on failure), but wants to reset sys.modules to the initial state in the case of a failed import, to work around corrupted partial imports (#11645).

    Is there an officially blessed way to unload a PyO3 module such that we're able to attempt importing it again? (Other ideas to work around this are very welcome also, I've not had much luck finding discussion on this point).

    Thanks,
    Adam

  16. 18 remaining items

  17. Luthaf commented on Sep 7, 2023

    @Luthaf

    I have something that looks similar to this as well in https://github.com/lab-cosmo/metatensor. The docs builds fine with sphinx 7.2.4, and fails with 7.2.5 with a numpy error that looks related to typechecking: numpy.core._exceptions._UFuncNoLoopError: ufunc 'equal' did not contain a loop with signature matching types (<class 'numpy.dtypes.Float64DType'>, <class 'numpy.dtypes.StrDType'>) -> None (full error below).

    Details
    sphinx-build -E -W -b html docs/src docs/build/html
    Sphinx v7.2.5 en cours d'exécution
    <metatensor>/.tox/docs/lib/python3.11/site-packages/breathe/project.py:116: RemovedInSphinx80Warning: Sphinx 8 will drop support for representing paths as strings. Use "pathlib.Path" or "os.fspath" instead.
      self._default_build_dir = os.path.dirname(app.doctreedir.rstrip(os.sep))
        Finished dev [unoptimized + debuginfo] target(s) in 0.05s
    chargement de l'inventaire intersphinx de https://docs.python.org/3/objects.inv...
    chargement de l'inventaire intersphinx de https://numpy.org/doc/stable/objects.inv...
    chargement de l'inventaire intersphinx de https://pytorch.org/docs/stable/objects.inv...
    Construction en cours [mo] : cibles périmées pour les fichiers po 0
    Écriture...
    Construction [html] : cibles périmées pour les fichiers sources 77
    Mise à jour de l'environnement : [nouvelle configuration] 77 ajouté(s), 0 modifié(s), 0 supprimé(s)
    Lecture des sources... [100%] reference/torch/tensor
    
    Warning, treated as error:
    autodoc: failed to import function 'checks_enabled' from module 'metatensor'; the following exception was raised:
    Traceback (most recent call last):
      File "<metatensor>/.tox/docs/lib/python3.11/site-packages/sphinx/ext/autodoc/importer.py", line 65, in import_module
        return importlib.import_module(modname)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      File "<miniforge3>/lib/python3.11/importlib/__init__.py", line 126, in import_module
        return _bootstrap._gcd_import(name[level:], package, level)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      File "<frozen importlib._bootstrap>", line 1204, in _gcd_import
      File "<frozen importlib._bootstrap>", line 1176, in _find_and_load
      File "<frozen importlib._bootstrap>", line 1147, in _find_and_load_unlocked
      File "<frozen importlib._bootstrap>", line 690, in _load_unlocked
      File "<frozen importlib._bootstrap_external>", line 940, in exec_module
      File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
      File "<metatensor>/.tox/docs/lib/python3.11/site-packages/metatensor/__init__.py", line 28, in <module>
        from . import core  # noqa
        ^^^^^^^^^^^^^^^^^^
      File "<metatensor>/.tox/docs/lib/python3.11/site-packages/metatensor/core/__init__.py", line 2, in <module>
        from .block import TensorBlock
      File "<metatensor>/.tox/docs/lib/python3.11/site-packages/metatensor/core/block.py", line 6, in <module>
        from ._c_lib import _get_library
      File "<metatensor>/.tox/docs/lib/python3.11/site-packages/metatensor/core/_c_lib.py", line 8, in <module>
        from .data.extract import ExternalCpuArray, register_external_data_wrapper
      File "<metatensor>/.tox/docs/lib/python3.11/site-packages/metatensor/core/data/__init__.py", line 1, in <module>
        from .array import ArrayWrapper  # noqa
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      File "<metatensor>/.tox/docs/lib/python3.11/site-packages/metatensor/core/data/array.py", line 3, in <module>
        import numpy as np
      File "<metatensor>/.tox/docs/lib/python3.11/site-packages/numpy/__init__.py", line 144, in <module>
        from . import lib
      File "<metatensor>/.tox/docs/lib/python3.11/site-packages/numpy/lib/__init__.py", line 25, in <module>
        from . import index_tricks
      File "<metatensor>/.tox/docs/lib/python3.11/site-packages/numpy/lib/index_tricks.py", line 12, in <module>
        import numpy.matrixlib as matrixlib
      File "<metatensor>/.tox/docs/lib/python3.11/site-packages/numpy/matrixlib/__init__.py", line 4, in <module>
        from . import defmatrix
      File "<metatensor>/.tox/docs/lib/python3.11/site-packages/numpy/matrixlib/defmatrix.py", line 12, in <module>
        from numpy.linalg import matrix_power
      File "<metatensor>/.tox/docs/lib/python3.11/site-packages/numpy/linalg/__init__.py", line 73, in <module>
        from . import linalg
      File "<metatensor>/.tox/docs/lib/python3.11/site-packages/numpy/linalg/linalg.py", line 37, in <module>
        from numpy._typing import NDArray
      File "<metatensor>/.tox/docs/lib/python3.11/site-packages/numpy/_typing/__init__.py", line 207, in <module>
        from ._ufunc import (
    ModuleNotFoundError: No module named 'numpy._typing._ufunc'
    
    The above exception was the direct cause of the following exception:
    
    Traceback (most recent call last):
      File "<metatensor>/.tox/docs/lib/python3.11/site-packages/sphinx/ext/autodoc/importer.py", line 90, in import_object
        module = import_module(modname, warningiserror=warningiserror)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      File "<metatensor>/.tox/docs/lib/python3.11/site-packages/sphinx/ext/autodoc/importer.py", line 69, in import_module
        raise ImportError(exc, traceback.format_exc()) from exc
    ImportError: (ModuleNotFoundError("No module named 'numpy._typing._ufunc'"), 'Traceback (most recent call last):\n  File "<metatensor>/.tox/docs/lib/python3.11/site-packages/sphinx/ext/autodoc/importer.py", line 65, in import_module\n    return importlib.import_module(modname)\n           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n  File "<miniforge3>/lib/python3.11/importlib/__init__.py", line 126, in import_module\n    return _bootstrap._gcd_import(name[level:], package, level)\n           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n  File "<frozen importlib._bootstrap>", line 1204, in _gcd_import\n  File "<frozen importlib._bootstrap>", line 1176, in _find_and_load\n  File "<frozen importlib._bootstrap>", line 1147, in _find_and_load_unlocked\n  File "<frozen importlib._bootstrap>", line 690, in _load_unlocked\n  File "<frozen importlib._bootstrap_external>", line 940, in exec_module\n  File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed\n  File "<metatensor>/.tox/docs/lib/python3.11/site-packages/metatensor/__init__.py", line 28, in <module>\n    from . import core  # noqa\n    ^^^^^^^^^^^^^^^^^^\n  File "<metatensor>/.tox/docs/lib/python3.11/site-packages/metatensor/core/__init__.py", line 2, in <module>\n    from .block import TensorBlock\n  File "<metatensor>/.tox/docs/lib/python3.11/site-packages/metatensor/core/block.py", line 6, in <module>\n    from ._c_lib import _get_library\n  File "<metatensor>/.tox/docs/lib/python3.11/site-packages/metatensor/core/_c_lib.py", line 8, in <module>\n    from .data.extract import ExternalCpuArray, register_external_data_wrapper\n  File "<metatensor>/.tox/docs/lib/python3.11/site-packages/metatensor/core/data/__init__.py", line 1, in <module>\n    from .array import ArrayWrapper  # noqa\n    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n  File "<metatensor>/.tox/docs/lib/python3.11/site-packages/metatensor/core/data/array.py", line 3, in <module>\n    import numpy as np\n  File "<metatensor>/.tox/docs/lib/python3.11/site-packages/numpy/__init__.py", line 144, in <module>\n    from . import lib\n  File "<metatensor>/.tox/docs/lib/python3.11/site-packages/numpy/lib/__init__.py", line 25, in <module>\n    from . import index_tricks\n  File "<metatensor>/.tox/docs/lib/python3.11/site-packages/numpy/lib/index_tricks.py", line 12, in <module>\n    import numpy.matrixlib as matrixlib\n  File "<metatensor>/.tox/docs/lib/python3.11/site-packages/numpy/matrixlib/__init__.py", line 4, in <module>\n    from . import defmatrix\n  File "<metatensor>/.tox/docs/lib/python3.11/site-packages/numpy/matrixlib/defmatrix.py", line 12, in <module>\n    from numpy.linalg import matrix_power\n  File "<metatensor>/.tox/docs/lib/python3.11/site-packages/numpy/linalg/__init__.py", line 73, in <module>\n    from . import linalg\n  File "<metatensor>/.tox/docs/lib/python3.11/site-packages/numpy/linalg/linalg.py", line 37, in <module>\n    from numpy._typing import NDArray\n  File "<metatensor>/.tox/docs/lib/python3.11/site-packages/numpy/_typing/__init__.py", line 207, in <module>\n    from ._ufunc import (\nModuleNotFoundError: No module named \'numpy._typing._ufunc\'\n')
    
    During handling of the above exception, another exception occurred:
    
    Traceback (most recent call last):
      File "<metatensor>/.tox/docs/lib/python3.11/site-packages/sphinx/ext/autodoc/importer.py", line 65, in import_module
        return importlib.import_module(modname)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      File "<miniforge3>/lib/python3.11/importlib/__init__.py", line 126, in import_module
        return _bootstrap._gcd_import(name[level:], package, level)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      File "<frozen importlib._bootstrap>", line 1204, in _gcd_import
      File "<frozen importlib._bootstrap>", line 1176, in _find_and_load
      File "<frozen importlib._bootstrap>", line 1147, in _find_and_load_unlocked
      File "<frozen importlib._bootstrap>", line 690, in _load_unlocked
      File "<frozen importlib._bootstrap_external>", line 940, in exec_module
      File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
      File "<metatensor>/.tox/docs/lib/python3.11/site-packages/metatensor/__init__.py", line 28, in <module>
        from . import core  # noqa
        ^^^^^^^^^^^^^^^^^^
      File "<metatensor>/.tox/docs/lib/python3.11/site-packages/metatensor/core/__init__.py", line 2, in <module>
        from .block import TensorBlock
      File "<metatensor>/.tox/docs/lib/python3.11/site-packages/metatensor/core/block.py", line 6, in <module>
        from ._c_lib import _get_library
      File "<metatensor>/.tox/docs/lib/python3.11/site-packages/metatensor/core/_c_lib.py", line 8, in <module>
        from .data.extract import ExternalCpuArray, register_external_data_wrapper
      File "<metatensor>/.tox/docs/lib/python3.11/site-packages/metatensor/core/data/__init__.py", line 1, in <module>
        from .array import ArrayWrapper  # noqa
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      File "<metatensor>/.tox/docs/lib/python3.11/site-packages/metatensor/core/data/array.py", line 3, in <module>
        import numpy as np
      File "<metatensor>/.tox/docs/lib/python3.11/site-packages/numpy/__init__.py", line 387, in <module>
        _mac_os_check()
      File "<metatensor>/.tox/docs/lib/python3.11/site-packages/numpy/__init__.py", line 381, in _mac_os_check
        _ = polyfit(x, y, 2, cov=True)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^
      File "<metatensor>/.tox/docs/lib/python3.11/site-packages/numpy/lib/polynomial.py", line 669, in polyfit
        c, resids, rank, s = lstsq(lhs, rhs, rcond)
                             ^^^^^^^^^^^^^^^^^^^^^^
      File "<metatensor>/.tox/docs/lib/python3.11/site-packages/numpy/linalg/linalg.py", line 2303, in lstsq
        if rcond == "warn":
           ^^^^^^^^^^^^^^^
    numpy.core._exceptions._UFuncNoLoopError: ufunc 'equal' did not contain a loop with signature matching types (<class 'numpy.dtypes.Float64DType'>, <class 'numpy.dtypes.StrDType'>) -> None
    
    

    EDIT: we are only seeing this because we build docs with sphinx-build -W, other people might be affected but ignoring the warning.

  18. godlygeek commented on Sep 7, 2023

    @godlygeek
    Contributor

    Well, #11679 is an implementation of my second idea for how to make this work, leveraging importlib.reload().

    I'm not convinced that any of this is a good idea, and I think that leaving typing.TYPE_CHECKING alone is a better idea - or at the very least, making it opt-in for Sphinx to mess with it. TYPE_CHECKING is explicitly meant for static analysis, and setting it at runtime seems to just be asking for trouble.

    But, this new PR does seem to work in more cases than #11645 did. It handles pydantic, at least (I tested that locally, rather than introducing a new test dependency on pydantic). It also passes the tests we added for #11645. I think it's also more likely to leave things in a valid state if the reload fails, by virtue of loading with TYPE_CHECKING == False first, so that there's something valid already in sys.modules that we can leverage if reloading with TYPE_CHECKING == True fails.

  19. godlygeek commented on Sep 7, 2023

    @godlygeek
    Contributor

    @inducer Would you be able to test #11679 with grudge by any chance? I tried to reproduce with pip install grudge and .. automodule:: grudge but didn't get a failure even with Sphinx 7.2.5

  20. matthiasdiener commented on Sep 7, 2023

    @matthiasdiener

    @inducer Would you be able to test #11679 with grudge by any chance? I tried to reproduce with pip install grudge and .. automodule:: grudge but didn't get a failure even with Sphinx 7.2.5

    The grudge doc build still fails with #11679, with a new error message:

    WARNING: autodoc: failed to import function 'op.elementwise_min' from module 'grudge'; the following exception was raised:
    Traceback (most recent call last):
      File "/Users/mdiener/Work/emirge/sphinx/sphinx/ext/autodoc/importer.py", line 66, in import_module
        return importlib.import_module(modname)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      File "/Users/mdiener/Work/emirge/miniforge3/envs/ceesd/lib/python3.11/importlib/__init__.py", line 126, in import_module
        return _bootstrap._gcd_import(name[level:], package, level)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      File "<frozen importlib._bootstrap>", line 1204, in _gcd_import
      File "<frozen importlib._bootstrap>", line 1176, in _find_and_load
      File "<frozen importlib._bootstrap>", line 1147, in _find_and_load_unlocked
      File "<frozen importlib._bootstrap>", line 690, in _load_unlocked
      File "<frozen importlib._bootstrap_external>", line 940, in exec_module
      File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
      File "/Users/mdiener/Work/emirge/grudge/grudge/op.py", line 115, in <module>
        from grudge.trace_pair import (
      File "/Users/mdiener/Work/emirge/grudge/grudge/trace_pair.py", line 95, in <module>
        @dataclass_array_container
         ^^^^^^^^^^^^^^^^^^^^^^^^^
      File "/Users/mdiener/Work/emirge/arraycontext/arraycontext/container/dataclass.py", line 117, in dataclass_array_container
        raise ValueError(f"'{cls}' must have fields with array container type "
    ValueError: '<class 'grudge.trace_pair.TracePair'>' must have fields with array container type in order to use the 'dataclass_array_container' decorator
    
  21. godlygeek commented on Sep 7, 2023

    @godlygeek
    Contributor

    And it works with Sphinx 7.2.4? That's weird. Hm. Can you give me a recipe I can follow to reproduce that failure myself?

    I tried cloning https://github.com/inducer/grudge.git and doing a pip install path/to/clone, but things failed for me entirely differently than for you:

    WARNING: autodoc: failed to import module 'grudge'; the following exception was raised:
    cannot import name 'BoundaryTag' from 'meshmode.mesh' (/home/godlygeek/3ps/sphinx/venv/lib/python3.11/site-packages/meshmode/mesh/__init__.py)
    

    That's with meshmode 2021.2, and grudge's requirement is meshmode>=2020.2, so it ought to be compatible...

  22. matthiasdiener commented on Sep 7, 2023

    @matthiasdiener

    And it works with Sphinx 7.2.4? That's weird. Hm. Can you give me a recipe I can follow to reproduce that failure myself?

    Yes, it works with sphinx 7.2.4. I think the easiest way to install grudge and the correct dependencies might be via emirge:

    git clone https://github.com/illinois-ceesd/emirge
    cd emirge
    ./install.sh
    source config/activate_env.sh
    pip install matplotlib
    cd grudge/doc
    make html
    
  23. picnixz commented on Sep 7, 2023

    @picnixz
    Member

    Concerning the reason why we had this bug, I suggested the following: #11652 (comment)

  24. godlygeek commented on Sep 8, 2023

    @godlygeek
    Contributor

    Well, it looks like the issue with grudge is that it's using matplotlib.sphinxext.plot_directive, which does execute code from the module being documented. It does so after we've reloaded those modules, and some of the modules in this program don't take kindly to being reloaded.

    One of my assumptions with the "use importlib.reload" idea was that Sphinx wasn't going to actually try to call functions from the module, but matplotlib.sphinxext.plot_directive does.

    So... I'm not sure I have any good ideas at this point. I'm not seeing any reasonable way to import modules with typing.TYPE_CHECKING set that isn't going to badly break some things.

    For the sake of anyone stumbling upon this issue, it might be helpful to document that there's a reasonable workaround for both the problems caused by 7.2.0 and the different problems caused by 7.2.5: if you import the modules that you're trying to document yourself from inside your conf.py file, neither the 7.2.0 version of the TYPE_CHECKING reimport nor the 7.2.5 version will trigger. The already-loaded module will just get picked up from sys.modules with no additional magic.

  25. davidhewitt commented on Sep 11, 2023

    @davidhewitt

    Posted PyO3/pyo3#3446 - I'll try to ship this within a week and update Pydantic to use.

  26. picnixz commented on Sep 21, 2023

    @picnixz
    Member

    @pbybee With #11679 merged (7.2.6), I think your issue should not persist anymore. Could you confirm?

  27. Luthaf commented on Sep 21, 2023

    @Luthaf

    7.2.6 does fix the issue in my case (#11662 (comment)). Thanks a lot to all contributors!

  28. pbybee commented on Sep 22, 2023

    @pbybee
    Author

    Confirmed fixed in 7.2.6. Thanks for the hard work on this!

  29. locked as resolved and limited conversation to collaborators on Oct 23, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions