Django 4.1 alpha 1 released

66 views
Skip to first unread message

Carlton Gibson

unread,
May 18, 2022, 2:09:08 AMMay 18
to django-...@googlegroups.com, django...@googlegroups.com, Django developers (Contributions to Django itself)
Details are available on the Django project weblog:



אורי

unread,
May 18, 2022, 11:44:32 PMMay 18
to Django developers (Contributions to Django itself)
Hi,

Django 4.0 had this code:

        errors = {}
        if exclude is None:
            exclude = []
        else:
            exclude = list(exclude)

        try:
            self.clean_fields(exclude=exclude)
        except ValidationError as e:
            errors = e.update_error_dict(errors)

In Django 4.1 alpha it looks like this:

        errors = {}
        if exclude is None:
            exclude = set()
        else:
            exclude = set(exclude)

        try:
            self.clean_fields(exclude=exclude)
        except ValidationError as e:
            errors = e.update_error_dict(errors)

My model tests fail:
======================================================================
ERROR: test_username_too_long_exception_4 (speedy.core.accounts.tests.test_models.ReservedUsernameHebrewTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "D:\Uri\Speedy_Net\Git\speedy-net-public\speedy\core\accounts\tests\test_models.py", line 395, in test_username_too_long_exception_4
    reserved_username.save()
  File "D:\Uri\Speedy_Net\Git\speedy-net-public\speedy\core\base\models.py", line 21, in save
    return super().save(*args, **kwargs)
  File "D:\Uri\Speedy_Net\Git\speedy-net-public\speedy\core\base\models.py", line 12, in save
    self.full_clean()
  File "D:\Uri\Speedy_Net\Git\speedy-net-public\.venv_3.9\lib\site-packages\django\db\models\base.py", line 1464, in full_clean
    self.clean_fields(exclude=exclude)
  File "D:\Uri\Speedy_Net\Git\speedy-net-public\speedy\core\accounts\models.py", line 182, in clean_fields
    exclude += ['username', 'slug']
TypeError: unsupported operand type(s) for +=: 'set' and 'list'

----------------------------------------------------------------------

Is `exclude` a set now (instead of a list) and where is it documented? If it's not documented, please document it. I didn't find it documented on https://docs.djangoproject.com/en/dev/releases/4.1/.

What is the best written code to change the line `exclude += ['username', 'slug']` in my code? Is it `exclude |= set(['username', 'slug'])` or `exclude |= {'username', 'slug'}`? Or should I convert to list and then back to set?

What is the reason `exclude` was changed to a set?

Thanks,
Uri.

אורי


On Wed, May 18, 2022 at 9:09 AM Carlton Gibson <carlton...@gmail.com> wrote:
Details are available on the Django project weblog:



--
You received this message because you are subscribed to the Google Groups "Django developers (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-develop...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/CAJwKpyQmCB6q__p167GcmaH8dBT%3D-jSbtr2LLb_E63E9QRQUXQ%40mail.gmail.com.

אורי

unread,
May 18, 2022, 11:50:04 PMMay 18
to Django developers (Contributions to Django itself)
Hi,

In my code there is clean_fields with exclude as an optional argument:

    def clean_fields(self, exclude=None):
        """
        Allows to have different slug and username validators for Entity and User.
        """
        if (exclude is None):
            exclude = []

Is it still optional and should I define it as a set if it's None? Or is it not optional any more?

I see in Django 4.1 alpha this is the code now:
    def clean_fields(self, exclude=None):
        """
        Clean all fields and raise a ValidationError containing a dict
        of all validation errors if any occur.

        """
        if exclude is None:
            exclude = set()


On Wed, May 18, 2022 at 9:09 AM Carlton Gibson <carlton...@gmail.com> wrote:
Details are available on the Django project weblog:



Carlton Gibson

unread,
May 19, 2022, 3:21:03 AMMay 19
to Django developers (Contributions to Django itself)
Hi Uri, 

Good spot. The docs have `The optional exclude argument lets you provide a list of field names to exclude from validation.` for `clean_fields()` so expecting a list seems reasonable. 🤔

This was changed in https://github.com/django/django/commit/1ea7e3157d1f9b4db71e768d75ea57e47dbd49f9 — it looks like your use-case isn't covered by the existing tests. 
Can I ask you to open a ticket on Trac, with a minimal test and we can have a look? 

Thanks! 👍

Kind Regards,

Carlton

אורי

unread,
May 19, 2022, 5:07:29 AMMay 19
to Django developers (Contributions to Django itself)
Hi Carlton,

Thanks for your reply. I created a ticket:

I'm not sure how to create a test. Maybe you should just contain the following code (in a model):

def clean_fields(self, exclude=None):
        if (exclude is None):
            exclude = []

        exclude += ['username', 'slug']

        return super().clean_fields(exclude=exclude)

Or if you want to check for a set then change it to sets.

Thanks,
Uri.



Reply all
Reply to author
Forward
0 new messages