Skip to content

Commit 73a43e6

Browse files
authored
Wiring config (ets-labs#516)
* Implement POC * Implement concept with WiringConfiguration object * Update changelog * Add docs * Update changelog
1 parent 08ea997 commit 73a43e6

File tree

6 files changed

+5252
-3380
lines changed

6 files changed

+5252
-3380
lines changed

docs/main/changelog.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ Develop
1111
-------
1212
- Improve wiring with adding importing modules and packages from a string
1313
``container.wire(modules=["yourapp.module1"])``.
14+
- Add container wiring configuration ``wiring_config = containers.WiringConfiguration()``.
1415
- Update documentation and fix typos.
1516

1617
4.36.2

docs/wiring.rst

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ Method ``container.wire()`` can resolve relative imports:
215215

216216
.. code-block:: python
217217
218-
# In module "yourapp.foo":
218+
# In module "yourapp.main":
219219
220220
container.wire(
221221
modules=[
@@ -348,6 +348,76 @@ You can use that in testing to re-create and re-wire a container before each tes
348348
349349
module.fn()
350350
351+
Wiring configuration
352+
--------------------
353+
354+
You can specify wiring configuration in the container. When wiring configuration is defined,
355+
container will call method ``.wire()`` automatically when you create an instance:
356+
357+
.. code-block:: python
358+
359+
class Container(containers.DeclarativeContainer):
360+
361+
wiring_config = containers.WiringConfiguration(
362+
modules=[
363+
"yourapp.module1",
364+
"yourapp.module2",
365+
],
366+
packages=[
367+
"yourapp.package1",
368+
"yourapp.package2",
369+
],
370+
)
371+
372+
...
373+
374+
375+
if __name__ == "__main__":
376+
container = Container() # container.wire() is called automatically
377+
...
378+
379+
You can also use relative imports. Container will resolve them corresponding
380+
to the module of the container class:
381+
382+
.. code-block:: python
383+
384+
# In module "yourapp.container":
385+
386+
class Container(containers.DeclarativeContainer):
387+
388+
wiring_config = containers.WiringConfiguration(
389+
modules=[
390+
".module1", # Resolved to: "yourapp.module1"
391+
".module2", # Resolved to: "yourapp.module2"
392+
],
393+
)
394+
)
395+
396+
397+
# In module "yourapp.foo.bar.main":
398+
399+
if __name__ == "__main__":
400+
container = Container() # wire to "yourapp.module1" and "yourapp.module2"
401+
...
402+
403+
To use wiring configuration and call method ``.wire()`` manually, set flag ``auto_wire=False``:
404+
405+
.. code-block:: python
406+
:emphasize-lines: 5
407+
408+
class Container(containers.DeclarativeContainer):
409+
410+
wiring_config = containers.WiringConfiguration(
411+
modules=["yourapp.module1"],
412+
auto_wire=False,
413+
)
414+
415+
416+
if __name__ == "__main__":
417+
container = Container() # container.wire() is NOT called automatically
418+
container.wire() # wire to "yourapp.module1"
419+
...
420+
351421
.. _async-injections-wiring:
352422

353423
Asynchronous injections

0 commit comments

Comments
 (0)