Skip to main content

a python package for type checking the urls and associated views.

Project description

Django UrlConf Checks

pypi python Build Status codecov License

django-urlconfchecks is a static type checker that checks your URLconf parameter types with argument types specified in associated views. It leverages the Django's static check system.

Installation

pip install django-urlconfchecks

Python 3.8 or later is required. However, before Python 3.10 some checks relating to Optional types in view signatures are skipped due to stdlib limitations.

Usage

You can use this package in different ways:

As a Django app

Add django_urlconfchecks to your INSTALLED_APPS list in your settings.py file.

    INSTALLED_APPS = [
    ...
    'django_urlconfchecks',
]

As a command line tool

Run this command from the root of your project, were manage.py is located:

$ urlconfchecks --help

    Usage: urlconfchecks [OPTIONS]

      Check all URLConfs for errors.

    Options:
      --version
      -u, --urlconf PATH    Specify the URLconf to check.
      --install-completion  Install completion for the current shell.
      --show-completion     Show completion for the current shell, to copy it or
                            customize the installation.
      --help                Show this message and exit.

As a pre-commit hook

Add the following to your .pre-commit-config.yaml file:

  - repo: https://github.com/AliSayyah/django-urlconfchecks
    rev: v0.11.0
    hooks:
      - id: django-urlconfchecks

For more information, see the usage documentation.

Features

Using this package, URL pattern types will automatically be matched with associated views, and in case of mismatch, an error will be raised.

Example:

# urls.py
from django.urls import path

from . import views

urlpatterns = [
    path('articles/<str:year>/', views.year_archive),
    path('articles/<int:year>/<int:month>/', views.month_archive),
    path('articles/<int:year>/<int:month>/<slug:slug>/', views.article_detail),
]
# views.py

def year_archive(request, year: int):
    pass


def month_archive(request, year: int, month: int):
    pass


def article_detail(request, year: int, month: int, slug: str):
    pass

output will be:

(urlchecker.E002) For parameter `year`, annotated type int does not match expected `str` from urlconf
  • TODO:
    • Handle type checking parameterized generics e.g. typing.List[int], list[str] etc.
    • Should only warn for each unhandled Converter once.
    • Regex patterns perhaps? (only RoutePattern supported at the moment).

Credits

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_urlconfchecks-0.11.0.tar.gz (27.5 kB view details)

Uploaded Source

Built Distribution

django_urlconfchecks-0.11.0-py3-none-any.whl (21.9 kB view details)

Uploaded Python 3

File details

Details for the file django_urlconfchecks-0.11.0.tar.gz.

File metadata

  • Download URL: django_urlconfchecks-0.11.0.tar.gz
  • Upload date:
  • Size: 27.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.8

File hashes

Hashes for django_urlconfchecks-0.11.0.tar.gz
Algorithm Hash digest
SHA256 41e9313345740b4cf7020bd2b3080e7a458493444505e49dba819661ecabf541
MD5 e2926cb8ade7b037a34171b4d132309f
BLAKE2b-256 c995b4390b2b000b8bd2b2080f7a16aa487b0c0b59cba289cada30af431e426b

See more details on using hashes here.

File details

Details for the file django_urlconfchecks-0.11.0-py3-none-any.whl.

File metadata

File hashes

Hashes for django_urlconfchecks-0.11.0-py3-none-any.whl
Algorithm Hash digest
SHA256 98bd7588bf76ed97e1f019a6d251605e39e75a3bd91ee885b5ec018b7b30a199
MD5 8e7198abdec5d04d59b8fb28a938d4f0
BLAKE2b-256 e64cc6765c0256ae3df6f337cce27bae15668d96417314a2f55dfa972578ed7c

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page