You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@casbin.apache.org by "vimzoomer (via GitHub)" <gi...@apache.org> on 2026/03/15 10:54:13 UTC

[PR] fix: lazy init and infinite recursion fixes [casbin-python-django-orm-adapter]

vimzoomer opened a new pull request, #5:
URL: https://github.com/apache/casbin-python-django-orm-adapter/pull/5

   Hi, I have two fixes for the adapter:
   1. Accessing database in `ready` is discouraged by Django, since `ProxyEnforcer` lazy-initializes Casbin anyway, initialization should be removed from `CasbinAdapterConfig`.
   2. Infinite recursion in `__getattribute__(self, name)` caused by `self.db_alias`. Also, If `enforcer` is imported in testing environment it breaks Django's test runner which calls `__class__` on every imported module during test discovery.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@casbin.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@casbin.apache.org
For additional commands, e-mail: commits-help@casbin.apache.org


Re: [PR] fix: lazy init and infinite recursion fixes [casbin-python-django-orm-adapter]

Posted by "vimzoomer (via GitHub)" <gi...@apache.org>.
vimzoomer commented on code in PR #5:
URL: https://github.com/apache/casbin-python-django-orm-adapter/pull/5#discussion_r2936931498


##########
casbin_adapter/enforcer.py:
##########
@@ -12,7 +12,7 @@
 
 class ProxyEnforcer(Enforcer):
     _initialized = False
-    db_alias = "default"
+    db_alias = getattr(settings, "CASBIN_DB_ALIAS", "default")

Review Comment:
   Using `django.conf.settings` at import time is generally acceptable, but can be moved to `ready` for extra pre-cautions



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@casbin.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@casbin.apache.org
For additional commands, e-mail: commits-help@casbin.apache.org


Re: [PR] fix: lazy init and infinite recursion fixes [casbin-python-django-orm-adapter]

Posted by "Copilot (via GitHub)" <gi...@apache.org>.
Copilot commented on code in PR #5:
URL: https://github.com/apache/casbin-python-django-orm-adapter/pull/5#discussion_r2936744475


##########
casbin_adapter/enforcer.py:
##########
@@ -43,9 +43,9 @@ def _load(self):
                 self.set_role_manager(role_manager)
 
     def __getattribute__(self, name):
-        safe_methods = ["__init__", "_load", "_initialized"]
+        safe_methods = ["__init__", "_load", "_initialized", "__class__"]

Review Comment:
   Accessing `enforcer.db_alias` while `_initialized` is false will currently trigger `initialize_enforcer(...)` (DB access) and may raise the "registry is ready" exception, even though callers might only be trying to read configuration. Add `"db_alias"` (and potentially other harmless introspection attrs) to `safe_methods` so reading the alias does not force initialization.
   



##########
casbin_adapter/enforcer.py:
##########
@@ -43,9 +43,9 @@ def _load(self):
                 self.set_role_manager(role_manager)
 
     def __getattribute__(self, name):
-        safe_methods = ["__init__", "_load", "_initialized"]
+        safe_methods = ["__init__", "_load", "_initialized", "__class__"]
         if not super().__getattribute__("_initialized") and name not in safe_methods:
-            initialize_enforcer(self.db_alias)
+            initialize_enforcer(super().__getattribute__("db_alias"))
             if not super().__getattribute__("_initialized"):

Review Comment:
   There are no tests covering the updated lazy-init / recursion-avoidance behavior in `ProxyEnforcer` (e.g., `__class__` access not triggering initialization, and no recursion when initializing). Add targeted tests to prevent regressions, since this logic is sensitive to Django startup/test-discovery behavior.



##########
casbin_adapter/enforcer.py:
##########
@@ -12,7 +12,7 @@
 
 class ProxyEnforcer(Enforcer):
     _initialized = False
-    db_alias = "default"
+    db_alias = getattr(settings, "CASBIN_DB_ALIAS", "default")

Review Comment:
   `db_alias` is being read from `django.conf.settings` at import/class-definition time. Accessing `settings` before Django is configured (e.g., importing this module outside a fully-initialized Django context or before `django.setup()`) can raise `ImproperlyConfigured`, and it also undermines the goal of deferring initialization. Consider keeping the class attribute as a plain default (e.g., "default") and reading `CASBIN_DB_ALIAS` lazily when needed (inside `_load()` / `__getattribute__()` / `initialize_enforcer`).
   



##########
casbin_adapter/apps.py:
##########
@@ -4,9 +4,3 @@
 
 class CasbinAdapterConfig(AppConfig):
     name = "casbin_adapter"

Review Comment:
   After removing `ready()`, `from django.conf import settings` is now unused in this module. Please remove the unused import to avoid lint/quality issues.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@casbin.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@casbin.apache.org
For additional commands, e-mail: commits-help@casbin.apache.org


Re: [PR] fix: lazy init and infinite recursion fixes [casbin-python-django-orm-adapter]

Posted by "hsluoyz (via GitHub)" <gi...@apache.org>.
hsluoyz merged PR #5:
URL: https://github.com/apache/casbin-python-django-orm-adapter/pull/5


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@casbin.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@casbin.apache.org
For additional commands, e-mail: commits-help@casbin.apache.org


Re: [PR] fix: lazy init and infinite recursion fixes [casbin-python-django-orm-adapter]

Posted by "vimzoomer (via GitHub)" <gi...@apache.org>.
vimzoomer commented on PR #5:
URL: https://github.com/apache/casbin-python-django-orm-adapter/pull/5#issuecomment-4063382331

   @hsluoyz 


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@casbin.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@casbin.apache.org
For additional commands, e-mail: commits-help@casbin.apache.org


Re: [PR] fix: lazy init and infinite recursion fixes [casbin-python-django-orm-adapter]

Posted by "vimzoomer (via GitHub)" <gi...@apache.org>.
vimzoomer commented on code in PR #5:
URL: https://github.com/apache/casbin-python-django-orm-adapter/pull/5#discussion_r2936995506


##########
casbin_adapter/enforcer.py:
##########
@@ -43,9 +43,9 @@ def _load(self):
                 self.set_role_manager(role_manager)
 
     def __getattribute__(self, name):
-        safe_methods = ["__init__", "_load", "_initialized"]
+        safe_methods = ["__init__", "_load", "_initialized", "__class__"]
         if not super().__getattribute__("_initialized") and name not in safe_methods:
-            initialize_enforcer(self.db_alias)
+            initialize_enforcer(super().__getattribute__("db_alias"))
             if not super().__getattribute__("_initialized"):

Review Comment:
   Testing it properly wouldn't be that easy, currently calling any method from `safe_methods` obviously won't trigger any errors, same with `super().__getattribute__("db_alias")`



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@casbin.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@casbin.apache.org
For additional commands, e-mail: commits-help@casbin.apache.org


Re: [PR] fix: lazy init and infinite recursion fixes [casbin-python-django-orm-adapter]

Posted by "vimzoomer (via GitHub)" <gi...@apache.org>.
vimzoomer commented on PR #5:
URL: https://github.com/apache/casbin-python-django-orm-adapter/pull/5#issuecomment-4063378714

   @copilot


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@casbin.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@casbin.apache.org
For additional commands, e-mail: commits-help@casbin.apache.org