Skip to content

Commit 998c9dd

Browse files
lambytimgraham
authored andcommitted
Fixed #28663 -- Add a check for likely incorrectly migrated django.urls.path() routes.
1 parent a4f9ef4 commit 998c9dd

File tree

7 files changed

+58
-2
lines changed

7 files changed

+58
-2
lines changed

django/urls/resolvers.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,16 @@ def match(self, path):
255255
return None
256256

257257
def check(self):
258-
return self._check_pattern_startswith_slash()
258+
warnings = self._check_pattern_startswith_slash()
259+
route = self._route
260+
if '(?P<' in route or route.startswith('^') or route.endswith('$'):
261+
warnings.append(Warning(
262+
"Your URL pattern {} has a route that contains '(?P<', begins "
263+
"with a '^', or ends with a '$'. This was likely an oversight "
264+
"when migrating to django.urls.path().".format(self.describe()),
265+
id='2_0.W001',
266+
))
267+
return warnings
259268

260269
def _compile(self, route):
261270
return re.compile(_route_to_regex(route, self._is_endpoint)[0])

docs/ref/checks.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,9 @@ Backwards compatibility
9898
Compatibility checks warn of potential problems that might occur after
9999
upgrading Django.
100100

101-
Currently, there aren't any of these checks.
101+
* **2_0.W001**: Your URL pattern ``<pattern>`` has a ``route`` that contains
102+
``(?P<``, begins with a ``^``, or ends with a ``$``. This was likely an
103+
oversight when migrating from ``url()`` to :func:`~django.urls.path`.
102104

103105
Caches
104106
------

tests/check_framework/test_urls.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,36 @@ def test_check_unique_namespaces(self):
135135
self.assertEqual(result, [])
136136

137137

138+
class UpdatedToPathTests(SimpleTestCase):
139+
140+
@override_settings(ROOT_URLCONF='check_framework.urls.path_compatibility.contains_re_named_group')
141+
def test_contains_re_named_group(self):
142+
result = check_url_config(None)
143+
self.assertEqual(len(result), 1)
144+
warning = result[0]
145+
self.assertEqual(warning.id, '2_0.W001')
146+
expected_msg = "Your URL pattern '(?P<named-group>\\d+)' has a route"
147+
self.assertIn(expected_msg, warning.msg)
148+
149+
@override_settings(ROOT_URLCONF='check_framework.urls.path_compatibility.beginning_with_caret')
150+
def test_beginning_with_caret(self):
151+
result = check_url_config(None)
152+
self.assertEqual(len(result), 1)
153+
warning = result[0]
154+
self.assertEqual(warning.id, '2_0.W001')
155+
expected_msg = "Your URL pattern '^beginning-with-caret' has a route"
156+
self.assertIn(expected_msg, warning.msg)
157+
158+
@override_settings(ROOT_URLCONF='check_framework.urls.path_compatibility.ending_with_dollar')
159+
def test_ending_with_dollar(self):
160+
result = check_url_config(None)
161+
self.assertEqual(len(result), 1)
162+
warning = result[0]
163+
self.assertEqual(warning.id, '2_0.W001')
164+
expected_msg = "Your URL pattern 'ending-with-dollar$' has a route"
165+
self.assertIn(expected_msg, warning.msg)
166+
167+
138168
class CheckURLSettingsTests(SimpleTestCase):
139169

140170
@override_settings(STATIC_URL='a/', MEDIA_URL='b/')

tests/check_framework/urls/path_compatibility/__init__.py

Whitespace-only changes.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from django.urls import path
2+
3+
urlpatterns = [
4+
path('^beginning-with-caret', lambda x: x),
5+
]
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from django.urls import path
2+
3+
urlpatterns = [
4+
path('(?P<named-group>\d+)', lambda x: x),
5+
]
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from django.urls import path
2+
3+
urlpatterns = [
4+
path('ending-with-dollar$', lambda x: x),
5+
]

0 commit comments

Comments
 (0)