Skip to content

Commit

Permalink
fix: allow multiple uses of injector module in a single python process
Browse files Browse the repository at this point in the history
  • Loading branch information
danielknell committed Nov 23, 2023
1 parent 367955f commit 305bcde
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 10 deletions.
1 change: 1 addition & 0 deletions .dictionary
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ cls
composable
configurationerror
iterable
lineno
mypy
pragma
py
Expand Down
11 changes: 1 addition & 10 deletions src/banshee/extra/injector.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@

T = typing.TypeVar("T")


class InjectorHandlerFactory(banshee.HandlerFactory):
"""
Injector handler factory.
Expand Down Expand Up @@ -72,6 +71,7 @@ async def _handler(request: T) -> typing.Any:
return self.container.create_object(reference.handler)



class BansheeModule(injector.Module):
"""
Banshee module.
Expand All @@ -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(
Expand All @@ -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))
Expand Down
1 change: 1 addition & 0 deletions src/banshee/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]:
Expand Down
32 changes: 32 additions & 0 deletions tests/unit/test_injector_banshee_module.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 305bcde

Please sign in to comment.