[Django] #37310: MediaAsset instances with attributes violate equality/hash contract with strings

6 views
Skip to first unread message

Django

unread,
Sep 1, 2026, 3:05:08 AM (3 days ago) Sep 1
to django-...@googlegroups.com
#37310: MediaAsset instances with attributes violate equality/hash contract with
strings
-------------------------------------+-------------------------------------
Reporter: | Owner: miladkhoshdel
miladkhoshdel |
Type: Bug | Status: assigned
Component: Forms | Version: 6.1
Severity: Normal | Keywords: MediaAsset hash
Triage Stage: | equality
Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
After the fix for #37088, `MediaAsset.__eq__()` still considers an asset
equal to a string containing the same path, while `MediaAsset.__hash__()`
includes the asset's attributes.

This violates Python's requirement that equal objects have equal hashes.

Reproduction on Django 6.2.dev20260831204047:

{{{#!python
from django.forms import Script

asset = Script("/static/app.js", defer=True)
path = "/static/app.js"

print(asset == path)
print(hash(asset) == hash(path))
print(path in {asset})
}}}

Output:

{{{
True
False
False
}}}

Although `asset == path`, a set cannot find `path` because the two objects
are placed in different hash buckets. Dictionary lookups can be affected
in the same way. `Stylesheet` instances with attributes have the same
issue.

Expected behavior: Whenever `asset == path`, `hash(asset) == hash(path)`.

This appears to be a regression introduced by
bc9bed573ce39c3b739a4a3b7848816d464b6bdc for #37088 in Django 6.1.

A possible fix is to hash only `self._path`. Assets with the same path but
different attributes may then share a hash while remaining unequal, which
is valid Python behavior.
--
Ticket URL: <https://code.djangoproject.com/ticket/37310>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Sep 1, 2026, 3:37:35 AM (3 days ago) Sep 1
to django-...@googlegroups.com
#37310: MediaAsset instances with attributes violate equality/hash contract with
strings
-------------------------------------+-------------------------------------
Reporter: miladkhoshdel | Owner:
| miladkhoshdel
Type: Bug | Status: assigned
Component: Forms | Version: 6.1
Severity: Normal | Resolution:
Keywords: MediaAsset hash | Triage Stage:
equality | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by blighj):

* cc: Johannes, Maron (added)

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

Django

unread,
Sep 1, 2026, 3:38:09 AM (3 days ago) Sep 1
to django-...@googlegroups.com
#37310: MediaAsset instances with attributes violate equality/hash contract with
strings
-------------------------------------+-------------------------------------
Reporter: miladkhoshdel | Owner:
| miladkhoshdel
Type: Bug | Status: assigned
Component: Forms | Version: 6.1
Severity: Normal | Resolution:
Keywords: MediaAsset hash | Triage Stage:
equality | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by blighj):

* cc: Johannes, Maron (removed)

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

Django

unread,
Sep 1, 2026, 3:38:58 AM (3 days ago) Sep 1
to django-...@googlegroups.com
#37310: MediaAsset instances with attributes violate equality/hash contract with
strings
-------------------------------------+-------------------------------------
Reporter: miladkhoshdel | Owner:
| miladkhoshdel
Type: Bug | Status: assigned
Component: Forms | Version: 6.1
Severity: Normal | Resolution:
Keywords: MediaAsset hash | Triage Stage:
equality | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by blighj):

* cc: Johannes Maron (added)

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

Django

unread,
Sep 1, 2026, 5:13:24 AM (3 days ago) Sep 1
to django-...@googlegroups.com
#37310: MediaAsset instances with attributes violate equality/hash contract with
strings
-------------------------------------+-------------------------------------
Reporter: miladkhoshdel | Owner:
| miladkhoshdel
Type: Bug | Status: assigned
Component: Forms | Version: 6.1
Severity: Normal | Resolution:
Keywords: MediaAsset hash | Triage Stage:
equality | Unreviewed
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by miladkhoshdel):

* has_patch: 0 => 1

--
Ticket URL: <https://code.djangoproject.com/ticket/37310#comment:4>

Django

unread,
Sep 1, 2026, 5:17:40 AM (3 days ago) Sep 1
to django-...@googlegroups.com
#37310: MediaAsset instances with attributes violate equality/hash contract with
strings
-------------------------------------+-------------------------------------
Reporter: miladkhoshdel | Owner:
| miladkhoshdel
Type: Bug | Status: assigned
Component: Forms | Version: 6.1
Severity: Normal | Resolution:
Keywords: MediaAsset hash | Triage Stage:
equality | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by miladkhoshdel):

* has_patch: 1 => 0

--
Ticket URL: <https://code.djangoproject.com/ticket/37310#comment:5>

Django

unread,
Sep 1, 2026, 9:24:03 AM (3 days ago) Sep 1
to django-...@googlegroups.com
#37310: MediaAsset instances with attributes violate equality/hash contract with
strings
-------------------------------------+-------------------------------------
Reporter: miladkhoshdel | Owner:
| miladkhoshdel
Type: Bug | Status: assigned
Component: Forms | Version: 6.1
Severity: Normal | Resolution:
Keywords: MediaAsset hash | Triage Stage:
equality | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by miladkhoshdel):

I have a patch and regression test ready for this issue. PR #21866
[https://github.com/django/django/pull/21866] was automatically closed
because the ticket is still Unreviewed. I am continuing to work on the
ticket and will reopen or update the PR once the ticket is accepted.
--
Ticket URL: <https://code.djangoproject.com/ticket/37310#comment:6>
Reply all
Reply to author
Forward
0 new messages