11"""
22This module contains all functions which are used to improve the documentation of classes.
33"""
4+ from __future__ import annotations
45
56from django import forms
67from django .db import models
8+ from sphinx .application import Sphinx
79from sphinx .pycode import ModuleAnalyzer
810
911from .field_utils import get_field_type , get_field_verbose_name
1012
1113
12- def improve_class_docstring (app , cls , lines ) :
14+ def improve_class_docstring (app : Sphinx , cls : type , lines : list [ str ]) -> None :
1315 """
1416 Improve the documentation of a class if it's a Django model or form
1517
1618 :param app: The Sphinx application object
17- :type app: ~sphinx.application.Sphinx
18-
1919 :param cls: The instance of the class to document
20- :type cls: object
21-
2220 :param lines: The docstring lines
23- :type lines: list [ str ]
2421 """
2522 if issubclass (cls , models .Model ):
2623 improve_model_docstring (app , cls , lines )
2724 elif issubclass (cls , forms .BaseForm ):
2825 improve_form_docstring (cls , lines )
2926
3027
31- def improve_model_docstring (app , model , lines ) :
28+ def improve_model_docstring (app : Sphinx , model : models . Model , lines : list [ str ]) -> None :
3229 """
3330 Improve the documentation of a Django :class:`~django.db.models.Model` subclass.
3431
3532 This adds all model fields as parameters to the ``__init__()`` method.
3633
3734 :param app: The Sphinx application object
38- :type app: ~sphinx.application.Sphinx
39-
4035 :param model: The instance of the model to document
41- :type model: ~django.db.models.Model
42-
4336 :param lines: The docstring lines
44- :type lines: list [ str ]
4537 """
4638
4739 # Add database table name
@@ -115,18 +107,13 @@ def improve_model_docstring(app, model, lines):
115107 lines .append (".. inheritance-diagram::" ) # pragma: no cover
116108
117109
118- def add_db_table_name (app , model , lines ) :
110+ def add_db_table_name (app : Sphinx , model : models . Model , lines : list [ str ]) -> None :
119111 """
120112 Format and add table name by extension configuration.
121113
122114 :param app: The Sphinx application object
123- :type app: ~sphinx.application.Sphinx
124-
125115 :param model: The instance of the model to document
126- :type model: ~django.db.models.Model
127-
128116 :param lines: The docstring lines
129- :type lines: list [ str ]
130117 """
131118 if model ._meta .abstract and not app .config .django_show_db_tables_abstract :
132119 return
@@ -136,18 +123,15 @@ def add_db_table_name(app, model, lines):
136123 lines .insert (0 , f"**Database table:** ``{ table_name } ``" )
137124
138125
139- def add_model_parameters (fields , lines , field_docs ):
126+ def add_model_parameters (
127+ fields : list [models .Field ], lines : list [str ], field_docs : dict
128+ ) -> None :
140129 """
141130 Add the given fields as model parameter with the ``:param:`` directive
142131
143132 :param fields: The list of fields
144- :type fields: list [ ~django.db.models.Field ]
145-
146133 :param lines: The list of current docstring lines
147- :type lines: list [ str ]
148-
149134 :param field_docs: The attribute docstrings of the model
150- :type field_docs: dict
151135 """
152136 for field in fields :
153137 # Add docstrings if they are found
@@ -165,16 +149,13 @@ def add_model_parameters(fields, lines, field_docs):
165149 lines .append (f":type { field .name } : { get_field_type (field , include_role = False )} " )
166150
167151
168- def improve_form_docstring (form , lines ) :
152+ def improve_form_docstring (form : forms . Form , lines : list [ str ]) -> None :
169153 """
170154 Improve the documentation of a Django :class:`~django.forms.Form` class.
171155 This highlights the available fields in the form.
172156
173157 :param form: The form object
174- :type form: ~django.forms.Form
175-
176158 :param lines: The list of existing docstring lines
177- :type lines: list [ str ]
178159 """
179160 lines .append ("**Form fields:**" )
180161 lines .append ("" )
0 commit comments