[Django] #35756: Auto reload raises TypeError: unhashable type: 'types.SimpleNamespace'

10 views
Skip to first unread message

Django

unread,
Sep 12, 2024, 7:40:25 AM9/12/24
to django-...@googlegroups.com
#35756: Auto reload raises TypeError: unhashable type: 'types.SimpleNamespace'
-------------------------------+--------------------------------------
Reporter: Paul Petersik | Type: Bug
Status: new | Component: Utilities
Version: 5.0 | Severity: Normal
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------
The `iter_modules_and_files` function from `django.utils.autoreload`
raises `TypeError: unhashable type: 'types.SimpleNamespace'` when a
`types.SimpleNamespace` is put into `sys.modules`.

This is for instance the case when the `zipp>=3.18.2` package is used (see
[https://github.com/jaraco/zipp/blob/d66007a66b7dbd88e69eaf59faae8b614cba256d/zipp/compat/overlay.py#L23])
which is used by `importlib_meta>=8.5.0` (see
[https://github.com/python/importlib_metadata/blob/90073b1aa7a49cc5fdbdc0e6e871f39e461b9422/pyproject.toml#L21])

The error can be reproduced by putting `from zipp.compat.overlay import
zipfile` somewhere in your django project.

This problem was already discussed and analyzed on stackoverflow:
https://stackoverflow.com/questions/78977665/django-autoreload-raises-
typeerror-unhashable-type-types-simplenamespace/78977785#78977785
--
Ticket URL: <https://code.djangoproject.com/ticket/35756>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Sep 12, 2024, 7:53:28 AM9/12/24
to django-...@googlegroups.com
#35756: Auto reload raises TypeError: unhashable type: 'types.SimpleNamespace'
-------------------------------+--------------------------------------
Reporter: Paul Petersik | Owner: (none)
Type: Bug | Status: new
Component: Utilities | Version: 5.0
Severity: Normal | Resolution:
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------
Description changed by Paul Petersik:

Old description:

> The `iter_modules_and_files` function from `django.utils.autoreload`
> raises `TypeError: unhashable type: 'types.SimpleNamespace'` when a
> `types.SimpleNamespace` is put into `sys.modules`.
>
> This is for instance the case when the `zipp>=3.18.2` package is used
> (see
> [https://github.com/jaraco/zipp/blob/d66007a66b7dbd88e69eaf59faae8b614cba256d/zipp/compat/overlay.py#L23])
> which is used by `importlib_meta>=8.5.0` (see
> [https://github.com/python/importlib_metadata/blob/90073b1aa7a49cc5fdbdc0e6e871f39e461b9422/pyproject.toml#L21])
>
> The error can be reproduced by putting `from zipp.compat.overlay import
> zipfile` somewhere in your django project.
>
> This problem was already discussed and analyzed on stackoverflow:
> https://stackoverflow.com/questions/78977665/django-autoreload-raises-
> typeerror-unhashable-type-types-simplenamespace/78977785#78977785

New description:

The `iter_modules_and_files` function from `django.utils.autoreload`
raises `TypeError: unhashable type: 'types.SimpleNamespace'` when a
`types.SimpleNamespace` is put into `sys.modules`.

This is for instance the case when the `zipp>=3.18.2` package is used (see
[https://github.com/jaraco/zipp/blob/d66007a66b7dbd88e69eaf59faae8b614cba256d/zipp/compat/overlay.py#L23])
which is used by `importlib_meta>=8.5.0` (see
[https://github.com/python/importlib_metadata/blob/90073b1aa7a49cc5fdbdc0e6e871f39e461b9422/pyproject.toml#L21])

The error can be reproduced by putting `from zipp.compat.overlay import
zipfile` somewhere in your django project.

This problem was already discussed and analyzed on stackoverflow:
[https://stackoverflow.com/questions/78977665/django-autoreload-raises-
typeerror-unhashable-type-types-simplenamespace/78977785#78977785]

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

Django

unread,
Sep 12, 2024, 8:21:52 AM9/12/24
to django-...@googlegroups.com
#35756: Auto reload raises TypeError: unhashable type: 'types.SimpleNamespace'
-------------------------------+--------------------------------------
Reporter: Paul Petersik | Owner: (none)
Type: Bug | Status: new
Component: Utilities | Version: 5.0
Severity: Normal | Resolution:
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------
Comment (by Tim Graham):

Related: The same crash was reported In #35085. It was concluded that
PyATS's monkey-patching of `sys.modules` wasn't something proper that
Django should account for.
--
Ticket URL: <https://code.djangoproject.com/ticket/35756#comment:2>

Django

unread,
Sep 12, 2024, 8:31:36 AM9/12/24
to django-...@googlegroups.com
#35756: Auto reload raises TypeError: unhashable type: 'types.SimpleNamespace'
-------------------------------+--------------------------------------
Reporter: Paul Petersik | Owner: (none)
Type: Bug | Status: closed
Component: Utilities | Version: 5.0
Severity: Normal | Resolution: duplicate
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------
Changes (by Sarah Boyce):

* resolution: => duplicate
* status: new => closed

Comment:

I can replicate an error, but as Tim mentioned, I will mark this is a
duplicate of #35085

{{{#!diff
--- a/tests/utils_tests/test_autoreload.py
+++ b/tests/utils_tests/test_autoreload.py
@@ -148,6 +148,12 @@ class TestIterModulesAndFiles(SimpleTestCase):
self.addCleanup(lambda: sys.modules.pop("time_proxy", None))
list(autoreload.iter_all_python_module_files()) # No crash.

+ def test_unhashable_objects_in_sys_module(self):
+ name_space = types.SimpleNamespace()
+ sys.modules["name_space"] = name_space # type:
ignore[assignment]
+ self.addCleanup(lambda: sys.modules.pop("name_space", None))
+ list(autoreload.iter_all_python_module_files()) # Crash
+
def test_module_without_spec(self):
module = types.ModuleType("test_module")
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/35756#comment:3>
Reply all
Reply to author
Forward
0 new messages