Skip to content

Commit

Permalink
Wiring config (#516)
Browse files Browse the repository at this point in the history
* Implement POC

* Implement concept with WiringConfiguration object

* Update changelog

* Add docs

* Update changelog
  • Loading branch information
rmk135 committed Oct 4, 2021
1 parent 08ea997 commit 73a43e6
Show file tree
Hide file tree
Showing 6 changed files with 5,252 additions and 3,380 deletions.
1 change: 1 addition & 0 deletions docs/main/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Develop
-------
- Improve wiring with adding importing modules and packages from a string
``container.wire(modules=["yourapp.module1"])``.
- Add container wiring configuration ``wiring_config = containers.WiringConfiguration()``.
- Update documentation and fix typos.

4.36.2
Expand Down
72 changes: 71 additions & 1 deletion docs/wiring.rst
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ Method ``container.wire()`` can resolve relative imports:

.. code-block:: python
# In module "yourapp.foo":
# In module "yourapp.main":
container.wire(
modules=[
Expand Down Expand Up @@ -348,6 +348,76 @@ You can use that in testing to re-create and re-wire a container before each tes
module.fn()
Wiring configuration
--------------------

You can specify wiring configuration in the container. When wiring configuration is defined,
container will call method ``.wire()`` automatically when you create an instance:

.. code-block:: python
class Container(containers.DeclarativeContainer):
wiring_config = containers.WiringConfiguration(
modules=[
"yourapp.module1",
"yourapp.module2",
],
packages=[
"yourapp.package1",
"yourapp.package2",
],
)
...
if __name__ == "__main__":
container = Container() # container.wire() is called automatically
...
You can also use relative imports. Container will resolve them corresponding
to the module of the container class:

.. code-block:: python
# In module "yourapp.container":
class Container(containers.DeclarativeContainer):
wiring_config = containers.WiringConfiguration(
modules=[
".module1", # Resolved to: "yourapp.module1"
".module2", # Resolved to: "yourapp.module2"
],
)
)
# In module "yourapp.foo.bar.main":
if __name__ == "__main__":
container = Container() # wire to "yourapp.module1" and "yourapp.module2"
...
To use wiring configuration and call method ``.wire()`` manually, set flag ``auto_wire=False``:

.. code-block:: python
:emphasize-lines: 5
class Container(containers.DeclarativeContainer):
wiring_config = containers.WiringConfiguration(
modules=["yourapp.module1"],
auto_wire=False,
)
if __name__ == "__main__":
container = Container() # container.wire() is NOT called automatically
container.wire() # wire to "yourapp.module1"
...
.. _async-injections-wiring:

Asynchronous injections
Expand Down

0 comments on commit 73a43e6

Please sign in to comment.