﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
36577	Remove try/except around GIS layermapping imports	Adam Johnson	Adam Johnson	"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!"	Cleanup/optimization	closed	GIS	dev	Normal	fixed		Claude Paroz David Smith	Ready for checkin	1	0	0	0	0	0
