Skip to content

Explicit type hints

There are some cases where generating custom type stubs is not enough. In such cases, it might be required to add some explicit type annotations to your code.

Fortunately, django-autotyping can automatically add these type hints in some places.

python manage.py add_type_hints --project-dir src/ --diff --ignore DJA001

Still work in progress

This functionality is still work in progress.

Available rules

  • DJA001: Add type annotations to forward relations.

Configuration

This section describes the available configuration options for stubs generation. These values must be set as a dictionary under the CODE_GENERATION key:

AUTOTYPING = {
    "STUBS_GENERATION": {
        "PROJECT_DIR": Path(...),
    }
}

Configuration for adding type annotations to Django user code.

PROJECT_DIR: Path | None = None

The directory of the project, where code modifications should be applied.

DIFF: bool = False

Show changes to be applied instead of modifying existing files.

TYPE_CHECKING_BLOCK: bool = True

Whether newly added imports should be in an if TYPE_CHECKING block (avoids circular imports).

ASSUME_CLASS_GETITEM: bool = False

Whether generic classes in stubs files but not at runtime should be assumed to have a __class_getitem__ method. This can be achieved by using django-stubs-ext or manually.

Affected rules: DJA001.