Django typing helper
Project description
django-autotyping
Automatically add type hints for Django powered applications.
[!WARNING]
Still WIP
[!NOTE]
As of today, generated type hints will only play well withdjango-types
.django-stubs
requires a type for both the__set__
and__get__
types.
Installation
Through pip
:
pip install django-autotyping
Usage
django-autotyping
can be used as a CLI program:
usage: Add type hints to your models for better auto-completion.
positional arguments:
path Path to the directory containing the Django application. This directory should contain your `manage.py` file.
options:
-h, --help show this help message and exit
--settings-module SETTINGS_MODULE
Value of the `DJANGO_SETTINGS_MODULE` environment variable (a dotted Python path).
--disable [{DJA001} ...]
Rules to be disabled.
--type-checking-block
Whether newly added imports should be in an `if TYPE_CHECKING` block (avoids circular imports).
Rules
Add type hints to forward relations (DJA001
)
All subclasses of RelatedField
will be taken into account.
from typing import TYPE_CHECKING
from django.db import models
# Model is imported in an `if TYPE_CHECKING` block if `--type-checking-block` is used.
if TYPE_CHECKING:
# Related model is imported from the corresponding apps models module:
from myproject.reporters.models import Reporter
class Article(models.Model):
# If the field supports `__class_getitem__` at runtime, it is parametrized directly:
reporter = models.ForeignKey["Reporter"](
"reporters.Reporter",
on_delete=models.CASCADE,
)
# Otherwise, an explicit annotation is used. No unnecessary import if model is in the same file.
article_version: "models.OneToOneField[ArticleVersion]" = models.OneToOneField(
"ArticleVersion",
on_delete=models.CASCADE,
)
django-autotyping
is built with LibCST.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
django-autotyping-0.0.1.tar.gz
(12.5 kB
view hashes)
Built Distribution
Close
Hashes for django_autotyping-0.0.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9625b11cb9664f1811ce502206e38d812e56d9f7153fe3fb4035fec934a00291 |
|
MD5 | 75bca6b8eadb2332940b7e8db54c69b0 |
|
BLAKE2b-256 | 9909fbc1674d38944e0b0725aafae89804435d993b5dd5f264bd6052989f63fc |