#34920 closed Bug (fixed)

FileExtensionValidator.__eq__() should ignore allowed_extensions order.

Reported by: ksg Owned by: ksg
Component: Core (Other) Version: dev
Severity: Normal Keywords:
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Tim Graham)

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.

Change History (10)

comment:1 by ksg, 14 months ago

Description: modified (diff)

comment:3 by ksg, 14 months ago

Owner: changed from nobody to ksg
Status: newassigned

comment:4 by Tim Graham, 14 months ago

Description: modified (diff)

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

comment:5 by Mariusz Felisiak, 14 months ago

Summary: Reordered file extensions for improved validationFileExtensionValidator.__eq__() should ignore allowed_extensions order.
Triage Stage: UnreviewedAccepted

comment:6 by Mariusz Felisiak, 14 months ago

Patch needs improvement: set

in reply to:  4 comment:7 by ksg, 14 months ago

Replying to 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?

No, it's just an improvement to make eq look better! Should I change the ticket type to "Cleanup/optimization"?

Last edited 14 months ago by ksg (previous) (diff)

comment:8 by ksg, 14 months ago

Patch needs improvement: unset

comment:9 by Mariusz Felisiak, 14 months ago

Triage Stage: AcceptedReady for checkin

comment:10 by Mariusz Felisiak <felisiak.mariusz@…>, 14 months ago

Resolution: fixed
Status: assignedclosed

In d22ba076:

Fixed #34920 -- Made FileExtensionValidator.eq() ignore allowed_extensions ordering.

Note: See TracTickets for help on using tickets.
Back to Top