python.django.maintainability.duplicate-path-assignment.duplicate-name-assignment

Author
851
Download Count*
License
The name $NAME
is used for both $URL
and $OTHER_URL
, which can lead to unexpected behavior when using URL reversing. Pick a unique name for each path.
Run Locally
Run in CI
Defintion
rules:
- id: duplicate-name-assignment
languages:
- python
message: The name `$NAME` is used for both `$URL` and `$OTHER_URL`, which can
lead to unexpected behavior when using URL reversing. Pick a unique name
for each path.
metadata:
category: maintainability
license: Commons Clause License Condition v1.0[LGPL-2.1-only]
technology:
- django
references:
- https://docs.djangoproject.com/en/3.2/topics/http/urls/#naming-url-patterns
patterns:
- pattern: >
[..., django.urls.path('$URL', $VIEW, name='$NAME', ...), ...,
django.urls.path('$OTHER_URL', $OTHER_VIEW, name='$NAME', ...), ...]
- pattern-not: >
[..., django.urls.path('$URL', $VIEW, name='$NAME', ...), ...,
django.urls.path('$URL', $VIEW, name='$NAME', ...), ...]
- pattern-not: >
[..., django.urls.path('$URL', $VIEW, name='$NAME', ...), ...,
django.urls.path('$URL', $OTHER_VIEW, name='$NAME', ...), ...]
- pattern-not: >
[..., django.urls.path('$URL', $VIEW, name='$NAME', ...), ...,
django.urls.path('$OTHER_URL', $VIEW, name='$NAME', ...), ...]
severity: ERROR
Examples
duplicate-path-assignment.py
from django.urls import path
# ruleid: duplicate-path-assignment
urlpatterns = [
path('path/to/view', views.example_view, name="test"),
path('path/to/view', views.example_view, name="test"),
]
# ruleid: duplicate-path-assignment
urlpatterns = [
path('path/to/view', views.example_view, name="test"),
path('path/to/other_view', view.other_view, name="hello"),
path('path/to/view', views.example_view, name="test"),
]
# ruleid: duplicate-path-assignment
urlpatterns = [
path('path/to/view', views.example_view),
path('path/to/view', views.example_view),
]
# ruleid: duplicate-path-assignment
urlpatterns = [
path('path/to/view', views.example_view, {'abc': 'def'}),
path('path/to/view', views.example_view, {'abc': 'def'}),
]
# ruleid: duplicate-path-assignment
urlpatterns = [
path('path/to/view', views.example_view, {'abc': 'def'}),
path('path/to/view', views.example_view, {'def': 'abc'}),
]
# ruleid: duplicate-path-assignment
urlpatterns = [
path('path/to/view', views.example_view, {'abc': 'def'}, name="test123"),
path('path/to/view', views.example_view, {'abc': 'def'}, name="test123"),
]
# ruleid: duplicate-path-assignment
urlpatterns = [
path('path/to/view', views.example_view, {'abc': 'def'}, name="test123"),
path('path/to/view', views.example_view, {'def': 'abc'}),
]
# ruleid: duplicate-path-assignment
urlpatterns = [
path('path/to/view', views.example_view, {'abc': 'def'}, name="test123"),
path('path/to/view', views.example_view),
]
# ruleid: conflicting-path-assignment
urlpatterns = [
path('path/to/view', views.example_view, name="test"),
path('path/to/view', views.other_view, name="test"),
]
# ruleid: conflicting-path-assignment
urlpatterns = [
path('path/to/view', views.example_view, name="test"),
path('path/to/other_view', view.other_view, name="hello"),
path('path/to/view', views.other_view, name="test"),
]
# ruleid: conflicting-path-assignment
urlpatterns = [
path('path/to/view', views.example_view),
path('path/to/view', views.other_view),
]
# ruleid: conflicting-path-assignment
urlpatterns = [
path('path/to/view', views.example_view, {'abc': 'def'}),
path('path/to/view', views.other_view, {'abc': 'def'}),
]
# ruleid: conflicting-path-assignment
urlpatterns = [
path('path/to/view', views.example_view, {'abc': 'def'}),
path('path/to/view', views.other_view, {'def': 'abc'}),
]
# I would prefer duplicate-path-assignment to not match the following test cases
# to avoid giving two messages for the same issue, but could not find a way yet.
# todook: duplicate-path-assignment
# ruleid: duplicate-path-assignment-different-names, duplicate-path-assignment
urlpatterns = [
path('path/to/view', views.example_view, name="test"),
path('path/to/view', views.example_view, name="other_name"),
]
# todook: duplicate-path-assignment
# ruleid: duplicate-path-assignment-different-names, duplicate-path-assignment
urlpatterns = [
path('path/to/view', views.example_view, {'abc': 'def'}, name="test"),
path('path/to/view', views.example_view, {'abc': 'def'}, name="other_name"),
]
# todook: duplicate-path-assignment
# ruleid: duplicate-path-assignment-different-names, duplicate-path-assignment
urlpatterns = [
path('path/to/view', views.example_view, {'abc': 'def'}, name="test"),
path('path/to/other/view', views.other_view, {'abc': 'def'}, name="some_test"),
path('path/to/view', views.example_view, {'abc': 'def'}, name="other_name"),
]
# todook: duplicate-path-assignment
# ruleid: duplicate-path-assignment-different-names, duplicate-path-assignment
urlpatterns = [
path('path/to/view', views.example_view, {'abc': 'def'}, name="test123"),
path('path/to/view', views.example_view, {'def': 'abc'}, name="test456"),
]
# ruleid: duplicate-name-assignment
urlpatterns = [
path('path/to/view', views.example_view, name="test123"),
path('path/to/other/view', views.other_view, name="test123"),
]
# ruleid: conflicting-path-assignment
urlpatterns = [
path('path/to/view', views.example_view, name="test123"),
path('path/to/view', views.other_view, name="test123"),
]
# ruleid: duplicate-name-assignment
urlpatterns = [
path('path/to/view', views.example_view, name="test123"),
path('path/to/other/view', views.other_view, name="test123"),
]
# ok: duplicate-path-assignment-different-names, conflicting-path-assignment, duplicate-path-assignment
urlpatterns = [
path('path/to/other_view', views.example_view, name="test"),
path('path/to/view', views.example_view, name="test"),
]
# ok: duplicate-path-assignment-different-names, conflicting-path-assignment, duplicate-path-assignment
urlpatterns = [
path('path/to/other_view', views.example_view, name="test"),
path('path/to/view', views.other_view, name="test_abc"),
]
Short Link: https://sg.run/pk2Z