Repository: joke2k/django-environ
Pull request #482 (2023-08-27 19:38:57)

Use importlib.util.find_spec to replace pkgutil.find_loader
Aim: suppress the warning in #479 

`importlib.util.find_spec` is available from python 3.4, so it is compatible with python 3.6+


Patch:
diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml
index 783d742..6110260 100644
--- a/.github/workflows/build.yml
+++ b/.github/workflows/build.yml
@@ -27,7 +27,7 @@ jobs:
         uses: actions/checkout@v3.5.3
 
       - name: Set up Python 3.10
-        uses: actions/setup-python@v4.6.1
+        uses: actions/setup-python@v4.7.0
         with:
           python-version: '3.10'
 
@@ -67,7 +67,7 @@ jobs:
         uses: actions/checkout@v3.5.3
 
       - name: Set up Python 3.10
-        uses: actions/setup-python@v4.6.1
+        uses: actions/setup-python@v4.7.0
         with:
           python-version: '3.10'
 
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
index f7b5d67..86eefcb 100644
--- a/.github/workflows/ci.yml
+++ b/.github/workflows/ci.yml
@@ -71,7 +71,7 @@ jobs:
           fetch-depth: 5
 
       - name: Set up Python ${{ matrix.python }}
-        uses: actions/setup-python@v4.6.1
+        uses: actions/setup-python@v4.7.0
         with:
           python-version: ${{ matrix.python }}
 
diff --git a/.github/workflows/cs.yml b/.github/workflows/cs.yml
index 2e4c640..f2c9e4b 100644
--- a/.github/workflows/cs.yml
+++ b/.github/workflows/cs.yml
@@ -28,7 +28,7 @@ jobs:
         uses: actions/checkout@v3.5.3
 
       - name: Set up Python 3.10
-        uses: actions/setup-python@v4.6.1
+        uses: actions/setup-python@v4.7.0
         with:
           python-version: '3.10'
 
diff --git a/.github/workflows/docs.yml b/.github/workflows/docs.yml
index ff76054..6b91877 100644
--- a/.github/workflows/docs.yml
+++ b/.github/workflows/docs.yml
@@ -29,7 +29,7 @@ jobs:
         uses: actions/checkout@v3.5.3
 
       - name: Set up Python 3.10
-        uses: actions/setup-python@v4.6.1
+        uses: actions/setup-python@v4.7.0
         with:
           python-version: '3.10'
 
diff --git a/environ/compat.py b/environ/compat.py
index b4ffee4..49b5b48 100644
--- a/environ/compat.py
+++ b/environ/compat.py
@@ -8,14 +8,14 @@
 
 """This module handles import compatibility issues."""
 
-from pkgutil import find_loader
+from importlib.util import find_spec
 
-if find_loader('simplejson'):
+if find_spec('simplejson'):
     import simplejson as json
 else:
     import json
 
-if find_loader('django'):
+if find_spec('django'):
     from django import VERSION as DJANGO_VERSION
     from django.core.exceptions import ImproperlyConfigured
 else:
@@ -29,7 +29,7 @@ def choose_rediscache_driver():
     """Backward compatibility for RedisCache driver."""
 
     # django-redis library takes precedence
-    if find_loader('django_redis'):
+    if find_spec('django_redis'):
         return 'django_redis.cache.RedisCache'
 
     # use built-in support if Django 4+
@@ -51,7 +51,7 @@ def choose_postgres_driver():
 def choose_pymemcache_driver():
     """Backward compatibility for pymemcache."""
     old_django = DJANGO_VERSION is not None and DJANGO_VERSION < (3, 2)
-    if old_django or not find_loader('pymemcache'):
+    if old_django or not find_spec('pymemcache'):
         # The original backend choice for the 'pymemcache' scheme is
         # unfortunately 'pylibmc'.
         return 'django.core.cache.backends.memcached.PyLibMCCache'


Test patch:
diff --git a/tests/test_cache.py b/tests/test_cache.py
index 37d7b6e..a8aff16 100644
--- a/tests/test_cache.py
+++ b/tests/test_cache.py
@@ -107,8 +107,8 @@ def test_pymemcache_compat(django_version, pymemcache_installed):
     old = 'django.core.cache.backends.memcached.PyLibMCCache'
     new = 'django.core.cache.backends.memcached.PyMemcacheCache'
     with mock.patch.object(environ.compat, 'DJANGO_VERSION', django_version):
-        with mock.patch('environ.compat.find_loader') as mock_find_loader:
-            mock_find_loader.return_value = pymemcache_installed
+        with mock.patch('environ.compat.find_spec') as mock_find_spec:
+            mock_find_spec.return_value = pymemcache_installed
             driver = environ.compat.choose_pymemcache_driver()
             if django_version and django_version < (3, 2):
                 assert driver == old
@@ -124,8 +124,8 @@ def test_rediscache_compat(django_version, django_redis_installed):
     django_redis = 'django_redis.cache.RedisCache'
 
     with mock.patch.object(environ.compat, 'DJANGO_VERSION', django_version):
-        with mock.patch('environ.compat.find_loader') as mock_find_loader:
-            mock_find_loader.return_value = django_redis_installed
+        with mock.patch('environ.compat.find_spec') as mock_find_spec:
+            mock_find_spec.return_value = django_redis_installed
             driver = environ.compat.choose_rediscache_driver()
             if django_redis_installed:
                 assert driver == django_redis