Skip to content

Commit 4ebed98

Browse files
refactor: add cases for post_init method for attrs classes
1 parent 4535f9f commit 4ebed98

1 file changed

Lines changed: 14 additions & 6 deletions

File tree

openedx_authz/api/data.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,17 +74,25 @@ def __attrs_post_init__(self):
7474
7575
This method ensures that either external_key or namespaced_key is provided,
7676
and derives the other attribute based on the NAMESPACE and SEPARATOR.
77-
78-
Note:
79-
I will always instantiate with either external_key or namespaced_key, never both.
80-
So we need to derive the other one based on the NAMESPACE.
8177
"""
82-
if self.NAMESPACE and not self.namespaced_key:
78+
if not self.NAMESPACE:
79+
# No namespace defined, nothing to do
80+
return
81+
82+
# Case 1: Initialized with external_key only, derive namespaced_key
83+
if self.external_key and not self.namespaced_key:
8384
self.namespaced_key = f"{self.NAMESPACE}{self.SEPARATOR}{self.external_key}"
8485

85-
if self.NAMESPACE and not self.external_key and self.namespaced_key:
86+
# Case 2: Initialized with namespaced_key only, derive external_key
87+
if not self.external_key and self.namespaced_key:
8688
self.external_key = self.namespaced_key.split(self.SEPARATOR, 1)[1]
8789

90+
# Case 3: Neither provided, raise error
91+
if not self.external_key and not self.namespaced_key:
92+
raise ValueError(
93+
"Either external_key or namespaced_key must be provided."
94+
)
95+
8896

8997
class ScopeMeta(type):
9098
"""Metaclass for ScopeData to handle dynamic subclass instantiation based on namespace."""

0 commit comments

Comments
 (0)