[Django] #36577: Remove try/except around GIS layermapping imports

5 views
Skip to first unread message

Django

unread,
Aug 27, 2025, 5:56:55 AMAug 27
to django-...@googlegroups.com
#36577: Remove try/except around GIS layermapping imports
-------------------------------------+-------------------------------------
Reporter: Adam | Owner: Adam Johnson
Johnson |
Type: | Status: assigned
Cleanup/optimization |
Component: GIS | Version: dev
Severity: Normal | Keywords:
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
I tried [https://docs.astral.sh/ty/ ty] on a client project and it
reported this error:

{{{
warning[possibly-unbound-import]: Member `LayerMapError` of module
`django.contrib.gis.utils` is possibly unbound
--> ...
|
36 | from django.contrib.contenttypes.models import ContentType
37 | from django.contrib.gis.geos import GEOSGeometry, MultiPolygon,
Point, Polygon
38 | from django.contrib.gis.utils import LayerMapError
| ^^^^^^^^^^^^^
39 | from django.contrib.messages import get_messages
40 | from django.contrib.messages.middleware import MessageMiddleware
|
info: rule `possibly-unbound-import` is enabled by default
}}}

Looking at the `django.contrib.gis.utils` source code, there’s a
`try`/`except ImproperlyConfigured` around the import of `LayerMapError`
and `LayerMapping`:

{{{#!python
try:
# LayerMapping requires DJANGO_SETTINGS_MODULE to be set,
# and ImproperlyConfigured is raised if that's not the case.
from django.contrib.gis.utils.layermapping import LayerMapError,
LayerMapping

__all__ += ["LayerMapError", "LayerMapping"]

except ImproperlyConfigured:
pass
}}}

This block blames back to 79e68c225b926302ebb29c808dda8afa49856f5c, the
initial merge of `django.contrib.gis` into Django core.

I looked at the code and couldn’t see how `ImproperlyConfigured` could be
raised at import time, and also tried it, with no error:

{{{
$ python -c 'import django.contrib.gis.utils.layermapping' # succeeds
}}}

I think the code path to `ImproperlyConfigured` has long been removed,
potentially in one of the bigger changes to
`django.contrib.gis.utils.layermapping`, such as
ff60c5f9de3e8690d1e86f3e9e3f7248a15397c8 (multiple database support), but
that’s hard to test since it requires running Python 2.7!
--
Ticket URL: <https://code.djangoproject.com/ticket/36577>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Aug 27, 2025, 7:59:23 AMAug 27
to django-...@googlegroups.com
#36577: Remove try/except around GIS layermapping imports
-------------------------------------+-------------------------------------
Reporter: Adam Johnson | Owner: Adam
Type: | Johnson
Cleanup/optimization | Status: assigned
Component: GIS | Version: dev
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* has_patch: 0 => 1
* stage: Unreviewed => Accepted

Comment:

[https://github.com/django/django/pull/19784 PR]
--
Ticket URL: <https://code.djangoproject.com/ticket/36577#comment:1>

Django

unread,
Aug 27, 2025, 8:03:38 AMAug 27
to django-...@googlegroups.com
#36577: Remove try/except around GIS layermapping imports
-------------------------------------+-------------------------------------
Reporter: Adam Johnson | Owner: Adam
Type: | Johnson
Cleanup/optimization | Status: assigned
Component: GIS | Version: dev
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Natalia Bidart):

* cc: Claude Paroz, David Smith (added)

Comment:

Thank you Adam, I think this makes sense. Accepting on the basis that all
tests pass, and also let's include Claude and David in the ticket to see
if they have any objection.
--
Ticket URL: <https://code.djangoproject.com/ticket/36577#comment:2>

Django

unread,
Aug 27, 2025, 8:21:24 AMAug 27
to django-...@googlegroups.com
#36577: Remove try/except around GIS layermapping imports
-------------------------------------+-------------------------------------
Reporter: Adam Johnson | Owner: Adam
Type: | Johnson
Cleanup/optimization | Status: assigned
Component: GIS | Version: dev
Severity: Normal | Resolution:
Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Tim Graham):

* stage: Accepted => Ready for checkin

Comment:

I studied this and agree with the assessment.
--
Ticket URL: <https://code.djangoproject.com/ticket/36577#comment:3>

Django

unread,
Aug 27, 2025, 12:16:40 PMAug 27
to django-...@googlegroups.com
#36577: Remove try/except around GIS layermapping imports
-------------------------------------+-------------------------------------
Reporter: Adam Johnson | Owner: Adam
Type: | Johnson
Cleanup/optimization | Status: closed
Component: GIS | Version: dev
Severity: Normal | Resolution: fixed
Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by nessita <124304+nessita@…>):

* resolution: => fixed
* status: assigned => closed

Comment:

In [changeset:"d8426f64a75356832655c457be3c9761102d9cf1" d8426f64]:
{{{#!CommitTicketReference repository=""
revision="d8426f64a75356832655c457be3c9761102d9cf1"
Fixed #36577 -- Removed obsolete try-except for GIS layermapping imports.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/36577#comment:4>
Reply all
Reply to author
Forward
0 new messages