Giter Club home page Giter Club logo

django-urlconfchecks's Introduction

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

django-urlconfchecks's People

Contributors

alisayyah avatar dependabot[bot] avatar dmytrolitvinov avatar pre-commit-ci[bot] avatar spookylukey avatar visrut7 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

spookylukey

django-urlconfchecks's Issues

mkdocs-material-extensions is included as a runtime dependency

When I install django-urlconfchecks, I found that mkdocs-material-extensions got installed as well:

(cciw) luke@knox> pip install django-urlconfchecks                                  
Collecting django-urlconfchecks                                                     
  Using cached django_urlconfchecks-0.3.0-py3-none-any.whl (17 kB)                  
Collecting mkdocs-material-extensions<2.0.0,>=1.0.1                                 
  Using cached mkdocs_material_extensions-1.0.3-py3-none-any.whl (8.1 kB)           
Installing collected packages: mkdocs-material-extensions, django-urlconfchecks     
Successfully installed django-urlconfchecks-0.3.0 mkdocs-material-extensions-1.0.3  

It doesn't seem to be a runtime dependency, so this seems to be a mistake.

How can this be used with VSCode?

I'd like to use this with VSCode automatically, showing inline linting like Mypy, for instance. Anyone have thoughts on how to achieve this?

Default silenced views (CBV) does not with [email protected]

Describe the bug
After adding fine-grained method for silencing errors in corresponded PR #126 there is a default line for CBV views which does not work for [email protected]

To Reproduce
Steps to reproduce the behavior:

  1. Setup [email protected] at your project.
  2. Prepare SimpleView class:
class SimpleView(View):
    """
    A simple view with a docstring.
    """

    def get(self, request):
        return HttpResponse("This is a simple view")
  1. Use that view as a CBV view at urls
  2. Run urlconfchecks

Expected behavior
0 error

Actual behavior
1 error

Why this is happening? Starting from Django@4, using __qualname__ will transform correctly according to a default one that is defined in that package. Here is unit test from Django@4 - https://github.com/django/django/blob/898f0aa44fe63c20d7f4125672b8eb800a578ce5/tests/generic_views/test_base.py#L189

For [email protected] there is no such unit test for __qualname__ In Django==3.2 there is no such test: https://github.com/django/django/blob/777362d74aa3f3d88e6d60199f1d986200fb83b8/tests/generic_views/test_base.py#L173 And it simply returns the name of the class - just SimpleView`

Versions:

  • Django==3.2
  • Python==3.8

Additional context
The first initial request of that bug was commented in next PR: #126 (comment)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.