#34920: Reordered file extensions for improved validation
------------------------------+--------------------------------------
     Reporter:  ksg           |                    Owner:  ksg
         Type:  Bug           |                   Status:  assigned
    Component:  Core (Other)  |                  Version:  dev
     Severity:  Normal        |               Resolution:
     Keywords:                |             Triage Stage:  Unreviewed
    Has patch:  1             |      Needs documentation:  0
  Needs tests:  0             |  Patch needs improvement:  0
Easy pickings:  0             |                    UI/UX:  0
------------------------------+--------------------------------------

Old description:

> `django.core.validators.FileExtensionValidator` had an '__eq__' method to
> compare the validator class. However, comparing arrays is not accurate
> when the order of elements in the arrays is different.
>
> {{{
> def __eq__(self, other):
>     return (
>         isinstance(other, self.__class__)
>         and sorted(self.allowed_extensions) ==
> sorted(other.allowed_extensions)
>         and self.message == other.message
>         and self.code == other.code
>     )
> }}}
>
> This test case failed:
>
> {{{
> self.assertEqual(
>     FileExtensionValidator(["jpg", "png", "txt"]),
>     FileExtensionValidator(["txt", "jpg", "png"]),
> )
> }}}
>
> So I suggest comparing two extension arrays after sorting them.

New description:

 `django.core.validators.FileExtensionValidator` had an `__eq__` method to
 compare the validator class. However, comparing arrays is not accurate
 when the order of elements in the arrays is different.

 {{{
 def __eq__(self, other):
     return (
         isinstance(other, self.__class__)
         and sorted(self.allowed_extensions) ==
 sorted(other.allowed_extensions)
         and self.message == other.message
         and self.code == other.code
     )
 }}}

 This test case failed:

 {{{
 self.assertEqual(
     FileExtensionValidator(["jpg", "png", "txt"]),
     FileExtensionValidator(["txt", "jpg", "png"]),
 )
 }}}

 So I suggest comparing two extension arrays after sorting them.

--

Comment (by Tim Graham):

 I'd think that validators that behave identically should be considered
 equal. Did you run into a real-world bug with the current behavior?

-- 
Ticket URL: <https://code.djangoproject.com/ticket/34920#comment:4>
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/0107018b53cfb624-6cbaa386-8083-4b5c-95d1-9d58d16bed34-000000%40eu-central-1.amazonses.com.

Reply via email to