Skip to content

Commit

Permalink
Merge pull request #46 from WhyNotHugo/type-hints
Browse files Browse the repository at this point in the history
Add type hints to docstrings module
  • Loading branch information
timobrembeck authored Sep 25, 2023
2 parents 872cb8d + 5ae6bbc commit 09744e4
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 27 deletions.
6 changes: 6 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@
command_line = "-m pytest"
source = ["sphinxcontrib_django"]

[tool.coverage.report]
exclude_lines = [
"pragma: no cover",
"if TYPE_CHECKING:",
]

[tool.pytest.ini_options]
addopts = "-ra -vv --color=yes"
minversion = "6.0"
Expand Down
43 changes: 16 additions & 27 deletions sphinxcontrib_django/docstrings/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@
* Fix the intersphinx mappings to the Django documentation
(see :mod:`~sphinxcontrib_django.docstrings.patches`)
"""
from __future__ import annotations

import importlib
import os
from typing import TYPE_CHECKING

import django
from sphinx.errors import ConfigError
Expand All @@ -27,8 +30,13 @@
from .data import improve_data_docstring
from .methods import improve_method_docstring

if TYPE_CHECKING:
from sphinx.application import Sphinx
from sphinx.config import Config
from sphinx.ext.autodoc import Options


def setup(app):
def setup(app: Sphinx) -> dict:
"""
Allow this package to be used as Sphinx extension.
Expand All @@ -43,7 +51,6 @@ def setup(app):
:event:`config-inited` event.
:param app: The Sphinx application object
:type app: ~sphinx.application.Sphinx
"""
from .patches import patch_django_for_autodoc

Expand Down Expand Up @@ -86,18 +93,16 @@ def setup(app):
}


def setup_django(app, config):
def setup_django(app: Sphinx, config: Config) -> None:
"""
This function calls :func:`django.setup` so it doesn't have to be done in the app's
``conf.py``.
Called on the :event:`config-inited` event.
:param app: The Sphinx application object
:type app: ~sphinx.application.Sphinx
:param config: The Sphinx configuration
:type config: ~sphinx.config.Config
:raises ~sphinx.errors.ConfigError: If setting ``django_settings`` is not set correctly
"""
Expand All @@ -121,27 +126,20 @@ def setup_django(app, config):
app.emit("django-configured")


def autodoc_skip(app, what, name, obj, skip, options):
def autodoc_skip(
app: Sphinx, what: str, name: str, obj: object, options: Options, lines: list[str]
) -> bool | None:
"""
Hook to tell autodoc to include or exclude certain fields (see :event:`autodoc-skip-member`).
Sadly, it doesn't give a reference to the parent object,
so only the ``name`` can be used for referencing.
:param app: The Sphinx application object
:type app: ~sphinx.application.Sphinx
:param what: The parent type, ``class`` or ``module``
:type what: str
:param name: The name of the child method/attribute.
:type name: str
:param obj: The child value (e.g. a method, dict, or module reference)
:type obj: object
:param options: The current autodoc settings.
:type options: dict
"""
if name in EXCLUDE_MEMBERS:
return True
Expand All @@ -152,33 +150,24 @@ def autodoc_skip(app, what, name, obj, skip, options):
return None


def improve_docstring(app, what, name, obj, options, lines):
def improve_docstring(
app: Sphinx, what: str, name: str, obj: object, options: Options, lines: list[str]
) -> list[str]:
"""
Hook to improve the autodoc docstrings for Django models
(see :event:`autodoc-process-docstring`).
:param what: The type of the object which the docstring belongs to (one of ``module``,
``class``, ``exception``, ``function``, ``method`` and ``attribute``)
:type what: str
:param name: The fully qualified name of the object
:type name: str
:param obj: The documented object
:type obj: object
:param options: The options given to the directive: an object with attributes
``inherited_members``, ``undoc_members``, ``show_inheritance`` and ``noindex``
that are ``True`` if the flag option of same name was given to the auto
directive
:type options: object
:param lines: A list of strings – the lines of the processed docstring – that the event
handler can modify in place to change what Sphinx puts into the output.
:type lines: list [ str ]
:return: The modified list of lines
:rtype: list [ str ]
"""
if what == "class":
improve_class_docstring(app, obj, lines)
Expand Down

0 comments on commit 09744e4

Please sign in to comment.