Opened 4 weeks ago

Closed 4 weeks ago

#36577 closed Cleanup/optimization (fixed)

Remove try/except around GIS layermapping imports

Reported by: Adam Johnson Owned by: Adam Johnson
Component: GIS Version: dev
Severity: Normal Keywords:
Cc: Claude Paroz, David Smith Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

I tried 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:

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!

Change History (4)

comment:1 by Mariusz Felisiak, 4 weeks ago

Has patch: set
Triage Stage: UnreviewedAccepted

comment:2 by Natalia Bidart, 4 weeks ago

Cc: Claude Paroz David Smith added

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.

comment:3 by Tim Graham, 4 weeks ago

Triage Stage: AcceptedReady for checkin

I studied this and agree with the assessment.

comment:4 by nessita <124304+nessita@…>, 4 weeks ago

Resolution: fixed
Status: assignedclosed

In d8426f64:

Fixed #36577 -- Removed obsolete try-except for GIS layermapping imports.

Note: See TracTickets for help on using tickets.
Back to Top