Repository navigation
Render only class docstrings; test constructors have none #2221
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
Open
bernardhaddad
wants to merge
1
commit into
PyCalendar:main
Choose a base branch
from
bernardhaddad:docs-autoclass-content-1620
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.