-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
Disallow module cycles in autosummary #6792
Conversation
Consider the following piece of ReST: .. automodule:: sphinx.ext.autosummary :members: .. autosummary:: sphinx.ext.autosummary.Autosummary This inserts an autosummary after the module docstring, but before the members of the module. Without the change in this commit, this would fail because `import_by_name` would attempt to import sphinx.ext.autosummary.sphinx.ext.autosummary.Autosumary because the prefix (from the parent) is `sphinx.ext.autosummary`, and the name is `sphinx.ext.autosummary.Autosummary`, which is able to be imported from `sphinx.ext.autosummary`, but is not the way that anyone would want to refer to it.
This comment was marked as outdated.
This comment was marked as outdated.
It would be working fine with this code:
IMO, this is an intended behavior of autodoc and autosummary. |
Thanks for the quick response @tk0miya! While your snippet does work properly, it renders differently:
While mine renders as:
which, in our case, is what we want. Is there some option of As an aside, another reason why I find this to be a bug is because it works the way that I want it to work unless you happen to import the top-level name in the file you're summarizing. For example, picking another random file, the following works the way that I was expecting: .. automodule:: sphinx.addnodes
:members:
.. autosummary::
sphinx.addnodes.translatable
sphinx.addnodes.toctree Even though this could instead be .. automodule:: sphinx.addnodes
:members:
.. autosummary::
translatable
toctree it works the way that I want solely because if prefix is not None and name.startswith(prefix):
# Disallow module cycles (e.g., sphinx.ext.sphinx.ext...)
raise SphinxError("summarized item should not include the current module") Another reason why I consider the current behavior a bug is that the >>> import sphinx.ext.autosummary.sphinx.ext.autosummary
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'sphinx.ext.autosummary.sphinx' While it is possible to do from sphinx.ext.autosummary import sphinx.ext.autosummary is a
import sphinx.ext.autosummary as parent
for name in ["sphinx", "ext", "autosummary"]:
parent = getattr(parent, name) which works, but implies that you can do It seems like this block is specifically designed to result in what I'm considering a bug here. Do you remember the intended use case that would result in going through this code path? |
Thanks @tbekolay! A |
This is causing existing builds to emit warnings, and fail when |
Subject: Fix bug with module cycles in autsummary
Feature or Bugfix
Purpose
Consider the following piece of ReST:
This inserts an autosummary after the module docstring, but before the members of the module. Without the change in this commit, this would fail because
import_by_name
would attempt to importbecause the prefix (from the parent) is
sphinx.ext.autosummary
, and the name issphinx.ext.autosummary.Autosummary
, which is able to be imported fromsphinx.ext.autosummary
with the above string, but is not the way that anyone would want to refer to it.I was using Sphinx master. I would make this PR on the 2.3 branch, but there doesn't seem to be one at the moment.
I would be happy to add a test to this PR, but it's not clear to me how the
@pytest.mark.sphinx
fixture/mark works ... is there any quick documentation on this? Basically I would like to be able to run Sphinx with the above piece of ReST to show that there is a bug before this commit and is fixed with this commit.You can reproduce this locally by replacing the contents of
doc/usage/extensions/autosummary.rst
withWithout this PR, the autosummary does not generate the correct link (but for some reason does not raise a warning, though it does for me in other projects). With this change it generates the correct link.