Repository: django-guardian/django-guardian

Issue:
Using prefetch_permissions with an empty list throws an error
I you run the following code
```
checker = ObjectPermissionChecker(user)
checker.prefetch_perms([])
```
the error `UnboundLocalError: local variable 'model' referenced before assignment` is thrown.

I believe the fix is to set `model = None` at the top of `_get_pks_model_and_ctype`

Discussion:

Fix (patch):
diff --git a/guardian/core.py b/guardian/core.py
index 06f0635..c70677e 100644
--- a/guardian/core.py
+++ b/guardian/core.py
@@ -17,6 +17,10 @@ def _get_pks_model_and_ctype(objects):
     Assumes that all objects are of the same content type.
     """
 
+    model = None
+    ctype = None
+    pks = None
+
     if isinstance(objects, QuerySet):
         model = objects.model
         pks = [force_str(pk) for pk in objects.values_list("pk", flat=True)]


Test changes:
diff --git a/guardian/testapp/tests/test_core.py b/guardian/testapp/tests/test_core.py
index d76c600..a6c17b4 100644
--- a/guardian/testapp/tests/test_core.py
+++ b/guardian/testapp/tests/test_core.py
@@ -583,3 +583,30 @@ class ObjectPermissionCheckerTest(ObjectPermissionTestCase):
             guardian_settings.AUTO_PREFETCH = False
             ProjectUserObjectPermission.enabled = True
             ProjectGroupObjectPermission.enabled = True
+
+    def test_prefetch_user_perms_with_empty_objects(self):
+        settings.DEBUG = True
+        try:
+            from django.db import connection
+
+            ContentType.objects.clear_cache()
+            group1 = Group.objects.create(name="group1")
+            user = User.objects.create(username="active_user", is_active=True)
+            assign_perm("change_group", user, self.group)
+            assign_perm("change_group", user, group1)
+            checker = ObjectPermissionChecker(user)
+
+            # Checking that prefetching with empty objects does not raise an error
+            # This is a regression test for issue #519
+            prefetched_objects = []
+            self.assertTrue(checker.prefetch_perms(prefetched_objects))
+            query_count = len(connection.queries)
+
+            # Checking cache is empty
+            self.assertEqual(len(checker._obj_perms_cache), 0)
+
+            # Checking should work but will spawn queries as nothing was prefetched
+            checker.has_perm("change_group", self.group)
+            self.assertGreater(len(connection.queries), query_count)
+        finally:
+            settings.DEBUG = False