Skip to content

Commit fe1d9bc

Browse files
authored
fix: avoid circular import of AuthzEnforcer (#143)
* test: test can import AuthzEnforcer * fix: circular import in the enforcer * chore: bump version to 0.17.1
1 parent 5192606 commit fe1d9bc

4 files changed

Lines changed: 48 additions & 2 deletions

File tree

CHANGELOG.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,14 @@ Unreleased
1616

1717
*
1818

19+
0.17.1 - 2025-11-14
20+
********************
21+
22+
Fixed
23+
=====
24+
25+
* Avoid circular import of AuthzEnforcer.
26+
1927
0.17.0 - 2025-11-14
2028
********************
2129

openedx_authz/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@
44

55
import os
66

7-
__version__ = "0.17.0"
7+
__version__ = "0.17.1"
88

99
ROOT_DIRECTORY = os.path.dirname(os.path.abspath(__file__))

openedx_authz/engine/enforcer.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
from django.conf import settings
2323

2424
from openedx_authz.engine.adapter import ExtendedAdapter
25-
from openedx_authz.engine.matcher import is_admin_or_superuser_check
2625

2726

2827
def libraries_v2_enabled() -> bool:
@@ -201,6 +200,9 @@ def _initialize_enforcer(cls) -> SyncedEnforcer:
201200
Returns:
202201
SyncedEnforcer: Configured Casbin enforcer with adapter and auto-sync
203202
"""
203+
# Avoid circular import
204+
from openedx_authz.engine.matcher import is_admin_or_superuser_check # pylint: disable=import-outside-toplevel
205+
204206
db_alias = getattr(settings, "CASBIN_DB_ALIAS", "default")
205207

206208
try:
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
"""Test module imports."""
2+
import sys
3+
from unittest import TestCase
4+
5+
6+
class TestImports(TestCase):
7+
"""Test that imports work correctly."""
8+
9+
def setUp(self):
10+
"""Remove cached modules to ensure fresh imports and detect circular dependencies.
11+
"""
12+
super().setUp()
13+
14+
# List of modules to remove from cache to test fresh imports
15+
modules_to_clear = [
16+
'openedx_authz.engine.enforcer',
17+
'openedx_authz.engine.matcher',
18+
'openedx_authz.engine.adapter',
19+
'openedx_authz.api',
20+
'openedx_authz.api.permissions',
21+
'openedx_authz.api.roles',
22+
'openedx_authz.api.users',
23+
'openedx_authz.api.data',
24+
]
25+
26+
for module_name in modules_to_clear:
27+
if module_name in sys.modules:
28+
del sys.modules[module_name]
29+
30+
def test_import_authzenforcer(self):
31+
"""Test that AuthzEnforcer can be imported."""
32+
from openedx_authz.engine.enforcer import AuthzEnforcer # pylint: disable=import-outside-toplevel
33+
try:
34+
self.assertIsNotNone(AuthzEnforcer)
35+
except ImportError as e:
36+
self.fail(f"Failed to import AuthzEnforcer: {e}")

0 commit comments

Comments
 (0)