From 559a99394a02aff174d9ad04a82ac563e2be7fd7 Mon Sep 17 00:00:00 2001 From: remimd Date: Tue, 13 Aug 2024 13:34:24 +0200 Subject: [PATCH] =?UTF-8?q?refactoring:=20=E2=9A=A1=EF=B8=8F=20Delete=20`?= =?UTF-8?q?=5F=5Fsetup=5Fqueue`=20when=20finished?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- injection/_core/common/invertible.py | 4 ++-- injection/_core/common/lazy.py | 10 ++++----- injection/_core/module.py | 33 +++++++++++++++++++++++----- 3 files changed, 34 insertions(+), 13 deletions(-) diff --git a/injection/_core/common/invertible.py b/injection/_core/common/invertible.py index db5c627..f5d5128 100644 --- a/injection/_core/common/invertible.py +++ b/injection/_core/common/invertible.py @@ -13,8 +13,8 @@ def __invert__(self) -> T: @dataclass(repr=False, eq=False, frozen=True, slots=True) class SimpleInvertible[T](Invertible[T]): - callable: Callable[..., T] + getter: Callable[..., T] @override def __invert__(self) -> T: - return self.callable() + return self.getter() diff --git a/injection/_core/common/lazy.py b/injection/_core/common/lazy.py index 62a4dc8..530d96e 100644 --- a/injection/_core/common/lazy.py +++ b/injection/_core/common/lazy.py @@ -6,9 +6,9 @@ class Lazy[T](Invertible[T]): - __slots__ = ("__cache", "__is_set") + __slots__ = ("__iterator", "__is_set") - __cache: Iterator[T] + __iterator: Iterator[T] __is_set: bool def __init__(self, factory: Callable[..., T]) -> None: @@ -16,14 +16,14 @@ def __init__(self, factory: Callable[..., T]) -> None: @override def __invert__(self) -> T: - return next(self.__cache) + return next(self.__iterator) @property def is_set(self) -> bool: return self.__is_set def __setup_cache(self, factory: Callable[..., T]) -> None: - def cache_generator() -> Iterator[T]: + def infinite_yield() -> Iterator[T]: nonlocal factory cached = factory() self.__is_set = True @@ -32,7 +32,7 @@ def cache_generator() -> Iterator[T]: while True: yield cached - self.__cache = cache_generator() + self.__iterator = infinite_yield() self.__is_set = False diff --git a/injection/_core/module.py b/injection/_core/module.py index db8fc76..ad629d0 100644 --- a/injection/_core/module.py +++ b/injection/_core/module.py @@ -775,15 +775,14 @@ class InjectedFunction[**P, T](EventListener): __wrapped__: Callable[P, T] __dependencies: Dependencies __owner: type | None - __setup_queue: Queue[Callable[..., None]] + __setup_queue: Queue[Callable[..., Any]] | None def __init__(self, wrapped: Callable[P, T], /) -> None: self.__update_vars_from(wrapped) update_wrapper(self, wrapped, updated=()) self.__dependencies = Dependencies.empty() self.__owner = None - self.__setup_queue = Queue(maxsize=2) - self.on_setup(self.__set_signature) + self.__setup_queue = Queue() @override def __repr__(self) -> str: # pragma: no cover @@ -813,7 +812,14 @@ def __set_name__(self, owner: type, name: str) -> None: @property def signature(self) -> Signature: - return self.__signature__ + with suppress(AttributeError): + return self.__signature__ + + with synchronized(): + signature = inspect.signature(self.wrapped, eval_str=True) + self.__signature__ = signature + + return signature @property def wrapped(self) -> Callable[P, T]: @@ -855,7 +861,11 @@ def update(self, module: Module) -> Self: def on_setup[**_P, _T](self, wrapped: Callable[_P, _T] | None = None, /): # type: ignore[no-untyped-def] def decorator(wp): # type: ignore[no-untyped-def] - self.__setup_queue.put_nowait(wp) + queue = self.__setup_queue + + if queue is not None: + queue.put_nowait(wp) + return wp return decorator(wrapped) if wrapped else decorator @@ -871,10 +881,20 @@ def _(self, event: ModuleEvent, /) -> Iterator[None]: yield self.update(event.module) + @synchronized() + def __close_setup_queue(self) -> None: + if self.__setup_queue is None: + return + + self.__setup_queue = None + def __setup(self) -> None: queue = self.__setup_queue - while not queue.empty(): + if queue is None: + return + + while True: try: task = queue.get_nowait() except Empty: @@ -884,6 +904,7 @@ def __setup(self) -> None: queue.task_done() queue.join() + self.__close_setup_queue() def __set_signature(self) -> None: self.__signature__ = inspect.signature(self.wrapped, eval_str=True)