Skip to content

Commit c55b333

Browse files
authored
fix: πŸ› Unable to call unlock in a module in use
1 parent d403120 commit c55b333

File tree

3 files changed

+25
-16
lines changed

3 files changed

+25
-16
lines changed

β€Žinjection/_core/module.pyβ€Ž

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,12 @@ def __str__(self) -> str:
156156
)
157157

158158

159+
@dataclass(frozen=True, slots=True)
160+
class UnlockCalled(Event):
161+
def __str__(self) -> str:
162+
return "An `unlock` method has been called."
163+
164+
159165
"""
160166
Broker
161167
"""
@@ -179,7 +185,7 @@ def is_locked(self) -> bool:
179185
raise NotImplementedError
180186

181187
@abstractmethod
182-
def unlock(self) -> Self:
188+
def unsafe_unlocking(self) -> None:
183189
raise NotImplementedError
184190

185191
@abstractmethod
@@ -289,12 +295,10 @@ def update[T](self, updater: Updater[T]) -> Self:
289295

290296
return self
291297

292-
def unlock(self) -> Self:
298+
def unsafe_unlocking(self) -> None:
293299
for injectable in self.__injectables:
294300
injectable.unlock()
295301

296-
return self
297-
298302
async def all_ready(self) -> None:
299303
for injectable in self.__injectables:
300304
if injectable.is_locked:
@@ -802,11 +806,17 @@ def change_priority(self, module: Module, priority: Priority | PriorityStr) -> S
802806
return self
803807

804808
def unlock(self) -> Self:
805-
for broker in self.__brokers:
806-
broker.unlock()
809+
event = UnlockCalled()
810+
811+
with self.dispatch(event, lock_bypass=True):
812+
self.unsafe_unlocking()
807813

808814
return self
809815

816+
def unsafe_unlocking(self) -> None:
817+
for broker in self.__brokers:
818+
broker.unsafe_unlocking()
819+
810820
def load_profile(self, *names: str) -> ContextManager[Self]:
811821
modules = (self.from_name(name) for name in names)
812822
self.unlock().init_modules(*modules)
@@ -839,8 +849,9 @@ def on_event(self, event: Event, /) -> ContextManager[None]:
839849
return self.dispatch(self_event)
840850

841851
@contextmanager
842-
def dispatch(self, event: Event) -> Iterator[None]:
843-
self.__check_locking()
852+
def dispatch(self, event: Event, *, lock_bypass: bool = False) -> Iterator[None]:
853+
if not lock_bypass:
854+
self.__check_locking()
844855

845856
with self.__channel.dispatch(event):
846857
try:

β€Žtests/core/test_module.pyβ€Ž

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -422,8 +422,6 @@ class B: ...
422422
assert a is not module.get_instance(A)
423423
assert b is not module.get_instance(B)
424424

425-
# TODO: not yet implemented
426-
@pytest.mark.skip
427425
def test_unlock_with_module_in_use_raise_module_lock_error(self, module):
428426
second_module = Module()
429427
module.use(second_module)

β€Žuv.lockβ€Ž

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
Β (0)