From 451fe4192bea82730d16fea6f445952e8d624c2e Mon Sep 17 00:00:00 2001 From: bernardhaddad <87851931+bernardhaddad@users.noreply.github.com> Date: Sat, 5 Dec 2026 11:04:31 +0000 Subject: [PATCH] Render only class docstrings; test constructors have none Contributes to #1620. Set autoclass_content to "class", so the API reference shows the class docstring only. The pages for AdrFields, NFields and Alarm.Triggers lose the "Create new instance of ..." text that Python generates for a NamedTuple's __new__. Add test_no_init_docstrings.py: it walks every icalendar module and fails if a class's own __init__ or __new__ has a docstring. Constructors that Python generates (NamedTuple) are skipped, because their code is not in the class's module. Update the style guide to describe the new setup. AI use: Claude Opus 5.5 in Claude Code. Prompt (summary): find an open beginner issue in icalendar and prepare a pull request for it. Claude checked that no __init__ or __new__ docstrings remain in src/, wrote the test and checked that it fails when a docstring is added to vText.__new__, changed the Sphinx setting, updated the style guide, built the docs before and after the change to compare warnings and rendered pages, and ran the test suite and ruff. Output: this commit. Co-Authored-By: Claude Opus 5.5 --- docs/conf.py | 2 +- docs/contribute/documentation/style-guide.rst | 5 +- news/1620.documentation | 1 + .../tests/test_no_init_docstrings.py | 73 +++++++++++++++++++ 4 files changed, 77 insertions(+), 4 deletions(-) create mode 100644 news/1620.documentation create mode 100644 src/icalendar/tests/test_no_init_docstrings.py diff --git a/docs/conf.py b/docs/conf.py index b6ff4590d..d1516ddf2 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -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", diff --git a/docs/contribute/documentation/style-guide.rst b/docs/contribute/documentation/style-guide.rst index 72ad1a40f..8ae7c382d 100644 --- a/docs/contribute/documentation/style-guide.rst +++ b/docs/contribute/documentation/style-guide.rst @@ -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__``. + The test :file:`src/icalendar/tests/test_no_init_docstrings.py` fails if one of these methods has a docstring. .. seealso:: diff --git a/news/1620.documentation b/news/1620.documentation new file mode 100644 index 000000000..c4f256cad --- /dev/null +++ b/news/1620.documentation @@ -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` diff --git a/src/icalendar/tests/test_no_init_docstrings.py b/src/icalendar/tests/test_no_init_docstrings.py new file mode 100644 index 000000000..0e01820a5 --- /dev/null +++ b/src/icalendar/tests/test_no_init_docstrings.py @@ -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()