#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.