Skip to content

Commit 9e16379

Browse files
authored
Merge pull request #47 from WhyNotHugo/typing
Add a few more type hints
2 parents 557bba5 + 000ac65 commit 9e16379

File tree

7 files changed

+36
-53
lines changed

7 files changed

+36
-53
lines changed

sphinxcontrib_django/__init__.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,24 @@
11
"""
22
This is a sphinx extension which improves the documentation of Django apps.
33
"""
4+
5+
from __future__ import annotations
6+
47
__version__ = "2.4"
58

9+
from sphinx.application import Sphinx
10+
611
from . import docstrings, roles
712

813

9-
def setup(app):
14+
def setup(app: Sphinx) -> dict:
1015
"""
1116
Allow this module to be used as sphinx extension.
1217
1318
Setup the two sub-extensions :mod:`~sphinxcontrib_django.docstrings` and
1419
:mod:`~sphinxcontrib_django.roles` which can also be imported separately.
1520
1621
:param app: The Sphinx application object
17-
:type app: ~sphinx.application.Sphinx
1822
"""
1923
docstrings.setup(app)
2024
roles.setup(app)

sphinxcontrib_django/docstrings/attributes.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
"""
22
This module contains all functions which are used to improve the documentation of attributes.
33
"""
4+
from __future__ import annotations
5+
46
from django.db import models
57
from django.db.models.fields import related_descriptors
68
from django.db.models.fields.files import FileDescriptor

sphinxcontrib_django/docstrings/classes.py

Lines changed: 9 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,39 @@
11
"""
22
This module contains all functions which are used to improve the documentation of classes.
33
"""
4+
from __future__ import annotations
45

56
from django import forms
67
from django.db import models
8+
from sphinx.application import Sphinx
79
from sphinx.pycode import ModuleAnalyzer
810

911
from .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("")

sphinxcontrib_django/docstrings/field_utils.py

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,21 @@
33
:mod:`~sphinxcontrib_django.docstrings.attributes` and
44
:mod:`~sphinxcontrib_django.docstrings.classes` modules.
55
"""
6+
from __future__ import annotations
7+
68
from django.apps import apps
79
from django.contrib import contenttypes
810
from django.db import models
911
from django.utils.encoding import force_str
1012

1113

12-
def get_field_type(field, include_role=True):
14+
def get_field_type(field: models.Field, include_role: bool = True) -> str:
1315
"""
1416
Get the type of a field including the correct intersphinx mappings.
1517
1618
:param field: The field
17-
:type field: ~django.db.models.Field
18-
1919
:param include_directive: Whether or not the role :any:`py:class` should be included
20-
:type include_directive: bool
21-
2220
:return: The type of the field
23-
:rtype: str
2421
"""
2522
if isinstance(field, models.fields.related.RelatedField):
2623
to = field.remote_field.model
@@ -46,7 +43,7 @@ def get_field_type(field, include_role=True):
4643
return f"~{type(field).__module__}.{type(field).__name__}"
4744

4845

49-
def get_field_verbose_name(field):
46+
def get_field_verbose_name(field: models.Field) -> str:
5047
"""
5148
Get the verbose name of the field.
5249
If the field has a ``help_text``, it is also included.
@@ -55,7 +52,6 @@ def get_field_verbose_name(field):
5552
For reverse related fields, the originating field is linked.
5653
5754
:param field: The field
58-
:type field: ~django.db.models.Field
5955
"""
6056
help_text = ""
6157
# Check whether the field is a reverse related field
@@ -137,18 +133,13 @@ def get_field_verbose_name(field):
137133
return verbose_name
138134

139135

140-
def get_model_from_string(field, model_string):
136+
def get_model_from_string(field: models.Field, model_string: str) -> type[models.Model]:
141137
"""
142138
Get a model class from a string
143139
144140
:param field: The field
145-
:type field: ~django.db.models.Field
146-
147141
:param model_string: The string label of the model
148-
:type model_string: str
149-
150142
:return: The class of the model
151-
:rtype: type
152143
"""
153144
if "." in model_string:
154145
model = apps.get_model(model_string)

sphinxcontrib_django/roles.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,20 @@
2121
"sphinxcontrib_django.roles",
2222
]
2323
"""
24+
from __future__ import annotations
25+
2426
import logging
2527

28+
from sphinx.application import Sphinx
29+
from sphinx.config import Config
2630
from sphinx.errors import ExtensionError
2731

2832
from . import __version__
2933

3034
logger = logging.getLogger(__name__)
3135

3236

33-
def setup(app):
37+
def setup(app: Sphinx) -> dict:
3438
"""
3539
Allow this module to be used as Sphinx extension.
3640
@@ -39,7 +43,6 @@ def setup(app):
3943
It adds cross-reference types via :meth:`~sphinx.application.Sphinx.add_crossref_type`.
4044
4145
:param app: The Sphinx application object
42-
:type app: ~sphinx.application.Sphinx
4346
"""
4447
# Load sphinx.ext.intersphinx extension
4548
app.setup_extension("sphinx.ext.intersphinx")
@@ -65,18 +68,15 @@ def setup(app):
6568
}
6669

6770

68-
def add_default_intersphinx_mappings(app, config):
71+
def add_default_intersphinx_mappings(app: Sphinx, config: Config) -> None:
6972
"""
7073
This function provides a default intersphinx mapping to the documentations of Python, Django
7174
and Sphinx if ``intersphinx_mapping`` is not given in ``conf.py``.
7275
7376
Called on the :event:`config-inited` event.
7477
7578
:param app: The Sphinx application object
76-
:type app: ~sphinx.application.Sphinx
77-
7879
:param config: The Sphinx configuration
79-
:type config: ~sphinx.config.Config
8080
"""
8181
DEFAULT_INTERSPHINX_MAPPING = {
8282
"python": ("https://docs.python.org/", None),
@@ -87,4 +87,5 @@ def add_default_intersphinx_mappings(app, config):
8787
),
8888
}
8989
if not config.intersphinx_mapping:
90-
config.intersphinx_mapping = DEFAULT_INTERSPHINX_MAPPING
90+
# TYPING: type hints are missing `.intersphinx_mapping` attribute.
91+
config.intersphinx_mapping = DEFAULT_INTERSPHINX_MAPPING # type: ignore[attr-defined ]

tests/roots/test-docstrings/dummy_django_app/forms.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations
2+
13
from django import forms
24

35
from .models import SimpleModel
@@ -7,7 +9,7 @@ class SimpleForm(forms.ModelForm):
79
test1 = forms.CharField(label="Test1")
810
test2 = forms.CharField(help_text="Test2")
911

10-
def __init__(self, *args, **kwargs):
12+
def __init__(self, *args, **kwargs) -> None:
1113
"""
1214
This is a custom init method
1315
"""

tests/roots/test-docstrings/dummy_django_app/models.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations
2+
13
from django.contrib.contenttypes.fields import GenericForeignKey
24
from django.contrib.contenttypes.models import ContentType
35
from django.db import models
@@ -108,7 +110,7 @@ class TaggedItem(models.Model):
108110
object_id = models.PositiveIntegerField()
109111
content_object = GenericForeignKey("content_type", "object_id")
110112

111-
def __str__(self):
113+
def __str__(self) -> str:
112114
return self.tag
113115

114116

0 commit comments

Comments
 (0)