Skip to content
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

Add missing type annotations and require them in new code #61

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# This file only contains a selection of the most common options. For a full
# list see the documentation:
# https://www.sphinx-doc.org/en/master/usage/configuration.html
from __future__ import annotations

from sphinxcontrib_django import __version__

Expand Down
8 changes: 8 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,16 @@
# "D",
"RET",
"SIM",
"ANN",
]
ignore = [
"D1", # Missing docstrings
"ANN101", # Annotation for 'self'
]
line-length = 99

[tool.ruff.isort]
required-imports = ["from __future__ import annotations"]

[tool.ruff.per-file-ignores]
"tests/**/*.py" = ["ANN"] # TODO: tests are missing type annotations
2 changes: 2 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#!/usr/bin/env python3
from __future__ import annotations

from setuptools import setup

setup()
19 changes: 5 additions & 14 deletions sphinxcontrib_django/docstrings/attributes.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from django.db.models.manager import ManagerDescriptor
from django.db.models.query_utils import DeferredAttribute
from django.utils.module_loading import import_string
from sphinx.application import Sphinx
from sphinx.util.docstrings import prepare_docstring

from .field_utils import get_field_type, get_field_verbose_name
Expand All @@ -24,23 +25,18 @@
PhoneNumberDescriptor = None


def improve_attribute_docstring(app, attribute, name, lines):
def improve_attribute_docstring(
app: Sphinx, attribute: object, name: str, lines: list[str]
) -> None:
"""
Improve the documentation of various model fields.

This improves the navigation between related objects.

:param app: The Sphinx application object
:type app: ~sphinx.application.Sphinx

:param attribute: The instance of the object to document
:type attribute: object

:param name: The full dotted path to the object
:type name: str

:param lines: The docstring lines
:type lines: list [ str ]
"""
# Save initial docstring lines to append them to the modified lines
docstring_lines = lines.copy()
Expand Down Expand Up @@ -96,19 +92,14 @@ def improve_attribute_docstring(app, attribute, name, lines):
lines.extend(docstring_lines[:-1])


def get_field_details(app, field):
def get_field_details(app: Sphinx, field: models.Field) -> list[str]:
"""
This function returns the detail docstring of a model field.
It includes the field type and the verbose name of the field.

:param app: The Sphinx application object
:type app: ~sphinx.application.Sphinx

:param field: The field
:type field: ~django.db.models.Field

:return: The field details as list of strings
:rtype: list [ str ]
"""
choices_limit = app.config.django_choices_to_show

Expand Down
2 changes: 2 additions & 0 deletions sphinxcontrib_django/docstrings/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
(see :event:`autodoc-skip-member`)
"""
#: Ensure that the __init__ method gets documented (also see :confval:`autoclass_content` setting)
from __future__ import annotations

INCLUDE_MEMBERS = {"__init__"}

#: Members to hide.
Expand Down
7 changes: 3 additions & 4 deletions sphinxcontrib_django/docstrings/data.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
from __future__ import annotations

import io
import sys

from pprintpp import pprint as pp


def improve_data_docstring(data, lines):
def improve_data_docstring(data: object, lines: list[str]) -> None:
"""
Improve the documentation of data by pretty-printing into in the docstring.

:param data: The documented object
:type data: object

:param lines: The lines of docstring lines
:type lines: list [ str ]
"""
if isinstance(data, (list, tuple, dict, set)):
# Redirect stdout to StringIO to catch print
Expand Down
7 changes: 3 additions & 4 deletions sphinxcontrib_django/docstrings/methods.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
"""
This module contains all functions which are used to improve the documentation of methods.
"""
from __future__ import annotations

import re

RE_GET_FOO_DISPLAY = re.compile(r"\.get_(?P<field>[a-zA-Z0-9_]+)_display$")
RE_GET_NEXT_BY = re.compile(r"\.get_next_by_(?P<field>[a-zA-Z0-9_]+)$")
RE_GET_PREVIOUS_BY = re.compile(r"\.get_previous_by_(?P<field>[a-zA-Z0-9_]+)$")


def improve_method_docstring(name, lines):
def improve_method_docstring(name: str, lines: list[str]) -> None:
"""
Improve the documentation of methods automatically contributed to models by Django:

Expand All @@ -17,10 +19,7 @@ def improve_method_docstring(name, lines):
* :meth:`~django.db.models.Model.get_previous_by_FOO`

:param name: The full dotted path to the object.
:type name: str

:param lines: The lines of docstring lines
:type lines: list [ str ]
"""
if not lines:
# Not doing obj.__module__ lookups to avoid performance issues.
Expand Down
4 changes: 3 additions & 1 deletion sphinxcontrib_django/docstrings/patches.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
"""
This module contains patches for Django to improve its interaction with Sphinx.
"""
from __future__ import annotations

import contextlib

from django import apps, forms, test
Expand All @@ -24,7 +26,7 @@
MPTT = False


def patch_django_for_autodoc():
def patch_django_for_autodoc() -> None:
"""
Fix the appearance of some classes in autodoc.
E.g. the absolute path to the base model class is ``django.db.models.base.Model``, but its
Expand Down
2 changes: 2 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
https://github.com/sphinx-doc/sphinx/issues/7008), so this setup was created using the given code
snippets and the existing test cases for the autodoc extension.
"""
from __future__ import annotations

from unittest.mock import Mock

import pytest
Expand Down
2 changes: 2 additions & 0 deletions tests/roots/test-docstrings/conf.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import os
import sys

Expand Down
3 changes: 3 additions & 0 deletions tests/roots/test-docstrings/conflicting_sphinx_extension.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
from __future__ import annotations


def setup(app):
"""
Sphinx extension which also registers a "setting" directive
Expand Down
1 change: 1 addition & 0 deletions tests/roots/test-docstrings/dummy_django_app/settings.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
Dummy Django settings file
"""
from __future__ import annotations

SECRET_KEY = "dummy-key"

Expand Down
2 changes: 2 additions & 0 deletions tests/test_attribute_docstrings.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import pytest

try:
Expand Down
2 changes: 2 additions & 0 deletions tests/test_autodoc_skip.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import pytest


Expand Down
2 changes: 2 additions & 0 deletions tests/test_class_docstrings.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import pytest


Expand Down
2 changes: 2 additions & 0 deletions tests/test_data_docstrings.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import pytest


Expand Down
2 changes: 2 additions & 0 deletions tests/test_django_configured.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import pytest


Expand Down
2 changes: 2 additions & 0 deletions tests/test_django_setup.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import pytest
from sphinx.errors import ConfigError

Expand Down
2 changes: 2 additions & 0 deletions tests/test_method_docstrings.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import pytest


Expand Down
2 changes: 2 additions & 0 deletions tests/test_roles.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import pytest


Expand Down