Skip to main content

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.7 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.9.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

Metadata

Release files for django-urlconfchecks 0.9.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-urlconfchecks 0.9.0
File Size Uploaded
django_urlconfchecks-0.9.0.tar.gz 27.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for django-urlconfchecks 0.9.0
File Interpreter ABI Platform
django_urlconfchecks-0.9.0-py3-none-any.whl Python 3 none any Details

Total release size: 49.6 kB

Release files / django_urlconfchecks-0.9.0.tar.gz

Download URL django_urlconfchecks-0.9.0.tar.gz
Size 27.9 kB
Tags Source
SHA-256 checksum
How to use checksums
142f85dfe49bc8599397a77937dfebee3e3d70bec8b2d3c5afeb24d571bbc437
BLAKE2b-256 checksum
How to use checksums
679dc9235aa5c2ceae1756621249ec4c2f91c5f9cf7c2e9192ae542625330afb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.11.2

Release files / django_urlconfchecks-0.9.0-py3-none-any.whl

Download URL django_urlconfchecks-0.9.0-py3-none-any.whl
Size 21.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
d8da258715de546eaf292d70082a9e632aad9357717b2fad7668c83ff379748d
BLAKE2b-256 checksum
How to use checksums
5914a8962bebc3d77fdb2e9dfe0d21a4dabc4786c8a5fd08e0472869b0c06c94
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.11.2

Release history Release notifications | RSS feed

0.13.1

2 release files

0.13.0

2 release files

0.12.0

2 release files

0.11.0

2 release files

0.10.0

2 release files

This release

0.9.0 This release

2 release files

0.8.0

2 release files

0.7.3

2 release files

0.7.2

2 release files

0.7.1

2 release files

0.7.0

2 release files

0.6.0

2 release files

0.5.0

2 release files

0.4.0

2 release files

0.3.1

2 release files

0.3.0

2 release files

0.2.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page