Skip to content

Commit 759995d

Browse files
authored
fix: allow multiple uses of injector module in a single python process (#84)
1 parent 367955f commit 759995d

File tree

6 files changed

+92
-64
lines changed

6 files changed

+92
-64
lines changed

.dictionary

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ cls
33
composable
44
configurationerror
55
iterable
6+
lineno
67
mypy
78
pragma
89
py

.github/workflows/release-please.yml

Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -11,40 +11,40 @@ jobs:
1111
env:
1212
PACKAGE_NAME: "banshee"
1313
steps:
14-
- name: release please
15-
uses: google-github-actions/release-please-action@v3
16-
id: release
17-
with:
18-
release-type: python
19-
package-name: ${{ env.PACKAGE_NAME }}
20-
21-
- name: fetch code
22-
uses: actions/checkout@v4
23-
if: ${{ steps.release.outputs.release_created }}
24-
25-
- name: set python version
26-
if: ${{ steps.release.outputs.release_created }}
27-
uses: actions/setup-python@v4
28-
with:
29-
python-version: "3.10"
30-
31-
- name: install dependencies
32-
uses: artisanal-actions/poetry-install@v1
33-
if: ${{ steps.release.outputs.release_created }}
34-
35-
- name: build release
36-
if: ${{ steps.release.outputs.release_created }}
37-
run: poetry build
38-
39-
- name: upload assets
40-
uses: softprops/action-gh-release@v1
41-
if: ${{ steps.release.outputs.release_created }}
42-
with:
43-
tag_name: ${{ steps.release.outputs.tag_name }}
44-
files: dist/*
45-
46-
- name: publish release
47-
if: ${{ steps.release.outputs.release_created }}
48-
run: poetry publish
49-
env:
50-
POETRY_PYPI_TOKEN_PYPI: ${{ secrets.PYPI_AUTH_TOKEN }}
14+
- name: release please
15+
uses: google-github-actions/release-please-action@v3
16+
id: release
17+
with:
18+
release-type: python
19+
package-name: ${{ env.PACKAGE_NAME }}
20+
21+
- name: fetch code
22+
uses: actions/checkout@v4
23+
if: ${{ steps.release.outputs.release_created }}
24+
25+
- name: set python version
26+
if: ${{ steps.release.outputs.release_created }}
27+
uses: actions/setup-python@v4
28+
with:
29+
python-version: "3.10"
30+
31+
- name: install dependencies
32+
uses: artisanal-actions/poetry-install@v1
33+
if: ${{ steps.release.outputs.release_created }}
34+
35+
- name: build release
36+
if: ${{ steps.release.outputs.release_created }}
37+
run: poetry build
38+
39+
- name: upload assets
40+
uses: softprops/action-gh-release@v1
41+
if: ${{ steps.release.outputs.release_created }}
42+
with:
43+
tag_name: ${{ steps.release.outputs.tag_name }}
44+
files: dist/*
45+
46+
- name: publish release
47+
if: ${{ steps.release.outputs.release_created }}
48+
run: poetry publish
49+
env:
50+
POETRY_PYPI_TOKEN_PYPI: ${{ secrets.PYPI_AUTH_TOKEN }}

.github/workflows/tests.yaml

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,36 +3,38 @@ name: tests
33
on:
44
push:
55
paths-ignore:
6-
- 'docs/**'
6+
- "docs/**"
77
branches:
88
- main
99
pull_request:
1010
paths-ignore:
11-
- 'docs/**'
11+
- "docs/**"
1212
branches:
13-
- '**'
13+
- "**"
1414

1515
jobs:
1616
tests:
1717
runs-on: ubuntu-latest
1818

1919
steps:
20-
- name: fetch code
21-
uses: actions/checkout@v4
20+
- name: fetch code
21+
uses: actions/checkout@v4
2222

23-
- name: install en-gb dictionary
24-
run: sudo apt-get install -y hunspell-en-gb
23+
- name: install en-gb dictionary
24+
run: sudo apt-get install -y hunspell-en-gb
2525

26-
- name: set python version
27-
uses: actions/setup-python@v4
28-
with:
29-
python-version: "3.10"
26+
- name: set python version
27+
uses: actions/setup-python@v4
28+
with:
29+
python-version: "3.10"
3030

31-
- name: install dependencies
32-
uses: artisanal-actions/poetry-install@v1
31+
- name: install dependencies
32+
uses: artisanal-actions/poetry-install@v1
33+
with:
34+
extras: injector
3335

34-
- name: Install pytest plugin
35-
run: poetry run pip install pytest-github-actions-annotate-failures
36+
- name: Install pytest plugin
37+
run: poetry run pip install pytest-github-actions-annotate-failures
3638

37-
- name: run tests
38-
run: make ci
39+
- name: run tests
40+
run: make ci

src/banshee/extra/injector.py

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -92,11 +92,6 @@ def __init__(self, registry: banshee.Registry | None = None) -> None:
9292
self.registry = registry
9393

9494
def configure(self, binder: injector.Binder) -> None:
95-
binder.bind(injector.inject(banshee.IdentityMiddleware))
96-
binder.bind(injector.inject(banshee.CausationMiddleware))
97-
binder.bind(injector.inject(banshee.HandleAfterMiddleware))
98-
binder.bind(injector.inject(banshee.DispatchMiddleware))
99-
10095
if self.registry:
10196
# https://github.com/python/mypy/issues/4717
10297
binder.bind(
@@ -109,15 +104,11 @@ def configure(self, binder: injector.Binder) -> None:
109104
@injector.singleton
110105
@injector.provider
111106
def _provide_factory(self, container: injector.Injector) -> banshee.HandlerFactory:
112-
# pylint: disable=no-self-use
113-
114107
return InjectorHandlerFactory(container)
115108

116109
@injector.singleton
117110
@injector.provider
118111
def _provide_message_bus(self, container: injector.Injector) -> banshee.Bus:
119-
# pylint: disable=no-self-use
120-
121112
return (
122113
banshee.Builder()
123114
.with_middleware(container.get(banshee.IdentityMiddleware))

src/banshee/message.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ def message_for(
145145
:returns: a message for the request and context objects
146146
"""
147147
if not isinstance(request, Message):
148-
return Message(request, contexts=tuple(contexts) if contexts else tuple())
148+
return Message(request, contexts=tuple(contexts) if contexts else ())
149149

150150
if not contexts:
151151
return request
@@ -213,6 +213,7 @@ def __init__(self, iterator: collections.abc.Iterator[Middleware]) -> None:
213213
self.iterator = iterator
214214

215215
async def _final(self, message: Message[T], handle: HandleMessage) -> Message[T]:
216+
# pylint: disable=unused-argument
216217
return message
217218

218219
async def __call__(self, message: Message[T]) -> Message[T]:
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
"""
2+
Tests for :class:`banshee.extra.injector.BansheeModule`
3+
"""
4+
5+
import injector
6+
7+
import banshee
8+
import banshee.extra.injector
9+
10+
11+
def test_it_should_build_bus_instance() -> None:
12+
"""
13+
it should build bus instance.
14+
"""
15+
registry = banshee.Registry()
16+
container = injector.Injector(banshee.extra.injector.BansheeModule(registry))
17+
bus = container.get(banshee.Bus) # type: ignore
18+
19+
assert isinstance(bus, banshee.Bus)
20+
21+
22+
def test_it_should_allow_multiple_uses() -> None:
23+
"""
24+
it should allow multiple uses.
25+
"""
26+
registry = banshee.Registry()
27+
container = injector.Injector(banshee.extra.injector.BansheeModule(registry))
28+
bus1 = container.get(banshee.Bus) # type: ignore
29+
30+
container = injector.Injector(banshee.extra.injector.BansheeModule(registry))
31+
bus2 = container.get(banshee.Bus) # type: ignore
32+
33+
assert bus1 is not bus2

0 commit comments

Comments
 (0)