Skip to content

Commit

Permalink
feat: urls: allow path() instead of re_path() if a path contains dashes
Browse files Browse the repository at this point in the history
* also delete now unused re.py
  • Loading branch information
akx authored and browniebroke committed Oct 13, 2020
1 parent 4290882 commit de744cf
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 21 deletions.
18 changes: 0 additions & 18 deletions django_codemod/re.py

This file was deleted.

8 changes: 5 additions & 3 deletions django_codemod/visitors/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
from libcst import matchers as m
from libcst.codemod.visitors import AddImportsVisitor

from django_codemod import re
from django_codemod.constants import DJANGO_3_0, DJANGO_4_0
from django_codemod.visitors.base import BaseFuncRenameTransformer

Expand All @@ -11,6 +10,9 @@ class PatternNotSupported(RuntimeError):
pass


# A set of regex special characters sans the dash character
REGEX_SPECIALS_SANS_DASH = set("()[]{}?*+|^$\\.&~# \t\n\r\v\f")

REGEX_TO_CONVERTER = {
"[0-9]+": "int",
r"\d+": "int",
Expand Down Expand Up @@ -98,5 +100,5 @@ def parse_next_group(self, left_to_parse):

def check_route(self, route):
"""Check that route doesn't contain anymore regex."""
if route != re.escape(route):
raise PatternNotSupported("Route contains regex")
if set(route) & REGEX_SPECIALS_SANS_DASH:
raise PatternNotSupported(f"Route {route} contains regex")
2 changes: 2 additions & 0 deletions tests/visitors/test_urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ def test_all_path(self) -> None:
urlpatterns = [
url(r'^$', views.index, name='index'),
url(r'^about/$', views.about, name='about'),
url(r'^more-info/$', views.about, name='more-info'),
]
"""
after = """
Expand All @@ -87,6 +88,7 @@ def test_all_path(self) -> None:
urlpatterns = [
path('', views.index, name='index'),
path('about/', views.about, name='about'),
path('more-info/', views.about, name='more-info'),
]
"""
self.assertCodemod(before, after)
Expand Down

0 comments on commit de744cf

Please sign in to comment.