#31070: Add a check for URLconfs that mix named and unnamed capture groups
-------------------------------------+-------------------------------------
     Reporter:  Baptiste Mispelon    |                    Owner:  Baptiste
                                     |  Mispelon
         Type:  New feature          |                   Status:  assigned
    Component:  Core (System         |                  Version:  master
  checks)                            |
     Severity:  Normal               |               Resolution:
     Keywords:                       |             Triage Stage:  Accepted
    Has patch:  1                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  1
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------

Comment (by Baptiste Mispelon):

 Replying to [comment:5 Carlton Gibson]:
 >
 > From #31061, I had it in my mind that there was an actual error in play.
 > (But if an optional capture group—`(?P<...>...)?` fails to match, I
 guess the positional args might still come into play...)
 I don't believe that's the case actually.
 If there are named capture groups Django will always discard any unnamed
 groups, even if the named ones didn't match anything (this behavior had
 been broken and was restored as described in #31061).

 Confusing, right?
 That's why I thought having a warning to suggest you don't mix named and
 unnamed capture groups would be worthwhile.

 The scenario I had in mind is this one:
 1) Developer adds a new mixed `re_path` to their URLconf, thinking that
 Django will pass all groups (named and unnamed) to the view
 2) The new warning kicks in, alerting them about the potential issue
 3) If it was a bug, they fix it by changing the unnamed group to a named
 one. If it was by design, they change the unnamed group to a non-capturing
 one.


 For someone who's been using a mixed pattern for a while then it would
 start giving them a warning but the fix is easy enough and I would argue
 it makes the regexp more correct (explicit vs implicit and all that).
 With the recent addition of simplified patterns (`path` vs `re_path`) and
 the moving of the `urls` package (`django.core.urls` -> `django.urls`), I
 feel that the `urls.py` in people's projects have probably seen some
 changes with recent versions.


 But all-in-all, I don't feel too strongly about this. I came to this via
 #31061 which made me dive into URL dispatcher internals where I discovered
 this quirk. I thought this check could help prevent issues but if you
 think it's not worth it and want to mark this as `wontfix`, I wouldn't
 oppose it.

-- 
Ticket URL: <https://code.djangoproject.com/ticket/31070#comment:6>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/067.27947a6c762e66806e541ebce5c4e4e9%40djangoproject.com.

Reply via email to