Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@

# -- sphinx.ext.autodoc configuration ------------------------------------------------
# https://www.sphinx-doc.org/en/master/usage/extensions/autodoc.html
autoclass_content = "both"
autoclass_content = "class"
autodoc_default_options = {
"ignore-module-all": True,
"no-inherited-members": "object,CaselessDict,str,bool,NamedTuple,int,float,Enum",
Expand Down
5 changes: 2 additions & 3 deletions docs/contribute/documentation/style-guide.rst
Original file line number Diff line number Diff line change
Expand Up @@ -340,9 +340,8 @@ The following docstrings items are the most frequently used in icalendar, althou
Don't write docstrings for a class's ``__init__`` or ``__new__`` methods, if they're present.
Instead, write a docstring for the class itself.

Sphinx will automatically append the ``__init__`` or ``__new__`` docstrings to the class's docstring, which may render the documentation in a confusing manner to the reader.

If you find docstrings in a class's ``__init__`` or ``__new__`` methods, then please create a pull request that moves and merges their content into the class's docstring, leaving their docstrings empty.
Sphinx renders only the class's docstring, so it won't show a docstring written for ``__init__`` or ``__new__``.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
Sphinx renders only the class's docstring, so it won't show a docstring written for ``__init__`` or ``__new__``.
Sphinx is configured to render only the class's docstring.
It won't render a docstring written for ``__init__`` or ``__new__``.

The test :file:`src/icalendar/tests/test_no_init_docstrings.py` fails if one of these methods has a docstring.

.. seealso::

Expand Down
1 change: 1 addition & 0 deletions news/1620.documentation
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Render only the class docstring in the API reference, test that no ``__init__`` or ``__new__`` method has a docstring of its own, and update the style guide to match. I used Claude Opus 5.5 in Claude Code to write the test, change the Sphinx setting, and compare the documentation build before and after. :user:`bernardhaddad`
73 changes: 73 additions & 0 deletions src/icalendar/tests/test_no_init_docstrings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
"""Classes document their constructor in the class docstring.

Sphinx renders the class docstring only (``autoclass_content = "class"`` in
``docs/conf.py``), so a docstring on ``__init__`` or ``__new__`` would not
appear in the documentation. Put it in the class docstring instead.

See https://github.com/collective/icalendar/issues/1620
"""

import importlib
import inspect
import pkgutil

import pytest

import icalendar


def get_classes():
"""Return every class defined in an icalendar module, tests excluded."""
classes = {}
for info in pkgutil.walk_packages(icalendar.__path__, prefix="icalendar."):
if ".tests" in info.name:
continue
module = importlib.import_module(info.name)
for _, cls in inspect.getmembers(module, inspect.isclass):
if cls.__module__ == info.name:
classes[f"{cls.__module__}.{cls.__qualname__}"] = cls
return [classes[name] for name in sorted(classes)]


def constructors_with_docstrings():
"""Return ``(class, method name)`` for each constructor with a docstring."""
found = []
for cls in get_classes():
for name in ("__init__", "__new__"):
method = cls.__dict__.get(name)
if isinstance(method, staticmethod):
method = method.__func__
code = getattr(method, "__code__", None)
if code is None or code.co_filename != inspect.getsourcefile(cls):
# Not written in this class's module, for example the __new__
# that Python generates for a NamedTuple.
continue
if method.__doc__:
found.append(f"{cls.__module__}.{cls.__qualname__}.{name}")
return found


def test_classes_are_found():
"""The search covers the package, so an empty result means something."""
names = {cls.__name__ for cls in get_classes()}
assert {"Calendar", "Event", "vText", "vRecur"} <= names


def test_no_constructor_has_a_docstring():
"""Constructor documentation belongs in the class docstring."""
assert constructors_with_docstrings() == []


@pytest.mark.parametrize("name", ["__init__", "__new__"])
def test_detects_a_constructor_docstring(name, monkeypatch):
"""The check fails when a constructor gets a docstring."""

def constructor(*args, **kwargs):
"""Documented in the wrong place."""

# Make it look as if it were written in the class's own module.
constructor.__code__ = constructor.__code__.replace(
co_filename=inspect.getsourcefile(icalendar.cal.calendar.Calendar)
)
monkeypatch.setattr(icalendar.cal.calendar.Calendar, name, constructor)
assert f"icalendar.cal.calendar.Calendar.{name}" in constructors_with_docstrings()