From 305bcde014aa58798b0f28840e9d2bd75296a5b1 Mon Sep 17 00:00:00 2001 From: Daniel Knell Date: Thu, 23 Nov 2023 17:17:59 +0000 Subject: [PATCH] fix: allow multiple uses of injector module in a single python process --- .dictionary | 1 + src/banshee/extra/injector.py | 11 +------- src/banshee/message.py | 1 + tests/unit/test_injector_banshee_module.py | 32 ++++++++++++++++++++++ 4 files changed, 35 insertions(+), 10 deletions(-) create mode 100644 tests/unit/test_injector_banshee_module.py diff --git a/.dictionary b/.dictionary index 81e2782..27254f1 100644 --- a/.dictionary +++ b/.dictionary @@ -3,6 +3,7 @@ cls composable configurationerror iterable +lineno mypy pragma py diff --git a/src/banshee/extra/injector.py b/src/banshee/extra/injector.py index f76d4d3..efaa75f 100644 --- a/src/banshee/extra/injector.py +++ b/src/banshee/extra/injector.py @@ -12,7 +12,6 @@ T = typing.TypeVar("T") - class InjectorHandlerFactory(banshee.HandlerFactory): """ Injector handler factory. @@ -72,6 +71,7 @@ async def _handler(request: T) -> typing.Any: return self.container.create_object(reference.handler) + class BansheeModule(injector.Module): """ Banshee module. @@ -92,11 +92,6 @@ def __init__(self, registry: banshee.Registry | None = None) -> None: self.registry = registry def configure(self, binder: injector.Binder) -> None: - binder.bind(injector.inject(banshee.IdentityMiddleware)) - binder.bind(injector.inject(banshee.CausationMiddleware)) - binder.bind(injector.inject(banshee.HandleAfterMiddleware)) - binder.bind(injector.inject(banshee.DispatchMiddleware)) - if self.registry: # https://github.com/python/mypy/issues/4717 binder.bind( @@ -109,15 +104,11 @@ def configure(self, binder: injector.Binder) -> None: @injector.singleton @injector.provider def _provide_factory(self, container: injector.Injector) -> banshee.HandlerFactory: - # pylint: disable=no-self-use - return InjectorHandlerFactory(container) @injector.singleton @injector.provider def _provide_message_bus(self, container: injector.Injector) -> banshee.Bus: - # pylint: disable=no-self-use - return ( banshee.Builder() .with_middleware(container.get(banshee.IdentityMiddleware)) diff --git a/src/banshee/message.py b/src/banshee/message.py index fe1c0bf..f162fb7 100644 --- a/src/banshee/message.py +++ b/src/banshee/message.py @@ -213,6 +213,7 @@ def __init__(self, iterator: collections.abc.Iterator[Middleware]) -> None: self.iterator = iterator async def _final(self, message: Message[T], handle: HandleMessage) -> Message[T]: + # pylint: disable=unused-argument return message async def __call__(self, message: Message[T]) -> Message[T]: diff --git a/tests/unit/test_injector_banshee_module.py b/tests/unit/test_injector_banshee_module.py new file mode 100644 index 0000000..9aca228 --- /dev/null +++ b/tests/unit/test_injector_banshee_module.py @@ -0,0 +1,32 @@ +""" +Tests for :class:`banshee.extra.injector.BansheeModule` +""" + +import injector + +import banshee +import banshee.extra.injector + +def test_it_should_build_bus_instance() -> None: + """ + it should build bus instance. + """ + registry = banshee.Registry() + container = injector.Injector(banshee.extra.injector.BansheeModule(registry)) + bus = container.get(banshee.Bus) # type: ignore + + assert isinstance(bus, banshee.Bus) + + +def test_it_should_allow_multiple_uses() -> None: + """ + it should allow multiple uses. + """ + registry = banshee.Registry() + container = injector.Injector(banshee.extra.injector.BansheeModule(registry)) + bus1 = container.get(banshee.Bus) # type: ignore + + container = injector.Injector(banshee.extra.injector.BansheeModule(registry)) + bus2 = container.get(banshee.Bus) # type: ignore + + assert bus1 is not bus2