sphinx-doc / sphinx

The Sphinx documentation generator
https://www.sphinx-doc.org/
Other
6.22k stars 2.04k forks source link

`:meta private:` not honored after field containing indented text #11728

Open jwodder opened 9 months ago

jwodder commented 9 months ago

Describe the bug

The autodoc extension is supposed to hide members with the :meta private: info field, but if this field occurs after an info field containing indented text (even if there are non-indented fields in between), then :meta private: will not be honored.

How to Reproduce

(Complete MVCE project can be found at https://github.com/jwodder/sphinx-bug-20231017.)

index.rst:

.. automodule:: mvce

src/mvce/__init__.py:

def fibonacci(n: int) -> int:
    """Compute a Fibonacci number"""
    (a, b) = (0, 1)
    for _ in range(n):
        (a, b) = (b, a + b)
    return a

def afunction(n: int) -> str:
    """
    Do secret things.

    :param int n: Magic number
    :returns: Wouldn't you like to know?
    :raises ValueError:
        - if the moon is full
        - if I feel like it
    :meta private:
    """
    raise NotImplementedError

When the docs are built, afunction() will be included in the documentation despite the :meta private: field. If :meta private: is moved above the :raises: field or the :raises: field is deleted, afunction() will be absent when the docs are rebuilt.

Environment Information

Platform:              darwin; (macOS-14.0-x86_64-i386-64bit)
Python version:        3.11.6 (main, Oct  2 2023, 13:45:54) [Clang 15.0.0 (clang-1500.0.40.1)])
Python implementation: CPython
Sphinx version:        7.2.6
Docutils version:      0.20.1
Jinja2 version:        3.1.2
Pygments version:      2.16.1

Sphinx extensions

["sphinx.ext.autodoc", "sphinx.ext.intersphinx"]

Additional context

No response

picnixz commented 9 months ago

Ah I remember that. I think the trick is to put it at the top of the documentation and with newlines around. Not sure it will work though because I don't remember whether I already pushed a fix for that.

picnixz commented 6 months ago

@jwodder does adding newlines around :meta private: fix your issue?

jwodder commented 6 months ago

@picnixz Yes, but that shouldn't be necessary.

picnixz commented 6 months ago

It's probably something from docutils actually where each 'block' is delimited by newlines. It's needed for the state machine otherwise it incorrectly detects fields as being part of a group it should not (in your example, the :meta: would be part of the signature but it's actually for the whole docstring).

jwodder commented 6 months ago

@picnixz So you're saying that, when I don't add a newline, then the :meta: is associated with the function signature rather than the whole docstring, and that's why it doesn't do anything? Then why does :meta: take effect when I put it above the :raises: yet still in the same block, like this:

def afunction(n: int) -> str:
    """
    Do secret things.

    :param int n: Magic number
    :returns: Wouldn't you like to know?
    :meta private:
    :raises ValueError:
        - if the moon is full
        - if I feel like it
    """
    raise NotImplementedError
picnixz commented 6 months ago

Oh, then indeed it's an issue. I'll try investigating it as part of :meta: itself (https://github.com/sphinx-doc/sphinx/issues/11310).