Repository navigation
Description
Describe the bug
Hello,
When using the usual make html, I faced the following issue (minimized):
...
TypeError: concatenate requires ndarray or scalar arguments, got <class 'jaxlib.xla_extension.ArrayImpl'> at position 0.
/export/home/eertmans/repositories/DiffeRT/DiffeRT2d/differt2d/scene.py:docstring of differt2d.scene.Scene.square_scene:20: WARNING: Exception occurred in plotting differt2d-scene-3
from /export/home/eertmans/repositories/DiffeRT/DiffeRT2d/docs/source/reference/differt2d.scene.rst:
Traceback (most recent call last):
File "/home/eertmans/.cache/pypoetry/virtualenvs/differt2d-4IUpg1Dz-py3.10/lib/python3.10/site-packages/matplotlib/sphinxext/plot_directive.py", line 481, in _run_code
exec(code, ns)
File "<string>", line 6, in <module>
File "/export/home/eertmans/repositories/DiffeRT/DiffeRT2d/differt2d/scene.py", line 602, in plot
[
File "/export/home/eertmans/repositories/DiffeRT/DiffeRT2d/differt2d/scene.py", line 603, in <listcomp>
emitter.plot(ax, *emitters_args, *args, **emitters_kwargs, **kwargs)
File "/export/home/eertmans/repositories/DiffeRT/DiffeRT2d/differt2d/geometry.py", line 254, in plot
[self.point[0]],
TypeError: 'ArrayImpl' object is not subscriptable
/export/home/eertmans/repositories/DiffeRT/DiffeRT2d/differt2d/scene.py:docstring of differt2d.scene.Scene.square_scene_with_obstacle:24: WARNING: Exception occurred in plotting differt2d-scene-4
from /export/home/eertmans/repositories/DiffeRT/DiffeRT2d/docs/source/reference/differt2d.scene.rst:
Traceback (most recent call last):
File "/home/eertmans/.cache/pypoetry/virtualenvs/differt2d-4IUpg1Dz-py3.10/lib/python3.10/site-packages/matplotlib/sphinxext/plot_directive.py", line 481, in _run_code
exec(code, ns)
File "<string>", line 6, in <module>
File "/export/home/eertmans/repositories/DiffeRT/DiffeRT2d/differt2d/scene.py", line 602, in plot
[
File "/export/home/eertmans/repositories/DiffeRT/DiffeRT2d/differt2d/scene.py", line 603, in <listcomp>
emitter.plot(ax, *emitters_args, *args, **emitters_kwargs, **kwargs)
File "/export/home/eertmans/repositories/DiffeRT/DiffeRT2d/differt2d/geometry.py", line 254, in plot
[self.point[0]],
TypeError: 'ArrayImpl' object is not subscriptable
looking for now-outdated files... none found
pickling environment... done
checking consistency... done
preparing documents... done
copying downloadable files... [ 8%] ../build/plot_directive/reference/differt2d-geo
copying downloadable files... [100%] ../build/plot_directive/reference/differt2d-scene-4.py
copying static files... done
copying extra files... done
done
writing output... [100%] reference/index
generating indices... genindex py-modindex done
highlighting module code... [100%] differt2d.utils
writing additional pages... search done
dumping search index in English (code: en)... done
dumping object inventory... done
build succeeded, 13 warnings.
The HTML pages are in build/html.As you can see, they say that ArrayImpl is not indexable. But self.point (and others) should not be a ArrayImpl, but rather some concrete array value. When running pytest and other commands, I get the expected output. When building with Sphinx<7.2, it works as expected. I don't know what triggers this ArrayImpl type to be used, and this may be a side effect of using Jax.
This issue is faced when combining Sphinx, jax and matplotlib's Sphinx directive altogether. The error was quite hard to pinpoint, and it is still quite vague to me, even after taking a look at the 7.2 release notes.
I think the issue comes from the Sphinx side, because changing the version requirements to Sphinx<7.2 fixed it all. Using pip freeze, there was no other relevant package that could cause this issue (to my understanding). This is why I post the issue in this repository, but please tell me if I just rather put it elsewhere.
How to Reproduce
This is not a real MWE, but the issue happened on my repo (https://github.com/jeertmans/DiffeRT2d), which you can see the different between this commit (not working) to this commit (working).
Build logs are available here for the failing version:
logs_281.zip
Environment Information
Platform: linux; (Linux-6.2.0-26-generic-x86_64-with-glibc2.35)
Python version: 3.10.12 (main, Jun 11 2023, 05:26:28) [GCC 11.4.0])
Python implementation: CPython
Sphinx version: 7.2.0
Docutils version: 0.20.1
Jinja2 version: 3.1.2
Pygments version: 2.16.1
Sphinx extensions
extensions = [
# Built-in
"sphinx.ext.autodoc",
"sphinx.ext.extlinks",
"sphinx.ext.intersphinx",
"sphinx.ext.mathjax",
"sphinx.ext.viewcode",
# Additional
"myst_parser",
"matplotlib.sphinxext.plot_directive", # Relevant one
"sphinxext.opengraph",
"sphinx_copybutton",
"sphinx_autodoc_typehints",
]Additional context
No response
Activity
AA-Turner commentedon Aug 28, 2023
Hi Jérome (@jeertmans),
Please would you be able to paste the full log mentioned in the failure message, and ideally also the untruncated
-Toutput?Additionally, please may you try to reduce the set of extensions as much as possible -- certainly removing
myst_parser, and ideally as much as possible. If this is an autodoc failure, ideally just a single.. auto<blah>::directive in an index.rst file would be very helpful, otherwise knowing which directive caused the failure etc, please.Thanks,
Adam
jeertmans commentedon Aug 29, 2023
Ok so I managed to have a MWE. The error is not the same, but it also occurs when upgrading to Sphinx 7.2.
pyproject.tomlsphinx_11652_mwe/__init__.pysource/conf.pysource/index.rstOutput from failing build:
Full traceback
I have attached the project files if you prefer.
sphinx-11652-mwe.zip
picnixz commentedon Aug 29, 2023
Ah it's maybe because of some
TYPE_CHECKINGguard I think. It appears to me that the original exception is the fact that_typeshedcannot be found and this appears to come from scipy.jeertmans commentedon Aug 29, 2023
That's what I thought, too, but what change in 7.2 could have cause that?
AA-Turner commentedon Aug 30, 2023
@jeertmans -- I'm not able to reproduce the error you're seeing. I get:
with:
Are you able to check without Poetry please? I ran:
python3.11 -m venv .venvand thenpip install sphinx jax[cpu] chex matplotlibin the activated venv (through WSL). I then ran Sphinx withsphinx-build -M html . _build -aET.A
jeertmans commentedon Aug 30, 2023
I get the exact same error message as you when I do not use poetry. But what is also weird is that when I
pip freezeand compare both environments (Poetry and-m venv), I get the exact same output.Anyway, having
ArrayImplinstead of a concrete array is still an issue...picnixz commentedon Aug 30, 2023
Not entirely sure, but I'm wondering how matplotlib actually executes the code. Maybe it is an issue with what the local namespace consists of? Also, could you like print the type of f.x and check what happens? (I never used the plot directive) and check the output of jnp.arange(...) in the classmethod itself.
Finally, is the chex.dataclass possibly responsible for this? can you use a real dataclass instead and real functions to create your dataclass (still with this Array annotation)?
jeertmans commentedon Aug 30, 2023
I guess so..
Printing
f.x(by raising aValueError, because Matplotlib captures the output otherwise):Changing
chex.dataclasstodataclasses.dataclassoutputs the same.picnixz commentedon Aug 30, 2023
What about jnp.arange? By the way, maybe it's an issue on jax then? Or matplotlib and its compatibility with jax (can you bisect the (Sphinx) commit responsibile for the issue?)
jeertmans commentedon Aug 30, 2023
This faulty (but quite lengthy) commit is: ad61e41.
jeertmans commentedon Aug 30, 2023
More precisely, the error can be fixed with this checkout:
git checkout 80ec9798612073be699c3fee04a1d6738303dc14 sphinx/ext/autodoc/importer.pyjeertmans commentedon Aug 30, 2023
It's quite unexpected, to me, that the autodoc importer would set
TYPE_CHECKINGtoTrue. This is what, I suppose, causes the error becausejaxis imported thinking the library is loaded for type checking, not actual computation purposes. Maybe that also their fault to have a different behavior when usingTYPE_CHECKINGtoo... I might create an issue on their repo!AA-Turner commentedon Aug 30, 2023
It's intentional, as there are often e.g. typing or collections.abc imports in the TYPE_CHECKING block, which are needed for documentation. However, it should be that failure to import with TYPE_CHECKING=True falls back to TYPE_CHECKING=False.
AA-Turner commentedon Aug 30, 2023
Running autodoc on this works:
But fails with Jax:
I suspect therefore the problem is with Jax -- they'll also break with runtime type checkers (e.g. pyanalyze), I believe.
A
jeertmans commentedon Aug 31, 2023
But here we are also talking about import within docstrings.
I totally get that importing with type checking enabled is importing when running auto doc on a module for example. But why importing the content of the docstring also with type checking set to True?
Usually, code in dosctring are code examples, not type documentation
picnixz commentedon Sep 6, 2023
@AA-Turner by looking at the different arguments in different issues, I think we should not set
TYPE_CHECKING=Trueand rather improve the ModuleAnalyzer, possibly by taking some ideas from MyPy or pylint for instance.While it does indeed reduce the capability of Sphinx to discover new modules, I think it's more likely that we would hit more and more issues if we were to blindly set
TYPE_CHECKING=True. One argument in favor of not doing this is that the PEP (600 something iirc) mentions that the value should be assumed False at runtime but true for static type checkers. In particular, these tools do not need to assume that the code within it successfully executes (e.g., importing from a .pyi file).IMO, we should make the feature experimental and disabled by default while highlighting its limitations. Alternatively, we could suggest "default" type aliases instead or an enhanced mechanism for type aliases.
For my personal projects, I either use
import collections.abcorfrom collections.abc import ..., so I think we could create presets that assume thatcollections.abc.*and members fromcollections.abcare implicitly imported. Same fortypingand possiblytyping_extensions. That way, people do not need to specify them in theautodoc_type_aliasesmap.This would only work for projects that only use "standard" types. For others, I think they should manually specify the type aliases to use. I n addition, I think we could enhance the current
autodoc_type_aliasesmap by allowing to define type aliases per module being documented. That way, I could still havefrom p import Userandfrom q import Userin two different files and specify that the first (resp. second) one mapsUsertop.User(resp.q.User).Ideally, we should rather develop our static analysis tool rather than relying on dynamic analysis since we could on techniques from other tools and avoid runtime surprises.
picnixz commentedon Sep 19, 2023
@jeertmans With #11679 being merged, I think you shouldn't have issues, so we may close the issue unless the issue persists.
jeertmans commentedon Sep 19, 2023
Is it already part of any release @picnixz?
AA-Turner commentedon Sep 19, 2023
7.2.6
jeertmans commentedon Sep 21, 2023
Ok, I can confirm that it works under 7.2.6, thanks for the patch!