[Django] #34920: Reordered file extensions for improved validation

20 views
Skip to first unread message

Django

unread,
Oct 21, 2023, 1:51:38 PM10/21/23
to django-...@googlegroups.com
#34920: Reordered file extensions for improved validation
----------------------------------------+------------------------
Reporter: ksg97031 | Owner: nobody
Type: Bug | Status: new
Component: Core (Other) | Version: dev
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 1
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
----------------------------------------+------------------------
`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.

```python
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:

```python
self.assertEqual(
FileExtensionValidator(["jpg", "png", "txt"]),
FileExtensionValidator(["txt", "jpg", "png"]),
)
```

So I suggest comparing two extension arrays after sorting them.

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

Django

unread,
Oct 21, 2023, 1:52:30 PM10/21/23
to django-...@googlegroups.com
#34920: Reordered file extensions for improved validation
------------------------------+--------------------------------------

Reporter: ksg97031 | Owner: nobody
Type: Bug | Status: new
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
------------------------------+--------------------------------------
Description changed by ksg97031:

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.
>
> ```python
> 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:
>
> ```python
> 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.

--

--
Ticket URL: <https://code.djangoproject.com/ticket/34920#comment:1>

Django

unread,
Oct 21, 2023, 1:55:37 PM10/21/23
to django-...@googlegroups.com
#34920: Reordered file extensions for improved validation
------------------------------+--------------------------------------
Reporter: ksg97031 | Owner: nobody
Type: Bug | Status: new
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
------------------------------+--------------------------------------

Comment (by ksg97031):

PR: https://github.com/django/django/pull/17398

--
Ticket URL: <https://code.djangoproject.com/ticket/34920#comment:2>

Django

unread,
Oct 21, 2023, 2:30:40 PM10/21/23
to django-...@googlegroups.com
#34920: Reordered file extensions for improved validation
------------------------------+--------------------------------------
Reporter: ksg97031 | Owner: ksg97031
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
------------------------------+--------------------------------------
Changes (by ksg97031):

* owner: nobody => ksg97031
* status: new => assigned


--
Ticket URL: <https://code.djangoproject.com/ticket/34920#comment:3>

Django

unread,
Oct 21, 2023, 3:56:48 PM10/21/23
to django-...@googlegroups.com
#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.
>

New description:

This test case failed:

--

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>

Reply all
Reply to author
Forward
0 new messages