#31069: Change in behavior for optional re matches. --------------------------------+------------------------------------ Reporter: Terence Honles | Owner: nobody Type: Bug | Status: new Component: Documentation | Version: 3.0 Severity: Normal | Resolution: Keywords: | Triage Stage: Accepted Has patch: 0 | Needs documentation: 0 Needs tests: 0 | Patch needs improvement: 0 Easy pickings: 0 | UI/UX: 0 --------------------------------+------------------------------------ Description changed by Terence Honles:
Old description: > I am mainly pointing this out as it probably should have been called out > as a breaking change from Django 2.X to 3.X (And I think the release > notes should probably be adjusted if the old functionality is not to be > restored). You can probably argue either way about if this is a bug since > it's coming from re's `match.groupdict()`. It's related to > https://code.djangoproject.com/ticket/31061 and caused by > https://code.djangoproject.com/ticket/26431 / > https://github.com/django/django/pull/11477 > > The test is the following: > > {{{ > from django.urls import re_path > > def view(request, optional1, optional2): ... > > urlpatterns = [ > re_path(r'^(?P<optional1>\w+/)(?:(?P<optional2>\w+)/)', view) > ] > }}} > > For Django 2.X this would work as expected because named captures are > returned even if they are `None`, however in Django 3.0 this would raise > an error because positional parameter `optional2` is no longer provided. > (i.e. `view() missing 1 required positional argument: 'optional2'`) > > You can argue that `optional2` should actually have had a default all > along, but it didn't before and code may unexpectedly break because it is > now required. > > I'm not very familiar with https://code.djangoproject.com/ticket/26431, > but I think if possible it probably should have been fixed at a different > level. New description: I am mainly pointing this out as it probably should have been called out as a breaking change from Django 2.X to 3.X (And I think the release notes should probably be adjusted if the old functionality is not to be restored). You can probably argue either way about if this is a bug since it's coming from re's `match.groupdict()`. It's related to https://code.djangoproject.com/ticket/31061 and caused by https://code.djangoproject.com/ticket/26431 / https://github.com/django/django/pull/11477 The test is the following: {{{ from django.urls import re_path def view(request, optional1, optional2): ... urlpatterns = [ re_path(r'^(?P<optional1>\w+/)(?:(?P<optional2>\w+)/)?', view) ] }}} For Django 2.X this would work as expected because named captures are returned even if they are `None`, however in Django 3.0 this would raise an error because positional parameter `optional2` is no longer provided. (i.e. `view() missing 1 required positional argument: 'optional2'`) You can argue that `optional2` should actually have had a default all along, but it didn't before and code may unexpectedly break because it is now required. I'm not very familiar with https://code.djangoproject.com/ticket/26431, but I think if possible it probably should have been fixed at a different level. -- -- Ticket URL: <https://code.djangoproject.com/ticket/31069#comment:5> 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 django-updates+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/django-updates/071.00c51748d7811714b6a0f6ac4aac0d1b%40djangoproject.com.