Compare commits

..

No commits in common. "master" and "4.43.0" have entirely different histories.

102 changed files with 269869 additions and 1882 deletions

10
.coveragerc Normal file
View File

@ -0,0 +1,10 @@
[run]
source = dependency_injector
omit = tests/unit
plugins = Cython.Coverage
[report]
show_missing = true
[html]
directory=reports/unittests/

View File

@ -10,7 +10,7 @@ jobs:
tests: tests:
name: Run tests name: Run tests
runs-on: ubuntu-24.04 runs-on: ubuntu-22.04
steps: steps:
- uses: actions/checkout@v3 - uses: actions/checkout@v3
- uses: actions/setup-python@v4 - uses: actions/setup-python@v4
@ -23,7 +23,7 @@ jobs:
linters: linters:
name: Run linters name: Run linters
runs-on: ubuntu-24.04 runs-on: ubuntu-22.04
strategy: strategy:
matrix: matrix:
toxenv: [flake8, pydocstyle, mypy, pylint] toxenv: [flake8, pydocstyle, mypy, pylint]
@ -40,18 +40,17 @@ jobs:
build-sdist: build-sdist:
name: Build source tarball name: Build source tarball
needs: [tests, linters] needs: [tests, linters]
runs-on: ubuntu-24.04 runs-on: ubuntu-22.04
steps: steps:
- uses: actions/checkout@v3 - uses: actions/checkout@v3
- uses: actions/setup-python@v4 - uses: actions/setup-python@v4
with: with:
python-version: 3.13 python-version: 3.13
- run: | - run: |
python -m pip install --upgrade build python -m pip install --upgrade pip setuptools
python -m build --sdist python setup.py sdist
- uses: actions/upload-artifact@v4 - uses: actions/upload-artifact@v3
with: with:
name: cibw-sdist
path: ./dist/* path: ./dist/*
build-wheels: build-wheels:
@ -60,28 +59,45 @@ jobs:
runs-on: ${{ matrix.os }} runs-on: ${{ matrix.os }}
strategy: strategy:
matrix: matrix:
os: [ubuntu-24.04, ubuntu-24.04-arm, windows-2019, macos-14] os: [ubuntu-22.04, windows-2019, macos-14]
env: env:
CIBW_SKIP: cp27-* CIBW_SKIP: cp27-*
steps: steps:
- uses: actions/checkout@v3 - uses: actions/checkout@v3
- name: Build wheels - name: Build wheels
uses: pypa/cibuildwheel@v2.20.0 uses: pypa/cibuildwheel@v2.20.0
- uses: actions/upload-artifact@v4 - uses: actions/upload-artifact@v3
with:
path: ./wheelhouse/*.whl
build-wheels-linux-aarch64:
name: Build wheels (ubuntu-22.04-aarch64)
needs: [tests, linters]
runs-on: ubuntu-22.04
env:
CIBW_SKIP: cp27-*
steps:
- uses: actions/checkout@v3
- name: Set up QEMU
if: runner.os == 'Linux'
uses: docker/setup-qemu-action@v2
- name: Build wheels
uses: pypa/cibuildwheel@v2.20.0
env:
CIBW_ARCHS_LINUX: aarch64
- uses: actions/upload-artifact@v3
with: with:
name: cibw-wheels-x86-${{ matrix.os }}-${{ strategy.job-index }}
path: ./wheelhouse/*.whl path: ./wheelhouse/*.whl
publish: publish:
name: Publish on PyPI name: Publish on PyPI
needs: [build-sdist, build-wheels] needs: [build-sdist, build-wheels, build-wheels-linux-aarch64]
runs-on: ubuntu-24.04 runs-on: ubuntu-22.04
steps: steps:
- uses: actions/download-artifact@v4 - uses: actions/download-artifact@v3
with: with:
pattern: cibw-* name: artifact
path: dist path: dist
merge-multiple: true
- uses: pypa/gh-action-pypi-publish@release/v1 - uses: pypa/gh-action-pypi-publish@release/v1
with: with:
user: __token__ user: __token__
@ -93,7 +109,7 @@ jobs:
publish-docs: publish-docs:
name: Publish docs name: Publish docs
needs: [publish] needs: [publish]
runs-on: ubuntu-24.04 runs-on: ubuntu-22.04
steps: steps:
- uses: actions/checkout@v3 - uses: actions/checkout@v3
- uses: actions/setup-python@v4 - uses: actions/setup-python@v4

View File

@ -36,17 +36,6 @@ jobs:
env: env:
TOXENV: ${{ matrix.python-version }} TOXENV: ${{ matrix.python-version }}
test-different-pydantic-versions:
name: Run tests with different pydantic versions
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-python@v4
with:
python-version: "3.12"
- run: pip install tox
- run: tox -e pydantic-v1,pydantic-v2
test-coverage: test-coverage:
name: Run tests with coverage name: Run tests with coverage
runs-on: ubuntu-latest runs-on: ubuntu-latest
@ -61,6 +50,7 @@ jobs:
with: with:
python-version: 3.12 python-version: 3.12
- run: pip install tox 'cython>=3,<4' - run: pip install tox 'cython>=3,<4'
- run: make cythonize
- run: tox -vv - run: tox -vv
env: env:
TOXENV: coveralls TOXENV: coveralls

12
.gitignore vendored
View File

@ -63,11 +63,13 @@ venv*/
# Vim Rope # Vim Rope
.ropeproject/ .ropeproject/
# Cython artifacts # C extensions
src/**/*.c src/dependency_injector/*.h
src/**/*.h src/dependency_injector/*.so
src/**/*.so src/dependency_injector/containers/*.h
src/**/*.html src/dependency_injector/containers/*.so
src/dependency_injector/providers/*.h
src/dependency_injector/providers/*.so
# Workspace for samples # Workspace for samples
.workspace/ .workspace/

49
.pylintrc Normal file
View File

@ -0,0 +1,49 @@
[MASTER]
# Add <file or directory> to the black list. It should be a base name, not a
# path. You may set this option multiple times.
ignore=utils,tests
[MESSAGES CONTROL]
# Disable the message(s) with the given id(s).
# disable-msg=
[SIMILARITIES]
# Minimum lines number of a similarity.
min-similarity-lines=5
[TYPECHECK]
ignore-mixin-members=yes
# ignored-classes=
zope=no
# generated-members=providedBy,implementedBy,rawDataReceived
[DESIGN]
# Maximum number of arguments for function / method
max-args=10
# Maximum number of locals for function / method body
max-locals=20
# Maximum number of return / yield for function / method body
max-returns=10
# Maximum number of branch for function / method body
max-branchs=10
# Maximum number of statements in function / method body
max-statements=60
# Maximum number of parents for a class (see R0901).
max-parents=10
# Maximum number of attributes for a class (see R0902).
max-attributes=30
# Minimum number of public methods for a class (see R0903).
min-public-methods=0
# Maximum number of public methods for a class (see R0904).
max-public-methods=30

View File

@ -1,6 +1,14 @@
VERSION := $(shell python setup.py --version) VERSION := $(shell python setup.py --version)
export COVERAGE_RCFILE := pyproject.toml CYTHON_SRC := $(shell find src/dependency_injector -name '*.pyx')
CYTHON_DIRECTIVES = -Xlanguage_level=3
ifdef DEPENDENCY_INJECTOR_DEBUG_MODE
CYTHON_DIRECTIVES += -Xprofile=True
CYTHON_DIRECTIVES += -Xlinetrace=True
endif
clean: clean:
# Clean sources # Clean sources
@ -17,17 +25,21 @@ clean:
find examples -name '*.py[co]' -delete find examples -name '*.py[co]' -delete
find examples -name '__pycache__' -delete find examples -name '__pycache__' -delete
build: clean cythonize:
# Compile C extensions # Compile Cython to C
python setup.py build_ext --inplace cython -a $(CYTHON_DIRECTIVES) $(CYTHON_SRC)
# Move all Cython html reports # Move all Cython html reports
mkdir -p reports/cython/ mkdir -p reports/cython/
find src -name '*.html' -exec mv {} reports/cython/ \; find src -name '*.html' -exec mv {} reports/cython/ \;
build: clean cythonize
# Compile C extensions
python setup.py build_ext --inplace
docs-live: docs-live:
sphinx-autobuild docs docs/_build/html sphinx-autobuild docs docs/_build/html
install: uninstall clean build install: uninstall clean cythonize
pip install -ve . pip install -ve .
uninstall: uninstall:
@ -36,9 +48,9 @@ uninstall:
test: test:
# Unit tests with coverage report # Unit tests with coverage report
coverage erase coverage erase
coverage run -m pytest -c tests/.configs/pytest.ini coverage run --rcfile=./.coveragerc -m pytest -c tests/.configs/pytest.ini
coverage report coverage report --rcfile=./.coveragerc
coverage html coverage html --rcfile=./.coveragerc
check: check:
flake8 src/dependency_injector/ flake8 src/dependency_injector/
@ -49,9 +61,9 @@ check:
mypy tests/typing mypy tests/typing
test-publish: build test-publish: cythonize
# Create distributions # Create distributions
python -m build --sdist python setup.py sdist
# Upload distributions to PyPI # Upload distributions to PyPI
twine upload --repository testpypi dist/dependency-injector-$(VERSION)* twine upload --repository testpypi dist/dependency-injector-$(VERSION)*

View File

@ -7,48 +7,6 @@ that were made in every particular version.
From version 0.7.6 *Dependency Injector* framework strictly From version 0.7.6 *Dependency Injector* framework strictly
follows `Semantic versioning`_ follows `Semantic versioning`_
4.46.0
------
- Add option to disable env var interpolation in configs (`#861 <https://github.com/ets-labs/python-dependency-injector/pull/861>`_)
- Fix ``Closing`` dependency resolution (`#852 <https://github.com/ets-labs/python-dependency-injector/pull/852>`_)
- Add support for ``inspect.iscoroutinefunction()`` in ``Coroutine`` provider (`#830 <https://github.com/ets-labs/python-dependency-injector/pull/830>`_)
- Fix broken wiring of sync inject-decorated methods (`#673 <https://github.com/ets-labs/python-dependency-injector/pull/673>`_)
- Add support for ``typing.Annotated`` (`#721 <https://github.com/ets-labs/python-dependency-injector/pull/721>`_, `#853 <https://github.com/ets-labs/python-dependency-injector/pull/853>`_)
- Documentation updates for movie-lister example (`#747 <https://github.com/ets-labs/python-dependency-injector/pull/747>`_)
- Fix type propagation in ``Provider.provider`` (`#744 <https://github.com/ets-labs/python-dependency-injector/pull/744>`_)
Many thanks for the contributions to:
- `ZipFile <https://github.com/ZipFile>`_
- `Yegor Statkevich <https://github.com/jazzthief>`_
- `Federico Tomasi <https://github.com/federinik>`_
- `Martin Lafrance <https://github.com/martlaf>`_
- `Philip Bjorge <https://github.com/philipbjorge>`_
- `Ilya Kazakov <https://github.com/mrKazzila>`_
4.45.0
--------
- Add Starlette lifespan handler implementation (`#683 <https://github.com/ets-labs/python-dependency-injector/pull/683>`_).
- Raise exception in ``ThreadLocalSingleton`` instead of hiding it in finally (`#845 <https://github.com/ets-labs/python-dependency-injector/pull/845>`_).
- Improve debuggability of ``deepcopy`` errors (`#839 <https://github.com/ets-labs/python-dependency-injector/pull/839>`_).
- Update examples (`#838 <https://github.com/ets-labs/python-dependency-injector/pull/838>`_).
- Upgrade testing dependencies (`#837 <https://github.com/ets-labs/python-dependency-injector/pull/837>`_).
- Add minor fixes to the documentation (`#709 <https://github.com/ets-labs/python-dependency-injector/pull/709>`_).
- Remove ``six`` from the dependencies (`3ba4704 <https://github.com/ets-labs/python-dependency-injector/commit/3ba4704bc1cb00310749fd2eda0c8221167c313c>`_).
Many thanks for the contributions to:
- `ZipFile <https://github.com/ZipFile>`_
- `František Trebuňa <https://github.com/gortibaldik>`_
- `JC (Jonathan Chen) <https://github.com/dijonkitchen>`_
4.44.0
--------
- Implement support for Pydantic 2. PR: `#832 <https://github.com/ets-labs/python-dependency-injector/pull/832>`_.
- Implement `PEP-517 <https://peps.python.org/pep-0517/>`_, `PEP-518 <https://peps.python.org/pep-0518/>`_, and
`PEP-621 <https://peps.python.org/pep-0621/>`_. PR: `#829 <https://github.com/ets-labs/python-dependency-injector/pull/829>`_.
Many thanks to `ZipFile <https://github.com/ZipFile>`_ for both contributions.
4.43.0 4.43.0
-------- --------
- Add support for Python 3.13. - Add support for Python 3.13.

View File

@ -183,22 +183,22 @@ See also: :ref:`configuration-envs-interpolation`.
Loading from a Pydantic settings Loading from a Pydantic settings
-------------------------------- --------------------------------
``Configuration`` provider can load configuration from a ``pydantic_settings.BaseSettings`` object using the ``Configuration`` provider can load configuration from a ``pydantic`` settings object using the
:py:meth:`Configuration.from_pydantic` method: :py:meth:`Configuration.from_pydantic` method:
.. literalinclude:: ../../examples/providers/configuration/configuration_pydantic.py .. literalinclude:: ../../examples/providers/configuration/configuration_pydantic.py
:language: python :language: python
:lines: 3- :lines: 3-
:emphasize-lines: 32 :emphasize-lines: 31
To get the data from pydantic settings ``Configuration`` provider calls its ``model_dump()`` method. To get the data from pydantic settings ``Configuration`` provider calls ``Settings.dict()`` method.
If you need to pass an argument to this call, use ``.from_pydantic()`` keyword arguments. If you need to pass an argument to this call, use ``.from_pydantic()`` keyword arguments.
.. code-block:: python .. code-block:: python
container.config.from_pydantic(Settings(), exclude={"optional"}) container.config.from_pydantic(Settings(), exclude={"optional"})
Alternatively, you can provide a ``pydantic_settings.BaseSettings`` object over the configuration provider argument. In that case, Alternatively, you can provide a ``pydantic`` settings object over the configuration provider argument. In that case,
the container will call ``config.from_pydantic()`` automatically: the container will call ``config.from_pydantic()`` automatically:
.. code-block:: python .. code-block:: python
@ -215,23 +215,18 @@ the container will call ``config.from_pydantic()`` automatically:
.. note:: .. note::
``Dependency Injector`` doesn't install ``pydantic-settings`` by default. ``Dependency Injector`` doesn't install ``pydantic`` by default.
You can install the ``Dependency Injector`` with an extra dependency:: You can install the ``Dependency Injector`` with an extra dependency::
pip install dependency-injector[pydantic2] pip install dependency-injector[pydantic]
or install ``pydantic-settings`` directly:: or install ``pydantic`` directly::
pip install pydantic-settings pip install pydantic
*Don't forget to mirror the changes in the requirements file.* *Don't forget to mirror the changes in the requirements file.*
.. note::
For backward-compatibility, Pydantic v1 is still supported.
Passing ``pydantic.BaseSettings`` instances will work just as fine as ``pydantic_settings.BaseSettings``.
Loading from a dictionary Loading from a dictionary
------------------------- -------------------------
@ -366,19 +361,6 @@ See also: :ref:`configuration-strict-mode`.
assert container.config.section.option() is None assert container.config.section.option() is None
If you want to disable environment variables interpolation, pass ``envs_required=None``:
.. code-block:: yaml
:caption: templates.yml
template_string: 'Hello, ${name}!'
.. code-block:: python
>>> container.config.from_yaml("templates.yml", envs_required=None)
>>> container.config.template_string()
'Hello, ${name}!'
Mandatory and optional sources Mandatory and optional sources
------------------------------ ------------------------------

View File

@ -24,7 +24,7 @@ returns it on the rest of the calls.
.. note:: .. note::
``Singleton`` provider makes dependencies injection only when it creates an object. When an object ``Singleton`` provider makes dependencies injection only when creates an object. When an object
is created and memorized ``Singleton`` provider just returns it without applying injections. is created and memorized ``Singleton`` provider just returns it without applying injections.
Specialization of the provided type and abstract singletons work the same like like for the Specialization of the provided type and abstract singletons work the same like like for the

View File

@ -18,7 +18,7 @@ In this tutorial we will use:
- Python 3 - Python 3
- Docker - Docker
- Docker Compose - Docker-compose
Start from the scratch or jump to the section: Start from the scratch or jump to the section:
@ -47,27 +47,28 @@ response it will log:
Prerequisites Prerequisites
------------- -------------
We will use `docker compose <https://docs.docker.com/compose/>`_ in this tutorial. Let's check the versions: We will use `Docker <https://www.docker.com/>`_ and
`docker-compose <https://docs.docker.com/compose/>`_ in this tutorial. Let's check the versions:
.. code-block:: bash .. code-block:: bash
docker --version docker --version
docker compose version docker-compose --version
The output should look something like: The output should look something like:
.. code-block:: bash .. code-block:: bash
Docker version 27.3.1, build ce12230 Docker version 20.10.5, build 55c4c88
Docker Compose version v2.29.7 docker-compose version 1.29.0, build 07737305
.. note:: .. note::
If you don't have ``Docker`` or ``docker compose`` you need to install them before proceeding. If you don't have ``Docker`` or ``docker-compose`` you need to install them before proceeding.
Follow these installation guides: Follow these installation guides:
- `Install Docker <https://docs.docker.com/get-docker/>`_ - `Install Docker <https://docs.docker.com/get-docker/>`_
- `Install docker compose <https://docs.docker.com/compose/install/>`_ - `Install docker-compose <https://docs.docker.com/compose/install/>`_
The prerequisites are satisfied. Let's get started with the project layout. The prerequisites are satisfied. Let's get started with the project layout.
@ -128,13 +129,13 @@ Put next lines into the ``requirements.txt`` file:
pytest-cov pytest-cov
Second, we need to create the ``Dockerfile``. It will describe the daemon's build process and Second, we need to create the ``Dockerfile``. It will describe the daemon's build process and
specify how to run it. We will use ``python:3.13-bookworm`` as a base image. specify how to run it. We will use ``python:3.9-buster`` as a base image.
Put next lines into the ``Dockerfile`` file: Put next lines into the ``Dockerfile`` file:
.. code-block:: bash .. code-block:: bash
FROM python:3.13-bookworm FROM python:3.10-buster
ENV PYTHONUNBUFFERED=1 ENV PYTHONUNBUFFERED=1
@ -154,6 +155,8 @@ Put next lines into the ``docker-compose.yml`` file:
.. code-block:: yaml .. code-block:: yaml
version: "3.7"
services: services:
monitor: monitor:
@ -168,7 +171,7 @@ Run in the terminal:
.. code-block:: bash .. code-block:: bash
docker compose build docker-compose build
The build process may take a couple of minutes. You should see something like this in the end: The build process may take a couple of minutes. You should see something like this in the end:
@ -181,7 +184,7 @@ After the build is done run the container:
.. code-block:: bash .. code-block:: bash
docker compose up docker-compose up
The output should look like: The output should look like:
@ -458,7 +461,7 @@ Run in the terminal:
.. code-block:: bash .. code-block:: bash
docker compose up docker-compose up
The output should look like: The output should look like:
@ -702,7 +705,7 @@ Run in the terminal:
.. code-block:: bash .. code-block:: bash
docker compose up docker-compose up
You should see: You should see:
@ -810,7 +813,7 @@ Run in the terminal:
.. code-block:: bash .. code-block:: bash
docker compose up docker-compose up
You should see: You should see:
@ -962,16 +965,15 @@ Run in the terminal:
.. code-block:: bash .. code-block:: bash
docker compose run --rm monitor py.test monitoringdaemon/tests.py --cov=monitoringdaemon docker-compose run --rm monitor py.test monitoringdaemon/tests.py --cov=monitoringdaemon
You should see: You should see:
.. code-block:: bash .. code-block:: bash
platform linux -- Python 3.13.1, pytest-8.3.4, pluggy-1.5.0 platform linux -- Python 3.10.0, pytest-6.2.5, py-1.10.0, pluggy-1.0.0
rootdir: /code rootdir: /code
plugins: cov-6.0.0, asyncio-0.24.0 plugins: asyncio-0.16.0, cov-3.0.0
asyncio: mode=Mode.STRICT, default_loop_scope=None
collected 2 items collected 2 items
monitoringdaemon/tests.py .. [100%] monitoringdaemon/tests.py .. [100%]

View File

@ -911,7 +911,7 @@ Create ``tests.py`` in the ``movies`` package:
and put next into it: and put next into it:
.. code-block:: python .. code-block:: python
:emphasize-lines: 41,50 :emphasize-lines: 36,51
"""Tests module.""" """Tests module."""
@ -941,18 +941,13 @@ and put next into it:
return container return container
@pytest.fixture def test_movies_directed_by(container):
def finder_mock(container):
finder_mock = mock.Mock() finder_mock = mock.Mock()
finder_mock.find_all.return_value = [ finder_mock.find_all.return_value = [
container.movie("The 33", 2015, "Patricia Riggen"), container.movie("The 33", 2015, "Patricia Riggen"),
container.movie("The Jungle Book", 2016, "Jon Favreau"), container.movie("The Jungle Book", 2016, "Jon Favreau"),
] ]
return finder_mock
def test_movies_directed_by(container, finder_mock):
with container.finder.override(finder_mock): with container.finder.override(finder_mock):
lister = container.lister() lister = container.lister()
movies = lister.movies_directed_by("Jon Favreau") movies = lister.movies_directed_by("Jon Favreau")
@ -961,7 +956,13 @@ and put next into it:
assert movies[0].title == "The Jungle Book" assert movies[0].title == "The Jungle Book"
def test_movies_released_in(container, finder_mock): def test_movies_released_in(container):
finder_mock = mock.Mock()
finder_mock.find_all.return_value = [
container.movie("The 33", 2015, "Patricia Riggen"),
container.movie("The Jungle Book", 2016, "Jon Favreau"),
]
with container.finder.override(finder_mock): with container.finder.override(finder_mock):
lister = container.lister() lister = container.lister()
movies = lister.movies_released_in(2015) movies = lister.movies_released_in(2015)
@ -994,9 +995,9 @@ You should see:
movies/entities.py 7 1 86% movies/entities.py 7 1 86%
movies/finders.py 26 13 50% movies/finders.py 26 13 50%
movies/listers.py 8 0 100% movies/listers.py 8 0 100%
movies/tests.py 24 0 100% movies/tests.py 23 0 100%
------------------------------------------ ------------------------------------------
TOTAL 90 30 67% TOTAL 89 30 66%
.. note:: .. note::

View File

@ -64,7 +64,7 @@ FastAPI example:
@app.api_route("/") @app.api_route("/")
@inject @inject
async def index(service: Annotated[Service, Depends(Provide[Container.service])]): async def index(service: Service = Depends(Provide[Container.service])):
value = await service.process() value = await service.process()
return {"result": value} return {"result": value}

View File

@ -98,9 +98,8 @@ The output should be something like:
.. code-block:: .. code-block::
platform linux -- Python 3.12.3, pytest-8.3.2, pluggy-1.5.0 platform darwin -- Python 3.10.0, pytest-6.2.5, py-1.10.0, pluggy-1.0.0
plugins: cov-6.0.0, anyio-4.4.0, asyncio-0.24.0, aiohttp-1.0.5 plugins: asyncio-0.16.0, anyio-3.3.4, aiohttp-0.3.0, cov-3.0.0
asyncio: mode=Mode.STRICT, default_loop_scope=None
collected 3 items collected 3 items
giphynavigator/tests.py ... [100%] giphynavigator/tests.py ... [100%]

View File

@ -3,15 +3,11 @@
from unittest import mock from unittest import mock
import pytest import pytest
import pytest_asyncio
from giphynavigator.application import create_app from giphynavigator.application import create_app
from giphynavigator.giphy import GiphyClient from giphynavigator.giphy import GiphyClient
pytestmark = pytest.mark.asyncio
@pytest.fixture @pytest.fixture
def app(): def app():
app = create_app() app = create_app()
@ -19,9 +15,9 @@ def app():
app.container.unwire() app.container.unwire()
@pytest_asyncio.fixture @pytest.fixture
async def client(app, aiohttp_client): def client(app, aiohttp_client, loop):
return await aiohttp_client(app) return loop.run_until_complete(aiohttp_client(app))
async def test_index(client, app): async def test_index(client, app):

View File

@ -2,5 +2,4 @@ dependency-injector
aiohttp aiohttp
pyyaml pyyaml
pytest-aiohttp pytest-aiohttp
pytest-asyncio
pytest-cov pytest-cov

View File

@ -1,4 +1,4 @@
FROM python:3.13-bookworm FROM python:3.10-buster
ENV PYTHONUNBUFFERED=1 ENV PYTHONUNBUFFERED=1

View File

@ -13,13 +13,13 @@ Build the Docker image:
.. code-block:: bash .. code-block:: bash
docker compose build docker-compose build
Run the docker-compose environment: Run the docker-compose environment:
.. code-block:: bash .. code-block:: bash
docker compose up docker-compose up
The output should be something like: The output should be something like:
@ -59,16 +59,15 @@ To run the tests do:
.. code-block:: bash .. code-block:: bash
docker compose run --rm monitor py.test monitoringdaemon/tests.py --cov=monitoringdaemon docker-compose run --rm monitor py.test monitoringdaemon/tests.py --cov=monitoringdaemon
The output should be something like: The output should be something like:
.. code-block:: .. code-block::
platform linux -- Python 3.13.1, pytest-8.3.4, pluggy-1.5.0 platform linux -- Python 3.10.0, pytest-6.2.5, py-1.10.0, pluggy-1.0.0
rootdir: /code rootdir: /code
plugins: cov-6.0.0, asyncio-0.24.0 plugins: asyncio-0.16.0, cov-3.0.0
asyncio: mode=Mode.STRICT, default_loop_scope=None
collected 2 items collected 2 items
monitoringdaemon/tests.py .. [100%] monitoringdaemon/tests.py .. [100%]

View File

@ -61,7 +61,7 @@ async def test_example_monitor(container, caplog):
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_dispatcher(container, caplog): async def test_dispatcher(container, caplog, event_loop):
caplog.set_level("INFO") caplog.set_level("INFO")
example_monitor_mock = mock.AsyncMock() example_monitor_mock = mock.AsyncMock()
@ -72,7 +72,6 @@ async def test_dispatcher(container, caplog):
httpbin_monitor=httpbin_monitor_mock, httpbin_monitor=httpbin_monitor_mock,
): ):
dispatcher = container.dispatcher() dispatcher = container.dispatcher()
event_loop = asyncio.get_running_loop()
event_loop.create_task(dispatcher.start()) event_loop.create_task(dispatcher.start())
await asyncio.sleep(0.1) await asyncio.sleep(0.1)
dispatcher.stop() dispatcher.stop()

View File

@ -1,4 +1,4 @@
FROM python:3.13-bookworm FROM python:3.10-buster
ENV PYTHONUNBUFFERED=1 ENV PYTHONUNBUFFERED=1

View File

@ -12,13 +12,13 @@ Build the Docker image:
.. code-block:: bash .. code-block:: bash
docker compose build docker-compose build
Run the docker-compose environment: Run the docker-compose environment:
.. code-block:: bash .. code-block:: bash
docker compose up docker-compose up
The output should be something like: The output should be something like:
@ -54,16 +54,16 @@ To run the tests do:
.. code-block:: bash .. code-block:: bash
docker compose run --rm example py.test fastapiredis/tests.py --cov=fastapiredis docker-compose run --rm example py.test fastapiredis/tests.py --cov=fastapiredis
The output should be something like: The output should be something like:
.. code-block:: .. code-block::
platform linux -- Python 3.13.1, pytest-8.3.4, pluggy-1.5.0 platform linux -- Python 3.10.9, pytest-7.2.0, pluggy-1.0.0
rootdir: /code rootdir: /code
plugins: cov-6.0.0, asyncio-0.24.0, anyio-4.7.0 plugins: cov-4.0.0, asyncio-0.20.3
asyncio: mode=Mode.STRICT, default_loop_scope=None collected 1 item
fastapiredis/tests.py . [100%] fastapiredis/tests.py . [100%]
@ -77,4 +77,4 @@ The output should be something like:
fastapiredis/services.py 7 3 57% fastapiredis/services.py 7 3 57%
fastapiredis/tests.py 18 0 100% fastapiredis/tests.py 18 0 100%
------------------------------------------------- -------------------------------------------------
TOTAL 52 7 87% TOTAL 52 7 87%

View File

@ -1,22 +1,18 @@
"""Application module.""" """Application module."""
from typing import Annotated from dependency_injector.wiring import inject, Provide
from fastapi import FastAPI, Depends
from fastapi import Depends, FastAPI
from dependency_injector.wiring import Provide, inject
from .containers import Container from .containers import Container
from .services import Service from .services import Service
app = FastAPI() app = FastAPI()
@app.api_route("/") @app.api_route("/")
@inject @inject
async def index( async def index(service: Service = Depends(Provide[Container.service])):
service: Annotated[Service, Depends(Provide[Container.service])]
) -> dict[str, str]:
value = await service.process() value = await service.process()
return {"result": value} return {"result": value}

View File

@ -1,6 +1,6 @@
from typing import AsyncIterator from typing import AsyncIterator
from redis.asyncio import from_url, Redis from aioredis import from_url, Redis
async def init_redis_pool(host: str, password: str) -> AsyncIterator[Redis]: async def init_redis_pool(host: str, password: str) -> AsyncIterator[Redis]:

View File

@ -1,6 +1,6 @@
"""Services module.""" """Services module."""
from redis.asyncio import Redis from aioredis import Redis
class Service: class Service:

View File

@ -3,7 +3,7 @@
from unittest import mock from unittest import mock
import pytest import pytest
from httpx import ASGITransport, AsyncClient from httpx import AsyncClient
from .application import app, container from .application import app, container
from .services import Service from .services import Service
@ -11,10 +11,7 @@ from .services import Service
@pytest.fixture @pytest.fixture
def client(event_loop): def client(event_loop):
client = AsyncClient( client = AsyncClient(app=app, base_url="http://test")
transport=ASGITransport(app=app),
base_url="http://test",
)
yield client yield client
event_loop.run_until_complete(client.aclose()) event_loop.run_until_complete(client.aclose())

View File

@ -1,7 +1,7 @@
dependency-injector dependency-injector
fastapi fastapi
uvicorn uvicorn
redis>=4.2 aioredis
# For testing: # For testing:
pytest pytest

View File

@ -1,7 +1,4 @@
from typing import Annotated from fastapi import FastAPI, Depends
from fastapi import Depends, FastAPI
from dependency_injector import containers, providers from dependency_injector import containers, providers
from dependency_injector.wiring import Provide, inject from dependency_injector.wiring import Provide, inject
@ -21,9 +18,7 @@ app = FastAPI()
@app.api_route("/") @app.api_route("/")
@inject @inject
async def index( async def index(service: Service = Depends(Provide[Container.service])):
service: Annotated[Service, Depends(Provide[Container.service])]
) -> dict[str, str]:
result = await service.process() result = await service.process()
return {"result": result} return {"result": result}

View File

@ -1,18 +1,14 @@
from unittest import mock from unittest import mock
import pytest import pytest
import pytest_asyncio from httpx import AsyncClient
from httpx import ASGITransport, AsyncClient
from fastapi_di_example import app, container, Service from fastapi_di_example import app, container, Service
@pytest_asyncio.fixture @pytest.fixture
async def client(): async def client(event_loop):
async with AsyncClient( async with AsyncClient(app=app, base_url="http://test") as client:
transport=ASGITransport(app=app),
base_url="http://test",
) as client:
yield client yield client

View File

@ -1,4 +1,4 @@
FROM python:3.13-bookworm FROM python:3.10-buster
ENV PYTHONUNBUFFERED=1 ENV PYTHONUNBUFFERED=1
ENV HOST=0.0.0.0 ENV HOST=0.0.0.0

View File

@ -15,13 +15,13 @@ Build the Docker image:
.. code-block:: bash .. code-block:: bash
docker compose build docker-compose build
Run the docker-compose environment: Run the docker-compose environment:
.. code-block:: bash .. code-block:: bash
docker compose up docker-compose up
The output should be something like: The output should be something like:
@ -67,15 +67,15 @@ To run the tests do:
.. code-block:: bash .. code-block:: bash
docker compose run --rm webapp py.test webapp/tests.py --cov=webapp docker-compose run --rm webapp py.test webapp/tests.py --cov=webapp
The output should be something like: The output should be something like:
.. code-block:: .. code-block::
platform linux -- Python 3.13.1, pytest-8.3.4, pluggy-1.5.0 platform linux -- Python 3.10.0, pytest-6.2.5, py-1.10.0, pluggy-1.0.0
rootdir: /code rootdir: /code
plugins: cov-6.0.0, anyio-4.7.0 plugins: cov-3.0.0
collected 7 items collected 7 items
webapp/tests.py ....... [100%] webapp/tests.py ....... [100%]

View File

@ -1,5 +1,5 @@
dependency-injector dependency-injector
fastapi[standard] fastapi
uvicorn uvicorn
pyyaml pyyaml
sqlalchemy sqlalchemy

View File

@ -1,14 +1,11 @@
"""Endpoints module.""" """Endpoints module."""
from typing import Annotated
from fastapi import APIRouter, Depends, Response, status from fastapi import APIRouter, Depends, Response, status
from dependency_injector.wiring import inject, Provide
from dependency_injector.wiring import Provide, inject
from .containers import Container from .containers import Container
from .repositories import NotFoundError
from .services import UserService from .services import UserService
from .repositories import NotFoundError
router = APIRouter() router = APIRouter()
@ -16,7 +13,7 @@ router = APIRouter()
@router.get("/users") @router.get("/users")
@inject @inject
def get_list( def get_list(
user_service: Annotated[UserService, Depends(Provide[Container.user_service])], user_service: UserService = Depends(Provide[Container.user_service]),
): ):
return user_service.get_users() return user_service.get_users()
@ -24,8 +21,8 @@ def get_list(
@router.get("/users/{user_id}") @router.get("/users/{user_id}")
@inject @inject
def get_by_id( def get_by_id(
user_id: int, user_id: int,
user_service: Annotated[UserService, Depends(Provide[Container.user_service])], user_service: UserService = Depends(Provide[Container.user_service]),
): ):
try: try:
return user_service.get_user_by_id(user_id) return user_service.get_user_by_id(user_id)
@ -36,7 +33,7 @@ def get_by_id(
@router.post("/users", status_code=status.HTTP_201_CREATED) @router.post("/users", status_code=status.HTTP_201_CREATED)
@inject @inject
def add( def add(
user_service: Annotated[UserService, Depends(Provide[Container.user_service])], user_service: UserService = Depends(Provide[Container.user_service]),
): ):
return user_service.create_user() return user_service.create_user()
@ -44,9 +41,9 @@ def add(
@router.delete("/users/{user_id}", status_code=status.HTTP_204_NO_CONTENT) @router.delete("/users/{user_id}", status_code=status.HTTP_204_NO_CONTENT)
@inject @inject
def remove( def remove(
user_id: int, user_id: int,
user_service: Annotated[UserService, Depends(Provide[Container.user_service])], user_service: UserService = Depends(Provide[Container.user_service]),
) -> Response: ):
try: try:
user_service.delete_user_by_id(user_id) user_service.delete_user_by_id(user_id)
except NotFoundError: except NotFoundError:

View File

@ -101,9 +101,9 @@ The output should be something like:
.. code-block:: .. code-block::
platform linux -- Python 3.12.3, pytest-8.3.2, pluggy-1.5.0 platform darwin -- Python 3.10.0, pytest-6.2.5, py-1.10.0, pluggy-1.0.0
plugins: cov-6.0.0, anyio-4.4.0, asyncio-0.24.0, aiohttp-1.0.5 plugins: asyncio-0.16.0, cov-3.0.0
asyncio: mode=Mode.STRICT, default_loop_scope=None collected 3 items
giphynavigator/tests.py ... [100%] giphynavigator/tests.py ... [100%]

View File

@ -1,14 +1,13 @@
"""Endpoints module.""" """Endpoints module."""
from typing import Annotated, List from typing import Optional, List
from fastapi import APIRouter, Depends from fastapi import APIRouter, Depends
from pydantic import BaseModel from pydantic import BaseModel
from dependency_injector.wiring import inject, Provide
from dependency_injector.wiring import Provide, inject
from .containers import Container
from .services import SearchService from .services import SearchService
from .containers import Container
class Gif(BaseModel): class Gif(BaseModel):
@ -27,15 +26,11 @@ router = APIRouter()
@router.get("/", response_model=Response) @router.get("/", response_model=Response)
@inject @inject
async def index( async def index(
default_query: Annotated[str, Depends(Provide[Container.config.default.query])], query: Optional[str] = None,
default_limit: Annotated[ limit: Optional[str] = None,
int, Depends(Provide[Container.config.default.limit.as_int()]) default_query: str = Depends(Provide[Container.config.default.query]),
], default_limit: int = Depends(Provide[Container.config.default.limit.as_int()]),
search_service: Annotated[ search_service: SearchService = Depends(Provide[Container.search_service]),
SearchService, Depends(Provide[Container.search_service])
],
query: str | None = None,
limit: int | None = None,
): ):
query = query or default_query query = query or default_query
limit = limit or default_limit limit = limit or default_limit

View File

@ -3,19 +3,15 @@
from unittest import mock from unittest import mock
import pytest import pytest
import pytest_asyncio from httpx import AsyncClient
from httpx import ASGITransport, AsyncClient
from giphynavigator.application import app from giphynavigator.application import app
from giphynavigator.giphy import GiphyClient from giphynavigator.giphy import GiphyClient
@pytest_asyncio.fixture @pytest.fixture
async def client(): async def client():
async with AsyncClient( async with AsyncClient(app=app, base_url="http://test") as client:
transport=ASGITransport(app=app),
base_url="http://test",
) as client:
yield client yield client

View File

@ -81,9 +81,8 @@ The output should be something like:
.. code-block:: .. code-block::
platform linux -- Python 3.12.3, pytest-8.3.2, pluggy-1.5.0 platform darwin -- Python 3.10.0, pytest-6.2.5, py-1.10.0, pluggy-1.0.0
plugins: cov-6.0.0, flask-1.3.0 plugins: cov-3.0.0, flask-1.2.0
asyncio: mode=Mode.STRICT, default_loop_scope=None
collected 2 items collected 2 items
githubnavigator/tests.py .. [100%] githubnavigator/tests.py .. [100%]

View File

@ -1,7 +1,7 @@
"""Application module.""" """Application module."""
from flask import Flask from flask import Flask
from flask_bootstrap import Bootstrap4 from flask_bootstrap import Bootstrap
from .containers import Container from .containers import Container
from .blueprints import example from .blueprints import example
@ -15,7 +15,7 @@ def create_app() -> Flask:
app.container = container app.container = container
app.register_blueprint(example.blueprint) app.register_blueprint(example.blueprint)
bootstrap = Bootstrap4() bootstrap = Bootstrap()
bootstrap.init_app(app) bootstrap.init_app(app)
return app return app

View File

@ -81,9 +81,8 @@ The output should be something like:
.. code-block:: .. code-block::
platform linux -- Python 3.12.3, pytest-8.3.2, pluggy-1.5.0 platform darwin -- Python 3.10.0, pytest-6.2.5, py-1.10.0, pluggy-1.0.0
plugins: cov-6.0.0, flask-1.3.0 plugins: cov-3.0.0, flask-1.2.0
asyncio: mode=Mode.STRICT, default_loop_scope=None
collected 2 items collected 2 items
githubnavigator/tests.py .. [100%] githubnavigator/tests.py .. [100%]

View File

@ -1,7 +1,7 @@
"""Application module.""" """Application module."""
from flask import Flask from flask import Flask
from flask_bootstrap import Bootstrap4 from flask_bootstrap import Bootstrap
from .containers import Container from .containers import Container
from . import views from . import views
@ -15,7 +15,7 @@ def create_app() -> Flask:
app.container = container app.container = container
app.add_url_rule("/", "index", views.index) app.add_url_rule("/", "index", views.index)
bootstrap = Bootstrap4() bootstrap = Bootstrap()
bootstrap.init_app(app) bootstrap.init_app(app)
return app return app

View File

@ -58,8 +58,8 @@ The output should be something like:
.. code-block:: .. code-block::
platform linux -- Python 3.12.3, pytest-8.3.2, pluggy-1.5.0 platform darwin -- Python 3.10.0, pytest-6.2.5, py-1.10.0, pluggy-1.0.0
plugins: cov-6.0.0 plugins: cov-3.0.0
collected 2 items collected 2 items
movies/tests.py .. [100%] movies/tests.py .. [100%]

View File

@ -26,18 +26,13 @@ def container():
return container return container
@pytest.fixture def test_movies_directed_by(container):
def finder_mock(container):
finder_mock = mock.Mock() finder_mock = mock.Mock()
finder_mock.find_all.return_value = [ finder_mock.find_all.return_value = [
container.movie("The 33", 2015, "Patricia Riggen"), container.movie("The 33", 2015, "Patricia Riggen"),
container.movie("The Jungle Book", 2016, "Jon Favreau"), container.movie("The Jungle Book", 2016, "Jon Favreau"),
] ]
return finder_mock
def test_movies_directed_by(container, finder_mock):
with container.finder.override(finder_mock): with container.finder.override(finder_mock):
lister = container.lister() lister = container.lister()
movies = lister.movies_directed_by("Jon Favreau") movies = lister.movies_directed_by("Jon Favreau")
@ -46,7 +41,13 @@ def test_movies_directed_by(container, finder_mock):
assert movies[0].title == "The Jungle Book" assert movies[0].title == "The Jungle Book"
def test_movies_released_in(container, finder_mock): def test_movies_released_in(container):
finder_mock = mock.Mock()
finder_mock.find_all.return_value = [
container.movie("The 33", 2015, "Patricia Riggen"),
container.movie("The Jungle Book", 2016, "Jon Favreau"),
]
with container.finder.override(finder_mock): with container.finder.override(finder_mock):
lister = container.lister() lister = container.lister()
movies = lister.movies_released_in(2015) movies = lister.movies_released_in(2015)

View File

@ -27,7 +27,7 @@ To run the application do:
.. code-block:: bash .. code-block:: bash
export GIPHY_API_KEY=wBJ2wZG7SRqfrU9nPgPiWvORmloDyuL0 export GIPHY_API_KEY=wBJ2wZG7SRqfrU9nPgPiWvORmloDyuL0
sanic giphynavigator.application:create_app python -m giphynavigator
The output should be something like: The output should be something like:
@ -98,9 +98,8 @@ The output should be something like:
.. code-block:: .. code-block::
platform linux -- Python 3.12.3, pytest-8.3.2, pluggy-1.5.0 platform darwin -- Python 3.10.0, pytest-6.2.5, py-1.10.0, pluggy-1.0.0
plugins: cov-6.0.0, anyio-4.4.0, asyncio-0.24.0 plugins: sanic-1.9.1, anyio-3.3.4, cov-3.0.0
asyncio: mode=Mode.STRICT, default_loop_scope=None
collected 3 items collected 3 items
giphynavigator/tests.py ... [100%] giphynavigator/tests.py ... [100%]

View File

@ -8,8 +8,6 @@ from sanic import Sanic
from giphynavigator.application import create_app from giphynavigator.application import create_app
from giphynavigator.giphy import GiphyClient from giphynavigator.giphy import GiphyClient
pytestmark = pytest.mark.asyncio
@pytest.fixture @pytest.fixture
def app(): def app():
@ -19,7 +17,12 @@ def app():
app.ctx.container.unwire() app.ctx.container.unwire()
async def test_index(app): @pytest.fixture
def test_client(loop, app, sanic_client):
return loop.run_until_complete(sanic_client(app))
async def test_index(app, test_client):
giphy_client_mock = mock.AsyncMock(spec=GiphyClient) giphy_client_mock = mock.AsyncMock(spec=GiphyClient)
giphy_client_mock.search.return_value = { giphy_client_mock.search.return_value = {
"data": [ "data": [
@ -29,7 +32,7 @@ async def test_index(app):
} }
with app.ctx.container.giphy_client.override(giphy_client_mock): with app.ctx.container.giphy_client.override(giphy_client_mock):
_, response = await app.asgi_client.get( response = await test_client.get(
"/", "/",
params={ params={
"query": "test", "query": "test",
@ -38,7 +41,7 @@ async def test_index(app):
) )
assert response.status_code == 200 assert response.status_code == 200
data = response.json data = response.json()
assert data == { assert data == {
"query": "test", "query": "test",
"limit": 10, "limit": 10,
@ -49,30 +52,30 @@ async def test_index(app):
} }
async def test_index_no_data(app): async def test_index_no_data(app, test_client):
giphy_client_mock = mock.AsyncMock(spec=GiphyClient) giphy_client_mock = mock.AsyncMock(spec=GiphyClient)
giphy_client_mock.search.return_value = { giphy_client_mock.search.return_value = {
"data": [], "data": [],
} }
with app.ctx.container.giphy_client.override(giphy_client_mock): with app.ctx.container.giphy_client.override(giphy_client_mock):
_, response = await app.asgi_client.get("/") response = await test_client.get("/")
assert response.status_code == 200 assert response.status_code == 200
data = response.json data = response.json()
assert data["gifs"] == [] assert data["gifs"] == []
async def test_index_default_params(app): async def test_index_default_params(app, test_client):
giphy_client_mock = mock.AsyncMock(spec=GiphyClient) giphy_client_mock = mock.AsyncMock(spec=GiphyClient)
giphy_client_mock.search.return_value = { giphy_client_mock.search.return_value = {
"data": [], "data": [],
} }
with app.ctx.container.giphy_client.override(giphy_client_mock): with app.ctx.container.giphy_client.override(giphy_client_mock):
_, response = await app.asgi_client.get("/") response = await test_client.get("/")
assert response.status_code == 200 assert response.status_code == 200
data = response.json data = response.json()
assert data["query"] == app.ctx.container.config.default.query() assert data["query"] == app.ctx.container.config.default.query()
assert data["limit"] == app.ctx.container.config.default.limit() assert data["limit"] == app.ctx.container.config.default.limit()

View File

@ -1,6 +1,6 @@
dependency-injector dependency-injector
sanic sanic<=21.6
sanic-testing
aiohttp aiohttp
pyyaml pyyaml
pytest-sanic
pytest-cov pytest-cov

View File

@ -1,39 +0,0 @@
Integration With Starlette-based Frameworks
===========================================
This is a `Starlette <https://www.starlette.io/>`_ +
`Dependency Injector <https://python-dependency-injector.ets-labs.org/>`_ example application
utilizing `lifespan API <https://www.starlette.io/lifespan/>`_.
.. note::
Pretty much `any framework built on top of Starlette <https://www.starlette.io/third-party-packages/#frameworks>`_
supports this feature (`FastAPI <https://fastapi.tiangolo.com/advanced/events/#lifespan>`_,
`Xpresso <https://xpresso-api.dev/latest/tutorial/lifespan/>`_, etc...).
Run
---
Create virtual environment:
.. code-block:: bash
python -m venv env
. env/bin/activate
Install requirements:
.. code-block:: bash
pip install -r requirements.txt
To run the application do:
.. code-block:: bash
python example.py
# or (logging won't be configured):
uvicorn --factory example:container.app
After that visit http://127.0.0.1:8000/ in your browser or use CLI command (``curl``, ``httpie``,
etc).

View File

@ -1,59 +0,0 @@
#!/usr/bin/env python
from logging import basicConfig, getLogger
from dependency_injector.containers import DeclarativeContainer
from dependency_injector.ext.starlette import Lifespan
from dependency_injector.providers import Factory, Resource, Self, Singleton
from starlette.applications import Starlette
from starlette.requests import Request
from starlette.responses import JSONResponse
from starlette.routing import Route
count = 0
def init():
log = getLogger(__name__)
log.info("Inittializing resources")
yield
log.info("Cleaning up resources")
async def homepage(request: Request) -> JSONResponse:
global count
response = JSONResponse({"hello": "world", "count": count})
count += 1
return response
class Container(DeclarativeContainer):
__self__ = Self()
lifespan = Singleton(Lifespan, __self__)
logging = Resource(
basicConfig,
level="DEBUG",
datefmt="%Y-%m-%d %H:%M",
format="%(asctime)s [%(levelname)s] %(name)s: %(message)s",
)
init = Resource(init)
app = Factory(
Starlette,
debug=True,
lifespan=lifespan,
routes=[Route("/", homepage)],
)
container = Container()
if __name__ == "__main__":
import uvicorn
uvicorn.run(
container.app,
factory=True,
# NOTE: `None` prevents uvicorn from configuring logging, which is
# impossible via CLI
log_config=None,
)

View File

@ -1,3 +0,0 @@
dependency-injector
starlette
uvicorn

View File

@ -3,7 +3,7 @@
import os import os
from dependency_injector import containers, providers from dependency_injector import containers, providers
from pydantic_settings import BaseSettings, SettingsConfigDict from pydantic import BaseSettings, Field
# Emulate environment variables # Emulate environment variables
os.environ["AWS_ACCESS_KEY_ID"] = "KEY" os.environ["AWS_ACCESS_KEY_ID"] = "KEY"
@ -11,16 +11,15 @@ os.environ["AWS_SECRET_ACCESS_KEY"] = "SECRET"
class AwsSettings(BaseSettings): class AwsSettings(BaseSettings):
model_config = SettingsConfigDict(env_prefix="aws_")
access_key_id: str access_key_id: str = Field(env="aws_access_key_id")
secret_access_key: str secret_access_key: str = Field(env="aws_secret_access_key")
class Settings(BaseSettings): class Settings(BaseSettings):
aws: AwsSettings = AwsSettings() aws: AwsSettings = AwsSettings()
optional: str = "default_value" optional: str = Field(default="default_value")
class Container(containers.DeclarativeContainer): class Container(containers.DeclarativeContainer):

View File

@ -3,7 +3,7 @@
import os import os
from dependency_injector import containers, providers from dependency_injector import containers, providers
from pydantic_settings import BaseSettings, SettingsConfigDict from pydantic import BaseSettings, Field
# Emulate environment variables # Emulate environment variables
os.environ["AWS_ACCESS_KEY_ID"] = "KEY" os.environ["AWS_ACCESS_KEY_ID"] = "KEY"
@ -11,16 +11,15 @@ os.environ["AWS_SECRET_ACCESS_KEY"] = "SECRET"
class AwsSettings(BaseSettings): class AwsSettings(BaseSettings):
model_config = SettingsConfigDict(env_prefix="aws_")
access_key_id: str access_key_id: str = Field(env="aws_access_key_id")
secret_access_key: str secret_access_key: str = Field(env="aws_secret_access_key")
class Settings(BaseSettings): class Settings(BaseSettings):
aws: AwsSettings = AwsSettings() aws: AwsSettings = AwsSettings()
optional: str = "default_value" optional: str = Field(default="default_value")
class Container(containers.DeclarativeContainer): class Container(containers.DeclarativeContainer):

View File

@ -2,10 +2,10 @@
from dependency_injector import containers, providers from dependency_injector import containers, providers
from dependency_injector.wiring import Provide, inject from dependency_injector.wiring import Provide, inject
from typing import Annotated
class Service: ... class Service:
...
class Container(containers.DeclarativeContainer): class Container(containers.DeclarativeContainer):
@ -13,16 +13,9 @@ class Container(containers.DeclarativeContainer):
service = providers.Factory(Service) service = providers.Factory(Service)
# You can place marker on parameter default value
@inject @inject
def main(service: Service = Provide[Container.service]) -> None: ... def main(service: Service = Provide[Container.service]) -> None:
...
# Also, you can place marker with typing.Annotated
@inject
def main_with_annotated(
service: Annotated[Service, Provide[Container.service]]
) -> None: ...
if __name__ == "__main__": if __name__ == "__main__":

View File

@ -1,101 +0,0 @@
[build-system]
requires = ["setuptools", "Cython"]
build-backend = "setuptools.build_meta"
[project]
name = "dependency-injector"
authors = [
{name = "Roman Mogylatov", email = "rmogilatov@gmail.com"},
]
maintainers = [
{name = "Roman Mogylatov", email = "rmogilatov@gmail.com"},
]
description = "Dependency injection framework for Python"
readme = {file = "README.rst", content-type = "text/x-rst"}
license = {file = "LICENSE.rst", content-type = "text/x-rst"}
requires-python = ">=3.7"
keywords = [
"Dependency injection",
"DI",
"Inversion of Control",
"IoC",
"Factory",
"Singleton",
"Design patterns",
"Flask",
]
classifiers = [
"Development Status :: 5 - Production/Stable",
"Intended Audience :: Developers",
"License :: OSI Approved :: BSD License",
"Operating System :: OS Independent",
"Programming Language :: Python",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.7",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12",
"Programming Language :: Python :: 3.13",
"Programming Language :: Python :: Implementation :: CPython",
"Programming Language :: Python :: Implementation :: PyPy",
"Framework :: AsyncIO",
"Framework :: Bottle",
"Framework :: Django",
"Framework :: Flask",
"Framework :: Pylons",
"Framework :: Pyramid",
"Framework :: Pytest",
"Framework :: TurboGears",
"Topic :: Software Development",
"Topic :: Software Development :: Libraries",
"Topic :: Software Development :: Libraries :: Python Modules",
]
dynamic = ["version"]
[project.optional-dependencies]
yaml = ["pyyaml"]
pydantic = ["pydantic"]
pydantic2 = ["pydantic-settings"]
flask = ["flask"]
aiohttp = ["aiohttp"]
[project.urls]
Homepage = "https://github.com/ets-labs/python-dependency-injector"
Documentation = "https://python-dependency-injector.ets-labs.org/"
Download = "https://pypi.python.org/pypi/dependency_injector"
[tool.setuptools]
package-dir = {"" = "src"}
[tool.setuptools.packages.find]
where = ["src"]
[tool.setuptools.package-data]
dependency_injector = ["*.pxd", "*.pyi", "py.typed"]
[tool.setuptools.dynamic]
version = {attr = "dependency_injector.__version__"}
[tool.coverage.run]
branch = false
relative_files = true
source_pkgs = ["dependency_injector"]
plugins = ["Cython.Coverage"]
[tool.coverage.html]
directory = "reports/unittests/"
[tool.coverage.report]
show_missing = true
[tool.isort]
profile = "black"
[tool.pylint.main]
ignore = ["tests"]
[tool.pylint.design]
min-public-methods = 0
max-public-methods = 30

View File

@ -5,7 +5,6 @@ pytest-asyncio
tox tox
coverage coverage
flake8 flake8
flake8-pyproject
pydocstyle pydocstyle
sphinx_autobuild sphinx_autobuild
pip pip
@ -18,6 +17,5 @@ numpy
scipy scipy
boto3 boto3
mypy_boto3_s3 mypy_boto3_s3
typing_extensions
-r requirements-ext.txt -r requirements-ext.txt

View File

@ -1,3 +1,3 @@
flask flask==2.1.3
werkzeug werkzeug==2.2.2
aiohttp aiohttp

1
requirements.txt Normal file
View File

@ -0,0 +1 @@
six>=1.7.0,<=1.16.0

View File

@ -2,7 +2,6 @@
max_line_length = 120 max_line_length = 120
max_complexity = 10 max_complexity = 10
exclude = types.py exclude = types.py
extend-ignore = E203,E701
per-file-ignores = per-file-ignores =
examples/demo/*: F841 examples/demo/*: F841
examples/containers/traverse.py: E501 examples/containers/traverse.py: E501

152
setup.py
View File

@ -1,42 +1,128 @@
"""`Dependency injector` setup script.""" """`Dependency injector` setup script."""
import os import os
import re
import sys
from Cython.Build import cythonize from setuptools import setup, Extension
from Cython.Compiler import Options
from setuptools import Extension, setup
debug = os.environ.get("DEPENDENCY_INJECTOR_DEBUG_MODE") == "1"
defined_macros = [] def _open(filename):
compiler_directives = { if sys.version_info[0] == 2:
"language_level": 3, return open(filename)
"profile": debug, return open(filename, encoding="utf-8")
"linetrace": debug,
}
Options.annotate = debug # Defining setup variables:
defined_macros = dict()
defined_macros["CYTHON_CLINE_IN_TRACEBACK"] = 0
# Getting description:
with _open("README.rst") as readme_file:
description = readme_file.read()
# Getting requirements:
with _open("requirements.txt") as requirements_file:
requirements = requirements_file.readlines()
# Getting version:
with _open("src/dependency_injector/__init__.py") as init_file:
version = re.search("__version__ = \"(.*?)\"", init_file.read()).group(1)
# Adding debug options: # Adding debug options:
if debug: if os.environ.get("DEPENDENCY_INJECTOR_DEBUG_MODE") == "1":
defined_macros.extend( defined_macros["CYTHON_TRACE"] = 1
[ defined_macros["CYTHON_TRACE_NOGIL"] = 1
("CYTHON_TRACE", "1"), defined_macros["CYTHON_CLINE_IN_TRACEBACK"] = 1
("CYTHON_TRACE_NOGIL", "1"),
("CYTHON_CLINE_IN_TRACEBACK", "1"),
]
)
setup( setup(name="dependency-injector",
ext_modules=cythonize( version=version,
[ description="Dependency injection framework for Python",
Extension( long_description=description,
"*", author="Roman Mogylatov",
["src/**/*.pyx"], author_email="rmogilatov@gmail.com",
define_macros=defined_macros, maintainer="Roman Mogylatov",
), maintainer_email="rmogilatov@gmail.com",
], url="https://github.com/ets-labs/python-dependency-injector",
annotate=debug, download_url="https://pypi.python.org/pypi/dependency_injector",
show_all_warnings=True, packages=[
compiler_directives=compiler_directives, "dependency_injector",
), "dependency_injector.ext",
) ],
package_dir={
"": "src",
},
package_data={
"dependency_injector": ["*.pxd", "*.pyi", "py.typed"],
},
ext_modules=[
Extension("dependency_injector.containers",
["src/dependency_injector/containers.c"],
define_macros=list(defined_macros.items()),
extra_compile_args=["-O2"]),
Extension("dependency_injector.providers",
["src/dependency_injector/providers.c"],
define_macros=list(defined_macros.items()),
extra_compile_args=["-O2"]),
Extension("dependency_injector._cwiring",
["src/dependency_injector/_cwiring.c"],
define_macros=list(defined_macros.items()),
extra_compile_args=["-O2"]),
],
install_requires=requirements,
extras_require={
"yaml": [
"pyyaml",
],
"pydantic": [
"pydantic",
],
"flask": [
"flask",
],
"aiohttp": [
"aiohttp",
],
},
zip_safe=True,
license="BSD New",
platforms=["any"],
keywords=[
"Dependency injection",
"DI",
"Inversion of Control",
"IoC",
"Factory",
"Singleton",
"Design patterns",
"Flask",
],
classifiers=[
"Development Status :: 5 - Production/Stable",
"Intended Audience :: Developers",
"License :: OSI Approved :: BSD License",
"Operating System :: OS Independent",
"Programming Language :: Python",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.7",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12",
"Programming Language :: Python :: 3.13",
"Programming Language :: Python :: Implementation :: CPython",
"Programming Language :: Python :: Implementation :: PyPy",
"Framework :: AsyncIO",
"Framework :: Bottle",
"Framework :: Django",
"Framework :: Flask",
"Framework :: Pylons",
"Framework :: Pyramid",
"Framework :: Pytest",
"Framework :: TurboGears",
"Topic :: Software Development",
"Topic :: Software Development :: Libraries",
"Topic :: Software Development :: Libraries :: Python Modules",
])

View File

@ -1,6 +1,6 @@
"""Top-level package.""" """Top-level package."""
__version__ = "4.46.0" __version__ = "4.43.0"
"""Version number. """Version number.
:type: str :type: str

File diff suppressed because it is too large Load Diff

View File

@ -2,39 +2,44 @@
import asyncio import asyncio
import collections.abc import collections.abc
import functools
import inspect import inspect
import types import types
from .wiring import _Marker from . import providers
from .wiring import _Marker, PatchedCallable
from .providers cimport Provider, Resource from .providers cimport Provider
def _sync_inject(object fn, tuple args, dict kwargs, dict injections, dict closings, /): def _get_sync_patched(fn, patched: PatchedCallable):
cdef object result @functools.wraps(fn)
cdef dict to_inject def _patched(*args, **kwargs):
cdef object arg_key cdef object result
cdef Provider provider cdef dict to_inject
cdef object arg_key
cdef Provider provider
to_inject = kwargs.copy() to_inject = kwargs.copy()
for arg_key, provider in injections.items(): for arg_key, provider in patched.injections.items():
if arg_key not in kwargs or isinstance(kwargs[arg_key], _Marker): if arg_key not in kwargs or isinstance(kwargs[arg_key], _Marker):
to_inject[arg_key] = provider() to_inject[arg_key] = provider()
result = fn(*args, **to_inject) result = fn(*args, **to_inject)
if closings: if patched.closing:
for arg_key, provider in closings.items(): for arg_key, provider in patched.closing.items():
if arg_key in kwargs and not isinstance(kwargs[arg_key], _Marker): if arg_key in kwargs and not isinstance(kwargs[arg_key], _Marker):
continue continue
if not isinstance(provider, Resource): if not isinstance(provider, providers.Resource):
continue continue
provider.shutdown() provider.shutdown()
return result return result
return _patched
async def _async_inject(object fn, tuple args, dict kwargs, dict injections, dict closings, /): async def _async_inject(object fn, tuple args, dict kwargs, dict injections, dict closings):
cdef object result cdef object result
cdef dict to_inject cdef dict to_inject
cdef list to_inject_await = [] cdef list to_inject_await = []
@ -64,7 +69,7 @@ async def _async_inject(object fn, tuple args, dict kwargs, dict injections, dic
for arg_key, provider in closings.items(): for arg_key, provider in closings.items():
if arg_key in kwargs and isinstance(kwargs[arg_key], _Marker): if arg_key in kwargs and isinstance(kwargs[arg_key], _Marker):
continue continue
if not isinstance(provider, Resource): if not isinstance(provider, providers.Resource):
continue continue
shutdown = provider.shutdown() shutdown = provider.shutdown()
if _isawaitable(shutdown): if _isawaitable(shutdown):

File diff suppressed because it is too large Load Diff

View File

@ -19,24 +19,21 @@ from typing import (
from .providers import Provider, Self, ProviderParent from .providers import Provider, Self, ProviderParent
C_Base = TypeVar("C_Base", bound="Container") C_Base = TypeVar("C_Base", bound="Container")
C = TypeVar("C", bound="DeclarativeContainer") C = TypeVar("C", bound="DeclarativeContainer")
C_Overriding = TypeVar("C_Overriding", bound="DeclarativeContainer") C_Overriding = TypeVar("C_Overriding", bound="DeclarativeContainer")
T = TypeVar("T") T = TypeVar("T")
TT = TypeVar("TT") TT = TypeVar("TT")
class WiringConfiguration: class WiringConfiguration:
modules: List[Any] modules: List[Any]
packages: List[Any] packages: List[Any]
from_package: Optional[str] from_package: Optional[str]
auto_wire: bool auto_wire: bool
def __init__( def __init__(self, modules: Optional[Iterable[Any]] = None, packages: Optional[Iterable[Any]] = None, from_package: Optional[str] = None, auto_wire: bool = True) -> None: ...
self,
modules: Optional[Iterable[Any]] = None,
packages: Optional[Iterable[Any]] = None,
from_package: Optional[str] = None,
auto_wire: bool = True,
) -> None: ...
class Container: class Container:
provider_type: Type[Provider] = Provider provider_type: Type[Provider] = Provider
@ -54,18 +51,11 @@ class Container:
def set_providers(self, **providers: Provider): ... def set_providers(self, **providers: Provider): ...
def set_provider(self, name: str, provider: Provider) -> None: ... def set_provider(self, name: str, provider: Provider) -> None: ...
def override(self, overriding: Union[Container, Type[Container]]) -> None: ... def override(self, overriding: Union[Container, Type[Container]]) -> None: ...
def override_providers( def override_providers(self, **overriding_providers: Union[Provider, Any]) -> ProvidersOverridingContext[C_Base]: ...
self, **overriding_providers: Union[Provider, Any]
) -> ProvidersOverridingContext[C_Base]: ...
def reset_last_overriding(self) -> None: ... def reset_last_overriding(self) -> None: ...
def reset_override(self) -> None: ... def reset_override(self) -> None: ...
def is_auto_wiring_enabled(self) -> bool: ... def is_auto_wiring_enabled(self) -> bool: ...
def wire( def wire(self, modules: Optional[Iterable[Any]] = None, packages: Optional[Iterable[Any]] = None, from_package: Optional[str] = None) -> None: ...
self,
modules: Optional[Iterable[Any]] = None,
packages: Optional[Iterable[Any]] = None,
from_package: Optional[str] = None,
) -> None: ...
def unwire(self) -> None: ... def unwire(self) -> None: ...
def init_resources(self) -> Optional[Awaitable]: ... def init_resources(self) -> Optional[Awaitable]: ...
def shutdown_resources(self) -> Optional[Awaitable]: ... def shutdown_resources(self) -> Optional[Awaitable]: ...
@ -74,9 +64,7 @@ class Container:
def reset_singletons(self) -> SingletonResetContext[C_Base]: ... def reset_singletons(self) -> SingletonResetContext[C_Base]: ...
def check_dependencies(self) -> None: ... def check_dependencies(self) -> None: ...
def from_schema(self, schema: Dict[Any, Any]) -> None: ... def from_schema(self, schema: Dict[Any, Any]) -> None: ...
def from_yaml_schema( def from_yaml_schema(self, filepath: Union[Path, str], loader: Optional[Any]=None) -> None: ...
self, filepath: Union[Path, str], loader: Optional[Any] = None
) -> None: ...
def from_json_schema(self, filepath: Union[Path, str]) -> None: ... def from_json_schema(self, filepath: Union[Path, str]) -> None: ...
@overload @overload
def resolve_provider_name(self, provider: Provider) -> str: ... def resolve_provider_name(self, provider: Provider) -> str: ...
@ -94,8 +82,10 @@ class Container:
@overload @overload
def traverse(cls, types: Optional[Iterable[Type[TT]]] = None) -> Iterator[TT]: ... def traverse(cls, types: Optional[Iterable[Type[TT]]] = None) -> Iterator[TT]: ...
class DynamicContainer(Container): ... class DynamicContainer(Container): ...
class DeclarativeContainer(Container): class DeclarativeContainer(Container):
cls_providers: ClassVar[Dict[str, Provider]] cls_providers: ClassVar[Dict[str, Provider]]
inherited_providers: ClassVar[Dict[str, Provider]] inherited_providers: ClassVar[Dict[str, Provider]]
@ -103,28 +93,29 @@ class DeclarativeContainer(Container):
@classmethod @classmethod
def override(cls, overriding: Union[Container, Type[Container]]) -> None: ... def override(cls, overriding: Union[Container, Type[Container]]) -> None: ...
@classmethod @classmethod
def override_providers( def override_providers(cls, **overriding_providers: Union[Provider, Any]) -> ProvidersOverridingContext[C_Base]: ...
cls, **overriding_providers: Union[Provider, Any]
) -> ProvidersOverridingContext[C_Base]: ...
@classmethod @classmethod
def reset_last_overriding(cls) -> None: ... def reset_last_overriding(cls) -> None: ...
@classmethod @classmethod
def reset_override(cls) -> None: ... def reset_override(cls) -> None: ...
class ProvidersOverridingContext(Generic[T]): class ProvidersOverridingContext(Generic[T]):
def __init__( def __init__(self, container: T, overridden_providers: Iterable[Union[Provider, Any]]) -> None: ...
self, container: T, overridden_providers: Iterable[Union[Provider, Any]]
) -> None: ...
def __enter__(self) -> T: ... def __enter__(self) -> T: ...
def __exit__(self, *_: Any) -> None: ... def __exit__(self, *_: Any) -> None: ...
class SingletonResetContext(Generic[T]): class SingletonResetContext(Generic[T]):
def __init__(self, container: T): ... def __init__(self, container: T): ...
def __enter__(self) -> T: ... def __enter__(self) -> T: ...
def __exit__(self, *_: Any) -> None: ... def __exit__(self, *_: Any) -> None: ...
def override(
container: Type[C], def override(container: Type[C]) -> _Callable[[Type[C_Overriding]], Type[C_Overriding]]: ...
) -> _Callable[[Type[C_Overriding]], Type[C_Overriding]]: ...
def copy(container: Type[C]) -> _Callable[[Type[C_Overriding]], Type[C_Overriding]]: ... def copy(container: Type[C]) -> _Callable[[Type[C_Overriding]], Type[C_Overriding]]: ...
def is_container(instance: Any) -> bool: ... def is_container(instance: Any) -> bool: ...

View File

@ -1,20 +1,45 @@
"""Containers module.""" """Containers module."""
import asyncio
import contextlib import contextlib
import copy as copy_module import copy as copy_module
import json import json
import sys
import importlib import importlib
import inspect import inspect
import warnings
try:
import asyncio
except ImportError:
asyncio = None
try: try:
import yaml import yaml
except ImportError: except ImportError:
yaml = None yaml = None
import six
from . import providers, errors from . import providers, errors
from .providers cimport __is_future_or_coroutine from .providers cimport __is_future_or_coroutine
from .wiring import wire, unwire
if sys.version_info[:2] >= (3, 6):
from .wiring import wire, unwire
else:
def wire(*args, **kwargs):
raise NotImplementedError("Wiring requires Python 3.6 or above")
def unwire(*args, **kwargs):
raise NotImplementedError("Wiring requires Python 3.6 or above")
if sys.version_info[:2] == (3, 5):
warnings.warn(
"Dependency Injector will drop support of Python 3.5 after Jan 1st of 2022. "
"This does not mean that there will be any immediate breaking changes, "
"but tests will no longer be executed on Python 3.5, and bugs will not be addressed.",
category=DeprecationWarning,
)
class WiringConfiguration: class WiringConfiguration:
@ -30,7 +55,7 @@ class WiringConfiguration:
return self.__class__(self.modules, self.packages, self.from_package, self.auto_wire) return self.__class__(self.modules, self.packages, self.from_package, self.auto_wire)
class Container: class Container(object):
"""Abstract container.""" """Abstract container."""
@ -176,7 +201,7 @@ class DynamicContainer(Container):
:rtype: None :rtype: None
""" """
for name, provider in providers.items(): for name, provider in six.iteritems(providers):
setattr(self, name, provider) setattr(self, name, provider)
def set_provider(self, name, provider): def set_provider(self, name, provider):
@ -209,7 +234,7 @@ class DynamicContainer(Container):
self.overridden += (overriding,) self.overridden += (overriding,)
for name, provider in overriding.providers.items(): for name, provider in six.iteritems(overriding.providers):
try: try:
getattr(self, name).override(provider) getattr(self, name).override(provider)
except AttributeError: except AttributeError:
@ -225,7 +250,7 @@ class DynamicContainer(Container):
:rtype: None :rtype: None
""" """
overridden_providers = [] overridden_providers = []
for name, overriding_provider in overriding_providers.items(): for name, overriding_provider in six.iteritems(overriding_providers):
container_provider = getattr(self, name) container_provider = getattr(self, name)
container_provider.override(overriding_provider) container_provider.override(overriding_provider)
overridden_providers.append(container_provider) overridden_providers.append(container_provider)
@ -241,7 +266,7 @@ class DynamicContainer(Container):
self.overridden = self.overridden[:-1] self.overridden = self.overridden[:-1]
for provider in self.providers.values(): for provider in six.itervalues(self.providers):
provider.reset_last_overriding() provider.reset_last_overriding()
def reset_override(self): def reset_override(self):
@ -251,7 +276,7 @@ class DynamicContainer(Container):
""" """
self.overridden = tuple() self.overridden = tuple()
for provider in self.providers.values(): for provider in six.itervalues(self.providers):
provider.reset_override() provider.reset_override()
def is_auto_wiring_enabled(self): def is_auto_wiring_enabled(self):
@ -470,13 +495,13 @@ class DeclarativeContainerMetaClass(type):
containers = { containers = {
name: container name: container
for name, container in attributes.items() for name, container in six.iteritems(attributes)
if is_container(container) if is_container(container)
} }
cls_providers = { cls_providers = {
name: provider name: provider
for name, provider in attributes.items() for name, provider in six.iteritems(attributes)
if isinstance(provider, providers.Provider) and not isinstance(provider, providers.Self) if isinstance(provider, providers.Provider) and not isinstance(provider, providers.Self)
} }
@ -484,7 +509,7 @@ class DeclarativeContainerMetaClass(type):
name: provider name: provider
for base in bases for base in bases
if is_container(base) and base is not DynamicContainer if is_container(base) and base is not DynamicContainer
for name, provider in base.providers.items() for name, provider in six.iteritems(base.providers)
} }
all_providers = {} all_providers = {}
@ -511,10 +536,10 @@ class DeclarativeContainerMetaClass(type):
self.set_container(cls) self.set_container(cls)
cls.__self__ = self cls.__self__ = self
for provider in cls.providers.values(): for provider in six.itervalues(cls.providers):
_check_provider_type(cls, provider) _check_provider_type(cls, provider)
for provider in cls.cls_providers.values(): for provider in six.itervalues(cls.cls_providers):
if isinstance(provider, providers.CHILD_PROVIDERS): if isinstance(provider, providers.CHILD_PROVIDERS):
provider.assign_parent(cls) provider.assign_parent(cls)
@ -616,7 +641,8 @@ class DeclarativeContainerMetaClass(type):
return self return self
class DeclarativeContainer(Container, metaclass=DeclarativeContainerMetaClass): @six.add_metaclass(DeclarativeContainerMetaClass)
class DeclarativeContainer(Container):
"""Declarative inversion of control container. """Declarative inversion of control container.
.. code-block:: python .. code-block:: python
@ -741,7 +767,7 @@ class DeclarativeContainer(Container, metaclass=DeclarativeContainerMetaClass):
cls.overridden += (overriding,) cls.overridden += (overriding,)
for name, provider in overriding.cls_providers.items(): for name, provider in six.iteritems(overriding.cls_providers):
try: try:
getattr(cls, name).override(provider) getattr(cls, name).override(provider)
except AttributeError: except AttributeError:
@ -758,7 +784,7 @@ class DeclarativeContainer(Container, metaclass=DeclarativeContainerMetaClass):
cls.overridden = cls.overridden[:-1] cls.overridden = cls.overridden[:-1]
for provider in cls.providers.values(): for provider in six.itervalues(cls.providers):
provider.reset_last_overriding() provider.reset_last_overriding()
@classmethod @classmethod
@ -769,7 +795,7 @@ class DeclarativeContainer(Container, metaclass=DeclarativeContainerMetaClass):
""" """
cls.overridden = tuple() cls.overridden = tuple()
for provider in cls.providers.values(): for provider in six.itervalues(cls.providers):
provider.reset_override() provider.reset_override()
@ -832,7 +858,7 @@ def copy(object base_container):
""" """
def _get_memo_for_matching_names(new_providers, base_providers): def _get_memo_for_matching_names(new_providers, base_providers):
memo = {} memo = {}
for new_provider_name, new_provider in new_providers.items(): for new_provider_name, new_provider in six.iteritems(new_providers):
if new_provider_name not in base_providers: if new_provider_name not in base_providers:
continue continue
source_provider = base_providers[new_provider_name] source_provider = base_providers[new_provider_name]
@ -851,7 +877,7 @@ def copy(object base_container):
new_providers.update(providers.deepcopy(base_container.providers, memo)) new_providers.update(providers.deepcopy(base_container.providers, memo))
new_providers.update(providers.deepcopy(new_container.cls_providers, memo)) new_providers.update(providers.deepcopy(new_container.cls_providers, memo))
for name, provider in new_providers.items(): for name, provider in six.iteritems(new_providers):
setattr(new_container, name, provider) setattr(new_container, name, provider)
return new_container return new_container

View File

@ -10,24 +10,3 @@ class Error(Exception):
class NoSuchProviderError(Error, AttributeError): class NoSuchProviderError(Error, AttributeError):
"""Error that is raised when provider lookup is failed.""" """Error that is raised when provider lookup is failed."""
class NonCopyableArgumentError(Error):
"""Error that is raised when provider argument is not deep-copyable."""
index: int
keyword: str
provider: object
def __init__(self, provider: object, index: int = -1, keyword: str = "") -> None:
self.provider = provider
self.index = index
self.keyword = keyword
def __str__(self) -> str:
s = (
f"keyword argument {self.keyword}"
if self.keyword
else f"argument at index {self.index}"
)
return f"Couldn't copy {s} for provider {self.provider!r}"

View File

@ -38,11 +38,9 @@ class View(providers.Callable):
def as_view(self): def as_view(self):
"""Return aiohttp view function.""" """Return aiohttp view function."""
@functools.wraps(self.provides) @functools.wraps(self.provides)
async def _view(request, *args, **kwargs): async def _view(request, *args, **kwargs):
return await self.__call__(request, *args, **kwargs) return await self.__call__(request, *args, **kwargs)
return _view return _view
@ -51,8 +49,6 @@ class ClassBasedView(providers.Factory):
def as_view(self): def as_view(self):
"""Return aiohttp view function.""" """Return aiohttp view function."""
async def _view(request, *args, **kwargs): async def _view(request, *args, **kwargs):
return await self.__call__(request, *args, **kwargs) return await self.__call__(request, *args, **kwargs)
return _view return _view

View File

@ -2,13 +2,22 @@ from typing import Awaitable as _Awaitable
from dependency_injector import providers from dependency_injector import providers
class Application(providers.Singleton): ... class Application(providers.Singleton): ...
class Extension(providers.Singleton): ... class Extension(providers.Singleton): ...
class Middleware(providers.DelegatedCallable): ... class Middleware(providers.DelegatedCallable): ...
class MiddlewareFactory(providers.Factory): ... class MiddlewareFactory(providers.Factory): ...
class View(providers.Callable): class View(providers.Callable):
def as_view(self) -> _Awaitable: ... def as_view(self) -> _Awaitable: ...
class ClassBasedView(providers.Factory): class ClassBasedView(providers.Factory):
def as_view(self) -> _Awaitable: ... def as_view(self) -> _Awaitable: ...

View File

@ -45,7 +45,6 @@ class ClassBasedView(providers.Factory):
def as_view(provider, name=None): def as_view(provider, name=None):
"""Transform class-based view provider to view function.""" """Transform class-based view provider to view function."""
if isinstance(provider, providers.Factory): if isinstance(provider, providers.Factory):
def view(*args, **kwargs): def view(*args, **kwargs):
self = provider() self = provider()
return self.dispatch_request(*args, **kwargs) return self.dispatch_request(*args, **kwargs)
@ -53,13 +52,12 @@ def as_view(provider, name=None):
assert name, 'Argument "endpoint" is required for class-based views' assert name, 'Argument "endpoint" is required for class-based views'
view.__name__ = name view.__name__ = name
elif isinstance(provider, providers.Callable): elif isinstance(provider, providers.Callable):
def view(*args, **kwargs): def view(*args, **kwargs):
return provider(*args, **kwargs) return provider(*args, **kwargs)
view.__name__ = provider.provides.__name__ view.__name__ = provider.provides.__name__
else: else:
raise errors.Error("Undefined provider type") raise errors.Error('Undefined provider type')
view.__doc__ = provider.provides.__doc__ view.__doc__ = provider.provides.__doc__
view.__module__ = provider.provides.__module__ view.__module__ = provider.provides.__module__
@ -67,14 +65,14 @@ def as_view(provider, name=None):
if isinstance(provider.provides, type): if isinstance(provider.provides, type):
view.view_class = provider.provides view.view_class = provider.provides
if hasattr(provider.provides, "decorators"): if hasattr(provider.provides, 'decorators'):
for decorator in provider.provides.decorators: for decorator in provider.provides.decorators:
view = decorator(view) view = decorator(view)
if hasattr(provider.provides, "methods"): if hasattr(provider.provides, 'methods'):
view.methods = provider.provides.methods view.methods = provider.provides.methods
if hasattr(provider.provides, "provide_automatic_options"): if hasattr(provider.provides, 'provide_automatic_options'):
view.provide_automatic_options = provider.provides.provide_automatic_options view.provide_automatic_options = provider.provides.provide_automatic_options
return view return view

View File

@ -3,17 +3,22 @@ from typing import Union, Optional, Callable as _Callable, Any
from flask import request as flask_request from flask import request as flask_request
from dependency_injector import providers from dependency_injector import providers
request: providers.Object[flask_request] request: providers.Object[flask_request]
class Application(providers.Singleton): ... class Application(providers.Singleton): ...
class Extension(providers.Singleton): ... class Extension(providers.Singleton): ...
class View(providers.Callable): class View(providers.Callable):
def as_view(self) -> _Callable[..., Any]: ... def as_view(self) -> _Callable[..., Any]: ...
class ClassBasedView(providers.Factory): class ClassBasedView(providers.Factory):
def as_view(self, name: str) -> _Callable[..., Any]: ... def as_view(self, name: str) -> _Callable[..., Any]: ...
def as_view(
provider: Union[View, ClassBasedView], name: Optional[str] = None def as_view(provider: Union[View, ClassBasedView], name: Optional[str] = None) -> _Callable[..., Any]: ...
) -> _Callable[..., Any]: ...

View File

@ -1,52 +0,0 @@
import sys
from typing import Any
if sys.version_info >= (3, 11): # pragma: no cover
from typing import Self
else: # pragma: no cover
from typing_extensions import Self
from dependency_injector.containers import Container
class Lifespan:
"""A starlette lifespan handler performing container resource initialization and shutdown.
See https://www.starlette.io/lifespan/ for details.
Usage:
.. code-block:: python
from dependency_injector.containers import DeclarativeContainer
from dependency_injector.ext.starlette import Lifespan
from dependency_injector.providers import Factory, Self, Singleton
from starlette.applications import Starlette
class Container(DeclarativeContainer):
__self__ = Self()
lifespan = Singleton(Lifespan, __self__)
app = Factory(Starlette, lifespan=lifespan)
:param container: container instance
"""
container: Container
def __init__(self, container: Container) -> None:
self.container = container
def __call__(self, app: Any) -> Self:
return self
async def __aenter__(self) -> None:
result = self.container.init_resources()
if result is not None:
await result
async def __aexit__(self, *exc_info: Any) -> None:
result = self.container.shutdown_resources()
if result is not None:
await result

File diff suppressed because it is too large Load Diff

View File

@ -1,6 +1,10 @@
"""Providers module.""" """Providers module."""
import asyncio try:
import asyncio
except ImportError:
asyncio = None
import functools import functools
cimport cython cimport cython
@ -15,7 +19,7 @@ cdef tuple __COROUTINE_TYPES
# Base providers # Base providers
cdef class Provider: cdef class Provider(object):
cdef tuple _overridden cdef tuple _overridden
cdef Provider _last_overriding cdef Provider _last_overriding
cdef tuple _overrides cdef tuple _overrides
@ -287,7 +291,7 @@ cdef class MethodCaller(Provider):
# Injections # Injections
cdef class Injection: cdef class Injection(object):
cdef object _value cdef object _value
cdef int _is_provider cdef int _is_provider
cdef int _is_delegated cdef int _is_delegated
@ -309,12 +313,12 @@ cpdef tuple parse_named_injections(dict kwargs)
# Utils # Utils
cdef class OverridingContext: cdef class OverridingContext(object):
cdef Provider _overridden cdef Provider _overridden
cdef Provider _overriding cdef Provider _overriding
cdef class BaseSingletonResetContext: cdef class BaseSingletonResetContext(object):
cdef object _singleton cdef object _singleton

View File

@ -33,6 +33,7 @@ except ImportError:
from . import resources from . import resources
Injection = Any Injection = Any
ProviderParent = Union["Provider", Any] ProviderParent = Union["Provider", Any]
T = TypeVar("T") T = TypeVar("T")
@ -40,13 +41,16 @@ TT = TypeVar("TT")
P = TypeVar("P", bound="Provider") P = TypeVar("P", bound="Provider")
BS = TypeVar("BS", bound="BaseSingleton") BS = TypeVar("BS", bound="BaseSingleton")
class Provider(Generic[T]): class Provider(Generic[T]):
def __init__(self) -> None: ... def __init__(self) -> None: ...
@overload @overload
def __call__(self, *args: Injection, **kwargs: Injection) -> T: ... def __call__(self, *args: Injection, **kwargs: Injection) -> T: ...
@overload @overload
def __call__(self, *args: Injection, **kwargs: Injection) -> Awaitable[T]: ... def __call__(self, *args: Injection, **kwargs: Injection) -> Awaitable[T]: ...
def async_(self, *args: Injection, **kwargs: Injection) -> Awaitable[T]: ... def async_(self, *args: Injection, **kwargs: Injection) -> Awaitable[T]: ...
def __deepcopy__(self, memo: Optional[_Dict[Any, Any]]) -> Provider: ... def __deepcopy__(self, memo: Optional[_Dict[Any, Any]]) -> Provider: ...
def __str__(self) -> str: ... def __str__(self) -> str: ...
def __repr__(self) -> str: ... def __repr__(self) -> str: ...
@ -63,9 +67,9 @@ class Provider(Generic[T]):
def unregister_overrides(self, provider: Union[Provider, Any]) -> None: ... def unregister_overrides(self, provider: Union[Provider, Any]) -> None: ...
def delegate(self) -> Provider: ... def delegate(self) -> Provider: ...
@property @property
def provider(self) -> Provider[T]: ... def provider(self) -> Provider: ...
@property @property
def provided(self) -> ProvidedInstance[T]: ... def provided(self) -> ProvidedInstance: ...
def enable_async_mode(self) -> None: ... def enable_async_mode(self) -> None: ...
def disable_async_mode(self) -> None: ... def disable_async_mode(self) -> None: ...
def reset_async_mode(self) -> None: ... def reset_async_mode(self) -> None: ...
@ -74,12 +78,9 @@ class Provider(Generic[T]):
def is_async_mode_undefined(self) -> bool: ... def is_async_mode_undefined(self) -> bool: ...
@property @property
def related(self) -> _Iterator[Provider]: ... def related(self) -> _Iterator[Provider]: ...
def traverse( def traverse(self, types: Optional[_Iterable[Type[TT]]] = None) -> _Iterator[TT]: ...
self, types: Optional[_Iterable[Type[TT]]] = None def _copy_overridings(self, copied: Provider, memo: Optional[_Dict[Any, Any]]) -> None: ...
) -> _Iterator[TT]: ...
def _copy_overridings(
self, copied: Provider, memo: Optional[_Dict[Any, Any]]
) -> None: ...
class Object(Provider[T]): class Object(Provider[T]):
def __init__(self, provides: Optional[T] = None) -> None: ... def __init__(self, provides: Optional[T] = None) -> None: ...
@ -87,6 +88,7 @@ class Object(Provider[T]):
def provides(self) -> Optional[T]: ... def provides(self) -> Optional[T]: ...
def set_provides(self, provides: Optional[T]) -> Object: ... def set_provides(self, provides: Optional[T]) -> Object: ...
class Self(Provider[T]): class Self(Provider[T]):
def __init__(self, container: Optional[T] = None) -> None: ... def __init__(self, container: Optional[T] = None) -> None: ...
def set_container(self, container: T) -> None: ... def set_container(self, container: T) -> None: ...
@ -94,51 +96,41 @@ class Self(Provider[T]):
@property @property
def alt_names(self) -> Tuple[Any]: ... def alt_names(self) -> Tuple[Any]: ...
class Delegate(Provider[Provider]): class Delegate(Provider[Provider]):
def __init__(self, provides: Optional[Provider] = None) -> None: ... def __init__(self, provides: Optional[Provider] = None) -> None: ...
@property @property
def provides(self) -> Optional[Provider]: ... def provides(self) -> Optional[Provider]: ...
def set_provides(self, provides: Optional[Provider]) -> Delegate: ... def set_provides(self, provides: Optional[Provider]) -> Delegate: ...
class Aggregate(Provider[T]): class Aggregate(Provider[T]):
def __init__( def __init__(self, provider_dict: Optional[_Dict[Any, Provider[T]]] = None, **provider_kwargs: Provider[T]): ...
self,
provider_dict: Optional[_Dict[Any, Provider[T]]] = None,
**provider_kwargs: Provider[T],
): ...
def __getattr__(self, provider_name: Any) -> Provider[T]: ... def __getattr__(self, provider_name: Any) -> Provider[T]: ...
@overload @overload
def __call__( def __call__(self, provider_name: Optional[Any] = None, *args: Injection, **kwargs: Injection) -> T: ...
self, provider_name: Optional[Any] = None, *args: Injection, **kwargs: Injection
) -> T: ...
@overload @overload
def __call__( def __call__(self, provider_name: Optional[Any] = None, *args: Injection, **kwargs: Injection) -> Awaitable[T]: ...
self, provider_name: Optional[Any] = None, *args: Injection, **kwargs: Injection def async_(self, provider_name: Optional[Any] = None, *args: Injection, **kwargs: Injection) -> Awaitable[T]: ...
) -> Awaitable[T]: ...
def async_(
self, provider_name: Optional[Any] = None, *args: Injection, **kwargs: Injection
) -> Awaitable[T]: ...
@property @property
def providers(self) -> _Dict[Any, Provider[T]]: ... def providers(self) -> _Dict[Any, Provider[T]]: ...
def set_providers( def set_providers(self, provider_dict: Optional[_Dict[Any, Provider[T]]] = None, **provider_kwargs: Provider[T]) -> Aggregate[T]: ...
self,
provider_dict: Optional[_Dict[Any, Provider[T]]] = None,
**provider_kwargs: Provider[T],
) -> Aggregate[T]: ...
class Dependency(Provider[T]): class Dependency(Provider[T]):
def __init__( def __init__(self, instance_of: Type[T] = object, default: Optional[Union[Provider, Any]] = None) -> None: ...
self,
instance_of: Type[T] = object,
default: Optional[Union[Provider, Any]] = None,
) -> None: ...
def __getattr__(self, name: str) -> Any: ... def __getattr__(self, name: str) -> Any: ...
@property @property
def instance_of(self) -> Type[T]: ... def instance_of(self) -> Type[T]: ...
def set_instance_of(self, instance_of: Type[T]) -> Dependency[T]: ... def set_instance_of(self, instance_of: Type[T]) -> Dependency[T]: ...
@property @property
def default(self) -> Provider[T]: ... def default(self) -> Provider[T]: ...
def set_default(self, default: Optional[Union[Provider, Any]]) -> Dependency[T]: ... def set_default(self, default: Optional[Union[Provider, Any]]) -> Dependency[T]: ...
@property @property
def is_defined(self) -> bool: ... def is_defined(self) -> bool: ...
def provided_by(self, provider: Provider) -> OverridingContext[P]: ... def provided_by(self, provider: Provider) -> OverridingContext[P]: ...
@ -148,8 +140,10 @@ class Dependency(Provider[T]):
def parent_name(self) -> Optional[str]: ... def parent_name(self) -> Optional[str]: ...
def assign_parent(self, parent: ProviderParent) -> None: ... def assign_parent(self, parent: ProviderParent) -> None: ...
class ExternalDependency(Dependency[T]): ... class ExternalDependency(Dependency[T]): ...
class DependenciesContainer(Object): class DependenciesContainer(Object):
def __init__(self, **dependencies: Provider) -> None: ... def __init__(self, **dependencies: Provider) -> None: ...
def __getattr__(self, name: str) -> Provider: ... def __getattr__(self, name: str) -> Provider: ...
@ -162,18 +156,12 @@ class DependenciesContainer(Object):
def parent_name(self) -> Optional[str]: ... def parent_name(self) -> Optional[str]: ...
def assign_parent(self, parent: ProviderParent) -> None: ... def assign_parent(self, parent: ProviderParent) -> None: ...
class Callable(Provider[T]): class Callable(Provider[T]):
def __init__( def __init__(self, provides: Optional[Union[_Callable[..., T], str]] = None, *args: Injection, **kwargs: Injection) -> None: ...
self,
provides: Optional[Union[_Callable[..., T], str]] = None,
*args: Injection,
**kwargs: Injection,
) -> None: ...
@property @property
def provides(self) -> Optional[_Callable[..., T]]: ... def provides(self) -> Optional[_Callable[..., T]]: ...
def set_provides( def set_provides(self, provides: Optional[Union[_Callable[..., T], str]]) -> Callable[T]: ...
self, provides: Optional[Union[_Callable[..., T], str]]
) -> Callable[T]: ...
@property @property
def args(self) -> Tuple[Injection]: ... def args(self) -> Tuple[Injection]: ...
def add_args(self, *args: Injection) -> Callable[T]: ... def add_args(self, *args: Injection) -> Callable[T]: ...
@ -185,23 +173,32 @@ class Callable(Provider[T]):
def set_kwargs(self, **kwargs: Injection) -> Callable[T]: ... def set_kwargs(self, **kwargs: Injection) -> Callable[T]: ...
def clear_kwargs(self) -> Callable[T]: ... def clear_kwargs(self) -> Callable[T]: ...
class DelegatedCallable(Callable[T]): ... class DelegatedCallable(Callable[T]): ...
class AbstractCallable(Callable[T]): class AbstractCallable(Callable[T]):
def override(self, provider: Callable) -> OverridingContext[P]: ... def override(self, provider: Callable) -> OverridingContext[P]: ...
class CallableDelegate(Delegate): class CallableDelegate(Delegate):
def __init__(self, callable: Callable) -> None: ... def __init__(self, callable: Callable) -> None: ...
class Coroutine(Callable[T]): ... class Coroutine(Callable[T]): ...
class DelegatedCoroutine(Coroutine[T]): ... class DelegatedCoroutine(Coroutine[T]): ...
class AbstractCoroutine(Coroutine[T]): class AbstractCoroutine(Coroutine[T]):
def override(self, provider: Coroutine) -> OverridingContext[P]: ... def override(self, provider: Coroutine) -> OverridingContext[P]: ...
class CoroutineDelegate(Delegate): class CoroutineDelegate(Delegate):
def __init__(self, coroutine: Coroutine) -> None: ... def __init__(self, coroutine: Coroutine) -> None: ...
class ConfigurationOption(Provider[Any]): class ConfigurationOption(Provider[Any]):
UNDEFINED: object UNDEFINED: object
def __init__(self, name: Tuple[str], root: Configuration) -> None: ... def __init__(self, name: Tuple[str], root: Configuration) -> None: ...
@ -215,137 +212,89 @@ class ConfigurationOption(Provider[Any]):
def get_name_segments(self) -> Tuple[Union[str, Provider]]: ... def get_name_segments(self) -> Tuple[Union[str, Provider]]: ...
def as_int(self) -> TypedConfigurationOption[int]: ... def as_int(self) -> TypedConfigurationOption[int]: ...
def as_float(self) -> TypedConfigurationOption[float]: ... def as_float(self) -> TypedConfigurationOption[float]: ...
def as_( def as_(self, callback: _Callable[..., T], *args: Injection, **kwargs: Injection) -> TypedConfigurationOption[T]: ...
self, callback: _Callable[..., T], *args: Injection, **kwargs: Injection
) -> TypedConfigurationOption[T]: ...
def required(self) -> ConfigurationOption: ... def required(self) -> ConfigurationOption: ...
def is_required(self) -> bool: ... def is_required(self) -> bool: ...
def update(self, value: Any) -> None: ... def update(self, value: Any) -> None: ...
def from_ini( def from_ini(self, filepath: Union[Path, str], required: bool = False, envs_required: bool = False) -> None: ...
self, def from_yaml(self, filepath: Union[Path, str], required: bool = False, loader: Optional[Any] = None, envs_required: bool = False) -> None: ...
filepath: Union[Path, str], def from_json(self, filepath: Union[Path, str], required: bool = False, envs_required: bool = False) -> None: ...
required: bool = False, def from_pydantic(self, settings: PydanticSettings, required: bool = False, **kwargs: Any) -> None: ...
envs_required: Optional[bool] = False,
) -> None: ...
def from_yaml(
self,
filepath: Union[Path, str],
required: bool = False,
loader: Optional[Any] = None,
envs_required: Optional[bool] = False,
) -> None: ...
def from_json(
self,
filepath: Union[Path, str],
required: bool = False,
envs_required: Optional[bool] = False,
) -> None: ...
def from_pydantic(
self, settings: PydanticSettings, required: bool = False, **kwargs: Any
) -> None: ...
def from_dict(self, options: _Dict[str, Any], required: bool = False) -> None: ... def from_dict(self, options: _Dict[str, Any], required: bool = False) -> None: ...
def from_env( def from_env(self, name: str, default: Optional[Any] = None, required: bool = False, as_: Optional[_Callable[..., Any]] = None) -> None: ...
self,
name: str,
default: Optional[Any] = None,
required: bool = False,
as_: Optional[_Callable[..., Any]] = None,
) -> None: ...
def from_value(self, value: Any) -> None: ... def from_value(self, value: Any) -> None: ...
class TypedConfigurationOption(Callable[T]): class TypedConfigurationOption(Callable[T]):
@property @property
def option(self) -> ConfigurationOption: ... def option(self) -> ConfigurationOption: ...
class Configuration(Object[Any]): class Configuration(Object[Any]):
DEFAULT_NAME: str = "config" DEFAULT_NAME: str = "config"
def __init__( def __init__(
self, self,
name: str = DEFAULT_NAME, name: str = DEFAULT_NAME,
default: Optional[Any] = None, default: Optional[Any] = None,
*, *,
strict: bool = False, strict: bool = False,
ini_files: Optional[_Iterable[Union[Path, str]]] = None, ini_files: Optional[_Iterable[Union[Path, str]]] = None,
yaml_files: Optional[_Iterable[Union[Path, str]]] = None, yaml_files: Optional[_Iterable[Union[Path, str]]] = None,
json_files: Optional[_Iterable[Union[Path, str]]] = None, json_files: Optional[_Iterable[Union[Path, str]]] = None,
pydantic_settings: Optional[_Iterable[PydanticSettings]] = None, pydantic_settings: Optional[_Iterable[PydanticSettings]] = None,
) -> None: ... ) -> None: ...
def __enter__(self) -> Configuration: ... def __enter__(self) -> Configuration : ...
def __exit__(self, *exc_info: Any) -> None: ... def __exit__(self, *exc_info: Any) -> None: ...
def __getattr__(self, item: str) -> ConfigurationOption: ... def __getattr__(self, item: str) -> ConfigurationOption: ...
def __getitem__(self, item: Union[str, Provider]) -> ConfigurationOption: ... def __getitem__(self, item: Union[str, Provider]) -> ConfigurationOption: ...
def get_name(self) -> str: ... def get_name(self) -> str: ...
def set_name(self, name: str) -> Configuration: ... def set_name(self, name: str) -> Configuration: ...
def get_default(self) -> _Dict[Any, Any]: ... def get_default(self) -> _Dict[Any, Any]: ...
def set_default(self, default: _Dict[Any, Any]): ... def set_default(self, default: _Dict[Any, Any]): ...
def get_strict(self) -> bool: ... def get_strict(self) -> bool: ...
def set_strict(self, strict: bool) -> Configuration: ... def set_strict(self, strict: bool) -> Configuration: ...
def get_children(self) -> _Dict[str, ConfigurationOption]: ... def get_children(self) -> _Dict[str, ConfigurationOption]: ...
def set_children( def set_children(self, children: _Dict[str, ConfigurationOption]) -> Configuration: ...
self, children: _Dict[str, ConfigurationOption]
) -> Configuration: ...
def get_ini_files(self) -> _List[Union[Path, str]]: ... def get_ini_files(self) -> _List[Union[Path, str]]: ...
def set_ini_files(self, files: _Iterable[Union[Path, str]]) -> Configuration: ... def set_ini_files(self, files: _Iterable[Union[Path, str]]) -> Configuration: ...
def get_yaml_files(self) -> _List[Union[Path, str]]: ... def get_yaml_files(self) -> _List[Union[Path, str]]: ...
def set_yaml_files(self, files: _Iterable[Union[Path, str]]) -> Configuration: ... def set_yaml_files(self, files: _Iterable[Union[Path, str]]) -> Configuration: ...
def get_json_files(self) -> _List[Union[Path, str]]: ... def get_json_files(self) -> _List[Union[Path, str]]: ...
def set_json_files(self, files: _Iterable[Union[Path, str]]) -> Configuration: ... def set_json_files(self, files: _Iterable[Union[Path, str]]) -> Configuration: ...
def get_pydantic_settings(self) -> _List[PydanticSettings]: ... def get_pydantic_settings(self) -> _List[PydanticSettings]: ...
def set_pydantic_settings( def set_pydantic_settings(self, settings: _Iterable[PydanticSettings]) -> Configuration: ...
self, settings: _Iterable[PydanticSettings]
) -> Configuration: ...
def load(self, required: bool = False, envs_required: bool = False) -> None: ... def load(self, required: bool = False, envs_required: bool = False) -> None: ...
def get(self, selector: str) -> Any: ... def get(self, selector: str) -> Any: ...
def set(self, selector: str, value: Any) -> OverridingContext[P]: ... def set(self, selector: str, value: Any) -> OverridingContext[P]: ...
def reset_cache(self) -> None: ... def reset_cache(self) -> None: ...
def update(self, value: Any) -> None: ... def update(self, value: Any) -> None: ...
def from_ini( def from_ini(self, filepath: Union[Path, str], required: bool = False, envs_required: bool = False) -> None: ...
self, def from_yaml(self, filepath: Union[Path, str], required: bool = False, loader: Optional[Any] = None, envs_required: bool = False) -> None: ...
filepath: Union[Path, str], def from_json(self, filepath: Union[Path, str], required: bool = False, envs_required: bool = False) -> None: ...
required: bool = False, def from_pydantic(self, settings: PydanticSettings, required: bool = False, **kwargs: Any) -> None: ...
envs_required: bool = False,
) -> None: ...
def from_yaml(
self,
filepath: Union[Path, str],
required: bool = False,
loader: Optional[Any] = None,
envs_required: bool = False,
) -> None: ...
def from_json(
self,
filepath: Union[Path, str],
required: bool = False,
envs_required: bool = False,
) -> None: ...
def from_pydantic(
self, settings: PydanticSettings, required: bool = False, **kwargs: Any
) -> None: ...
def from_dict(self, options: _Dict[str, Any], required: bool = False) -> None: ... def from_dict(self, options: _Dict[str, Any], required: bool = False) -> None: ...
def from_env( def from_env(self, name: str, default: Optional[Any] = None, required: bool = False, as_: Optional[_Callable[..., Any]] = None) -> None: ...
self,
name: str,
default: Optional[Any] = None,
required: bool = False,
as_: Optional[_Callable[..., Any]] = None,
) -> None: ...
def from_value(self, value: Any) -> None: ... def from_value(self, value: Any) -> None: ...
class Factory(Provider[T]): class Factory(Provider[T]):
provided_type: Optional[Type] provided_type: Optional[Type]
def __init__( def __init__(self, provides: Optional[Union[_Callable[..., T], str]] = None, *args: Injection, **kwargs: Injection) -> None: ...
self,
provides: Optional[Union[_Callable[..., T], str]] = None,
*args: Injection,
**kwargs: Injection,
) -> None: ...
@property @property
def cls(self) -> Type[T]: ... def cls(self) -> Type[T]: ...
@property @property
def provides(self) -> Optional[_Callable[..., T]]: ... def provides(self) -> Optional[_Callable[..., T]]: ...
def set_provides( def set_provides(self, provides: Optional[Union[_Callable[..., T], str]]) -> Factory[T]: ...
self, provides: Optional[Union[_Callable[..., T], str]]
) -> Factory[T]: ...
@property @property
def args(self) -> Tuple[Injection]: ... def args(self) -> Tuple[Injection]: ...
def add_args(self, *args: Injection) -> Factory[T]: ... def add_args(self, *args: Injection) -> Factory[T]: ...
@ -362,39 +311,33 @@ class Factory(Provider[T]):
def set_attributes(self, **kwargs: Injection) -> Factory[T]: ... def set_attributes(self, **kwargs: Injection) -> Factory[T]: ...
def clear_attributes(self) -> Factory[T]: ... def clear_attributes(self) -> Factory[T]: ...
class DelegatedFactory(Factory[T]): ... class DelegatedFactory(Factory[T]): ...
class AbstractFactory(Factory[T]): class AbstractFactory(Factory[T]):
def override(self, provider: Factory) -> OverridingContext[P]: ... def override(self, provider: Factory) -> OverridingContext[P]: ...
class FactoryDelegate(Delegate): class FactoryDelegate(Delegate):
def __init__(self, factory: Factory): ... def __init__(self, factory: Factory): ...
class FactoryAggregate(Aggregate[T]): class FactoryAggregate(Aggregate[T]):
def __getattr__(self, provider_name: Any) -> Factory[T]: ... def __getattr__(self, provider_name: Any) -> Factory[T]: ...
@property @property
def factories(self) -> _Dict[Any, Factory[T]]: ... def factories(self) -> _Dict[Any, Factory[T]]: ...
def set_factories( def set_factories(self, provider_dict: Optional[_Dict[Any, Factory[T]]] = None, **provider_kwargs: Factory[T]) -> FactoryAggregate[T]: ...
self,
provider_dict: Optional[_Dict[Any, Factory[T]]] = None,
**provider_kwargs: Factory[T],
) -> FactoryAggregate[T]: ...
class BaseSingleton(Provider[T]): class BaseSingleton(Provider[T]):
provided_type = Optional[Type] provided_type = Optional[Type]
def __init__( def __init__(self, provides: Optional[Union[_Callable[..., T], str]] = None, *args: Injection, **kwargs: Injection) -> None: ...
self,
provides: Optional[Union[_Callable[..., T], str]] = None,
*args: Injection,
**kwargs: Injection,
) -> None: ...
@property @property
def cls(self) -> Type[T]: ... def cls(self) -> Type[T]: ...
@property @property
def provides(self) -> Optional[_Callable[..., T]]: ... def provides(self) -> Optional[_Callable[..., T]]: ...
def set_provides( def set_provides(self, provides: Optional[Union[_Callable[..., T], str]]) -> BaseSingleton[T]: ...
self, provides: Optional[Union[_Callable[..., T], str]]
) -> BaseSingleton[T]: ...
@property @property
def args(self) -> Tuple[Injection]: ... def args(self) -> Tuple[Injection]: ...
def add_args(self, *args: Injection) -> BaseSingleton[T]: ... def add_args(self, *args: Injection) -> BaseSingleton[T]: ...
@ -413,20 +356,36 @@ class BaseSingleton(Provider[T]):
def reset(self) -> SingletonResetContext[BS]: ... def reset(self) -> SingletonResetContext[BS]: ...
def full_reset(self) -> SingletonFullResetContext[BS]: ... def full_reset(self) -> SingletonFullResetContext[BS]: ...
class Singleton(BaseSingleton[T]): ... class Singleton(BaseSingleton[T]): ...
class DelegatedSingleton(Singleton[T]): ... class DelegatedSingleton(Singleton[T]): ...
class ThreadSafeSingleton(Singleton[T]): ... class ThreadSafeSingleton(Singleton[T]): ...
class DelegatedThreadSafeSingleton(ThreadSafeSingleton[T]): ... class DelegatedThreadSafeSingleton(ThreadSafeSingleton[T]): ...
class ThreadLocalSingleton(BaseSingleton[T]): ... class ThreadLocalSingleton(BaseSingleton[T]): ...
class ContextLocalSingleton(BaseSingleton[T]): ... class ContextLocalSingleton(BaseSingleton[T]): ...
class DelegatedThreadLocalSingleton(ThreadLocalSingleton[T]): ... class DelegatedThreadLocalSingleton(ThreadLocalSingleton[T]): ...
class AbstractSingleton(BaseSingleton[T]): class AbstractSingleton(BaseSingleton[T]):
def override(self, provider: BaseSingleton) -> OverridingContext[P]: ... def override(self, provider: BaseSingleton) -> OverridingContext[P]: ...
class SingletonDelegate(Delegate): class SingletonDelegate(Delegate):
def __init__(self, singleton: BaseSingleton): ... def __init__(self, singleton: BaseSingleton): ...
class List(Provider[_List]): class List(Provider[_List]):
def __init__(self, *args: Injection): ... def __init__(self, *args: Injection): ...
@property @property
@ -435,63 +394,29 @@ class List(Provider[_List]):
def set_args(self, *args: Injection) -> List[T]: ... def set_args(self, *args: Injection) -> List[T]: ...
def clear_args(self) -> List[T]: ... def clear_args(self) -> List[T]: ...
class Dict(Provider[_Dict]): class Dict(Provider[_Dict]):
def __init__( def __init__(self, dict_: Optional[_Dict[Any, Injection]] = None, **kwargs: Injection): ...
self, dict_: Optional[_Dict[Any, Injection]] = None, **kwargs: Injection
): ...
@property @property
def kwargs(self) -> _Dict[Any, Injection]: ... def kwargs(self) -> _Dict[Any, Injection]: ...
def add_kwargs( def add_kwargs(self, dict_: Optional[_Dict[Any, Injection]] = None, **kwargs: Injection) -> Dict: ...
self, dict_: Optional[_Dict[Any, Injection]] = None, **kwargs: Injection def set_kwargs(self, dict_: Optional[_Dict[Any, Injection]] = None, **kwargs: Injection) -> Dict: ...
) -> Dict: ...
def set_kwargs(
self, dict_: Optional[_Dict[Any, Injection]] = None, **kwargs: Injection
) -> Dict: ...
def clear_kwargs(self) -> Dict: ... def clear_kwargs(self) -> Dict: ...
class Resource(Provider[T]): class Resource(Provider[T]):
@overload @overload
def __init__( def __init__(self, provides: Optional[Type[resources.Resource[T]]] = None, *args: Injection, **kwargs: Injection) -> None: ...
self,
provides: Optional[Type[resources.Resource[T]]] = None,
*args: Injection,
**kwargs: Injection,
) -> None: ...
@overload @overload
def __init__( def __init__(self, provides: Optional[Type[resources.AsyncResource[T]]] = None, *args: Injection, **kwargs: Injection) -> None: ...
self,
provides: Optional[Type[resources.AsyncResource[T]]] = None,
*args: Injection,
**kwargs: Injection,
) -> None: ...
@overload @overload
def __init__( def __init__(self, provides: Optional[_Callable[..., _Iterator[T]]] = None, *args: Injection, **kwargs: Injection) -> None: ...
self,
provides: Optional[_Callable[..., _Iterator[T]]] = None,
*args: Injection,
**kwargs: Injection,
) -> None: ...
@overload @overload
def __init__( def __init__(self, provides: Optional[_Callable[..., _AsyncIterator[T]]] = None, *args: Injection, **kwargs: Injection) -> None: ...
self,
provides: Optional[_Callable[..., _AsyncIterator[T]]] = None,
*args: Injection,
**kwargs: Injection,
) -> None: ...
@overload @overload
def __init__( def __init__(self, provides: Optional[_Callable[..., _Coroutine[Injection, Injection, T]]] = None, *args: Injection, **kwargs: Injection) -> None: ...
self,
provides: Optional[_Callable[..., _Coroutine[Injection, Injection, T]]] = None,
*args: Injection,
**kwargs: Injection,
) -> None: ...
@overload @overload
def __init__( def __init__(self, provides: Optional[Union[_Callable[..., T], str]] = None, *args: Injection, **kwargs: Injection) -> None: ...
self,
provides: Optional[Union[_Callable[..., T], str]] = None,
*args: Injection,
**kwargs: Injection,
) -> None: ...
@property @property
def provides(self) -> Optional[_Callable[..., Any]]: ... def provides(self) -> Optional[_Callable[..., Any]]: ...
def set_provides(self, provides: Optional[Any]) -> Resource[T]: ... def set_provides(self, provides: Optional[Any]) -> Resource[T]: ...
@ -510,13 +435,9 @@ class Resource(Provider[T]):
def init(self) -> Optional[Awaitable[T]]: ... def init(self) -> Optional[Awaitable[T]]: ...
def shutdown(self) -> Optional[Awaitable]: ... def shutdown(self) -> Optional[Awaitable]: ...
class Container(Provider[T]): class Container(Provider[T]):
def __init__( def __init__(self, container_cls: Type[T], container: Optional[T] = None, **overriding_providers: Union[Provider, Any]) -> None: ...
self,
container_cls: Type[T],
container: Optional[T] = None,
**overriding_providers: Union[Provider, Any],
) -> None: ...
def __getattr__(self, name: str) -> Provider: ... def __getattr__(self, name: str) -> Provider: ...
@property @property
def container(self) -> T: ... def container(self) -> T: ...
@ -527,51 +448,50 @@ class Container(Provider[T]):
def parent_name(self) -> Optional[str]: ... def parent_name(self) -> Optional[str]: ...
def assign_parent(self, parent: ProviderParent) -> None: ... def assign_parent(self, parent: ProviderParent) -> None: ...
class Selector(Provider[Any]): class Selector(Provider[Any]):
def __init__( def __init__(self, selector: Optional[_Callable[..., Any]] = None, **providers: Provider): ...
self, selector: Optional[_Callable[..., Any]] = None, **providers: Provider
): ...
def __getattr__(self, name: str) -> Provider: ... def __getattr__(self, name: str) -> Provider: ...
@property @property
def selector(self) -> Optional[_Callable[..., Any]]: ... def selector(self) -> Optional[_Callable[..., Any]]: ...
def set_selector(self, selector: Optional[_Callable[..., Any]]) -> Selector: ... def set_selector(self, selector: Optional[_Callable[..., Any]]) -> Selector: ...
@property @property
def providers(self) -> _Dict[str, Provider]: ... def providers(self) -> _Dict[str, Provider]: ...
def set_providers(self, **providers: Provider) -> Selector: ... def set_providers(self, **providers: Provider) -> Selector: ...
class ProvidedInstanceFluentInterface: class ProvidedInstanceFluentInterface:
def __getattr__(self, item: Any) -> AttributeGetter: ... def __getattr__(self, item: Any) -> AttributeGetter: ...
def __getitem__(self, item: Any) -> ItemGetter: ... def __getitem__(self, item: Any) -> ItemGetter: ...
def call(self, *args: Injection, **kwargs: Injection) -> MethodCaller: ... def call(self, *args: Injection, **kwargs: Injection) -> MethodCaller: ...
@property @property
def provides(self) -> Optional[Provider]: ... def provides(self) -> Optional[Provider]: ...
def set_provides( def set_provides(self, provides: Optional[Provider]) -> ProvidedInstanceFluentInterface: ...
self, provides: Optional[Provider]
) -> ProvidedInstanceFluentInterface: ...
class ProvidedInstance(Provider, ProvidedInstanceFluentInterface): class ProvidedInstance(Provider, ProvidedInstanceFluentInterface):
def __init__(self, provides: Optional[Provider] = None) -> None: ... def __init__(self, provides: Optional[Provider] = None) -> None: ...
class AttributeGetter(Provider, ProvidedInstanceFluentInterface): class AttributeGetter(Provider, ProvidedInstanceFluentInterface):
def __init__( def __init__(self, provides: Optional[Provider] = None, name: Optional[str] = None) -> None: ...
self, provides: Optional[Provider] = None, name: Optional[str] = None
) -> None: ...
@property @property
def name(self) -> Optional[str]: ... def name(self) -> Optional[str]: ...
def set_name(self, name: Optional[str]) -> ProvidedInstanceFluentInterface: ... def set_name(self, name: Optional[str]) -> ProvidedInstanceFluentInterface: ...
class ItemGetter(Provider, ProvidedInstanceFluentInterface): class ItemGetter(Provider, ProvidedInstanceFluentInterface):
def __init__( def __init__(self, provides: Optional[Provider] = None, name: Optional[str] = None) -> None: ...
self, provides: Optional[Provider] = None, name: Optional[str] = None
) -> None: ...
@property @property
def name(self) -> Optional[str]: ... def name(self) -> Optional[str]: ...
def set_name(self, name: Optional[str]) -> ProvidedInstanceFluentInterface: ... def set_name(self, name: Optional[str]) -> ProvidedInstanceFluentInterface: ...
class MethodCaller(Provider, ProvidedInstanceFluentInterface): class MethodCaller(Provider, ProvidedInstanceFluentInterface):
def __init__( def __init__(self, provides: Optional[Provider] = None, *args: Injection, **kwargs: Injection) -> None: ...
self, provides: Optional[Provider] = None, *args: Injection, **kwargs: Injection
) -> None: ...
class OverridingContext(Generic[T]): class OverridingContext(Generic[T]):
def __init__(self, overridden: Provider, overriding: Provider): ... def __init__(self, overridden: Provider, overriding: Provider): ...
@ -580,39 +500,47 @@ class OverridingContext(Generic[T]):
pass pass
... ...
class BaseSingletonResetContext(Generic[T]): class BaseSingletonResetContext(Generic[T]):
def __init__(self, provider: T): ... def __init__(self, provider: T): ...
def __enter__(self) -> T: ... def __enter__(self) -> T: ...
def __exit__(self, *_: Any) -> None: ... def __exit__(self, *_: Any) -> None: ...
class SingletonResetContext(BaseSingletonResetContext): ...
class SingletonFullResetContext(BaseSingletonResetContext): ... class SingletonResetContext(BaseSingletonResetContext):
...
class SingletonFullResetContext(BaseSingletonResetContext):
...
CHILD_PROVIDERS: Tuple[Provider] CHILD_PROVIDERS: Tuple[Provider]
def is_provider(instance: Any) -> bool: ... def is_provider(instance: Any) -> bool: ...
def ensure_is_provider(instance: Any) -> Provider: ... def ensure_is_provider(instance: Any) -> Provider: ...
def is_delegated(instance: Any) -> bool: ... def is_delegated(instance: Any) -> bool: ...
def represent_provider(provider: Provider, provides: Any) -> str: ... def represent_provider(provider: Provider, provides: Any) -> str: ...
def deepcopy(instance: Any, memo: Optional[_Dict[Any, Any]] = None) -> Any: ...
def deepcopy_args(
provider: Provider[Any], def deepcopy(instance: Any, memo: Optional[_Dict[Any, Any]] = None): Any: ...
args: Tuple[Any, ...],
memo: Optional[_Dict[int, Any]] = None,
) -> Tuple[Any, ...]: ...
def deepcopy_kwargs(
provider: Provider[Any],
kwargs: _Dict[str, Any],
memo: Optional[_Dict[int, Any]] = None,
) -> Dict[str, Any]: ...
def merge_dicts(dict1: _Dict[Any, Any], dict2: _Dict[Any, Any]) -> _Dict[Any, Any]: ... def merge_dicts(dict1: _Dict[Any, Any], dict2: _Dict[Any, Any]) -> _Dict[Any, Any]: ...
def traverse(
*providers: Provider, types: Optional[_Iterable[Type]] = None
) -> _Iterator[Provider]: ... def traverse(*providers: Provider, types: Optional[_Iterable[Type]]=None) -> _Iterator[Provider]: ...
if yaml: if yaml:
class YamlLoader(yaml.SafeLoader): ... class YamlLoader(yaml.SafeLoader): ...
else: else:
class YamlLoader: ... class YamlLoader: ...

View File

@ -2,9 +2,6 @@
from __future__ import absolute_import from __future__ import absolute_import
import asyncio
import builtins
import contextvars
import copy import copy
import errno import errno
import functools import functools
@ -15,62 +12,78 @@ import os
import re import re
import sys import sys
import threading import threading
import types
import warnings import warnings
from configparser import ConfigParser as IniConfigParser
try: try:
from inspect import _is_coroutine_mark as _is_coroutine_marker import contextvars
except ImportError: except ImportError:
try: contextvars = None
# Python >=3.12.0,<3.12.5
from inspect import _is_coroutine_marker try:
except ImportError: import builtins
except ImportError:
# Python 2.7
import __builtin__ as builtins
try:
import asyncio
except ImportError:
asyncio = None
_is_coroutine_marker = None
else:
if sys.version_info >= (3, 5, 3):
import asyncio.coroutines
_is_coroutine_marker = asyncio.coroutines._is_coroutine
else:
_is_coroutine_marker = True _is_coroutine_marker = True
try: try:
from asyncio.coroutines import _is_coroutine import ConfigParser as iniconfigparser
except ImportError: except ImportError:
_is_coroutine = True import configparser as iniconfigparser
try: try:
import yaml import yaml
except ImportError: except ImportError:
yaml = None yaml = None
has_pydantic_settings = True
cdef bint pydantic_v1 = False
cdef str pydantic_module = "pydantic_settings"
cdef str pydantic_extra = "pydantic2"
try: try:
from pydantic_settings import BaseSettings as PydanticSettings import pydantic
except ImportError: except ImportError:
try: pydantic = None
# pydantic-settings requires pydantic v2,
# so it is safe to assume that we're dealing with v1:
from pydantic import BaseSettings as PydanticSettings
pydantic_v1 = True
pydantic_module = "pydantic"
pydantic_extra = "pydantic"
except ImportError:
# if it is present, ofc
has_pydantic_settings = False
from .errors import ( from .errors import (
Error, Error,
NoSuchProviderError, NoSuchProviderError,
NonCopyableArgumentError,
) )
cimport cython cimport cython
if sys.version_info[0] == 3: # pragma: no cover
CLASS_TYPES = (type,)
else: # pragma: no cover
CLASS_TYPES = (type, types.ClassType)
copy._deepcopy_dispatch[types.MethodType] = \
lambda obj, memo: type(obj)(obj.im_func,
copy.deepcopy(obj.im_self, memo),
obj.im_class)
if sys.version_info[:2] == (3, 5):
warnings.warn(
"Dependency Injector will drop support of Python 3.5 after Jan 1st of 2022. "
"This does not mean that there will be any immediate breaking changes, "
"but tests will no longer be executed on Python 3.5, and bugs will not be addressed.",
category=DeprecationWarning,
)
config_env_marker_pattern = re.compile( config_env_marker_pattern = re.compile(
r"\${(?P<name>[^}^{:]+)(?P<separator>:?)(?P<default>.*?)}", r"\${(?P<name>[^}^{:]+)(?P<separator>:?)(?P<default>.*?)}",
) )
cdef str _resolve_config_env_markers(config_content: str, envs_required: bool): def _resolve_config_env_markers(config_content, envs_required=False):
"""Replace environment variable markers with their values.""" """Replace environment variable markers with their values."""
findings = list(config_env_marker_pattern.finditer(config_content)) findings = list(config_env_marker_pattern.finditer(config_content))
@ -89,19 +102,28 @@ cdef str _resolve_config_env_markers(config_content: str, envs_required: bool):
return config_content return config_content
cdef object _parse_ini_file(filepath, envs_required: bool | None): if sys.version_info[0] == 3:
parser = IniConfigParser() def _parse_ini_file(filepath, envs_required=False):
parser = iniconfigparser.ConfigParser()
with open(filepath) as config_file: with open(filepath) as config_file:
config_string = config_file.read()
if envs_required is not None:
config_string = _resolve_config_env_markers( config_string = _resolve_config_env_markers(
config_string, config_file.read(),
envs_required=envs_required, envs_required=envs_required,
) )
parser.read_string(config_string) parser.read_string(config_string)
return parser return parser
else:
import StringIO
def _parse_ini_file(filepath, envs_required=False):
parser = iniconfigparser.ConfigParser()
with open(filepath) as config_file:
config_string = _resolve_config_env_markers(
config_file.read(),
envs_required=envs_required,
)
parser.readfp(StringIO.StringIO(config_string))
return parser
if yaml: if yaml:
@ -125,35 +147,10 @@ cdef int ASYNC_MODE_ENABLED = 1
cdef int ASYNC_MODE_DISABLED = 2 cdef int ASYNC_MODE_DISABLED = 2
cdef set __iscoroutine_typecache = set() cdef set __iscoroutine_typecache = set()
cdef tuple __COROUTINE_TYPES = asyncio.coroutines._COROUTINE_TYPES cdef tuple __COROUTINE_TYPES = asyncio.coroutines._COROUTINE_TYPES if asyncio else tuple()
cdef dict pydantic_settings_to_dict(settings, dict kwargs):
if not has_pydantic_settings:
raise Error(
f"Unable to load pydantic configuration - {pydantic_module} is not installed. "
"Install pydantic or install Dependency Injector with pydantic extras: "
f"\"pip install dependency-injector[{pydantic_extra}]\""
)
if isinstance(settings, type) and issubclass(settings, PydanticSettings):
raise Error(
"Got settings class, but expect instance: "
"instead \"{0}\" use \"{0}()\"".format(settings.__name__)
)
if not isinstance(settings, PydanticSettings):
raise Error(
f"Unable to recognize settings instance, expect \"{pydantic_module}.BaseSettings\", "
f"got {settings} instead"
)
if pydantic_v1:
return settings.dict(**kwargs)
return settings.model_dump(mode="python", **kwargs)
cdef class Provider: cdef class Provider(object):
"""Base provider class. """Base provider class.
:py:class:`Provider` is callable (implements ``__call__`` method). Every :py:class:`Provider` is callable (implements ``__call__`` method). Every
@ -875,9 +872,12 @@ cdef class Dependency(Provider):
def set_instance_of(self, instance_of): def set_instance_of(self, instance_of):
"""Set type.""" """Set type."""
if not isinstance(instance_of, type): if not isinstance(instance_of, CLASS_TYPES):
raise TypeError( raise TypeError(
f"\"instance_of\" is not a class (got {instance_of!r}))", "\"instance_of\" has incorrect type (expected {0}, got {1}))".format(
CLASS_TYPES,
instance_of,
),
) )
self._instance_of = instance_of self._instance_of = instance_of
return self return self
@ -1212,8 +1212,8 @@ cdef class Callable(Provider):
copied = _memorized_duplicate(self, memo) copied = _memorized_duplicate(self, memo)
copied.set_provides(_copy_if_provider(self.provides, memo)) copied.set_provides(_copy_if_provider(self.provides, memo))
copied.set_args(*deepcopy_args(self, self.args, memo)) copied.set_args(*deepcopy(self.args, memo))
copied.set_kwargs(**deepcopy_kwargs(self, self.kwargs, memo)) copied.set_kwargs(**deepcopy(self.kwargs, memo))
self._copy_overridings(copied, memo) self._copy_overridings(copied, memo)
return copied return copied
@ -1434,11 +1434,12 @@ cdef class Coroutine(Callable):
some_coroutine.add_kwargs(keyword_argument1=3, keyword_argument=4) some_coroutine.add_kwargs(keyword_argument1=3, keyword_argument=4)
""" """
_is_coroutine_marker = _is_coroutine_marker # Python >=3.12 _is_coroutine = _is_coroutine_marker
_is_coroutine = _is_coroutine # Python <3.16
def set_provides(self, provides): def set_provides(self, provides):
"""Set provider provides.""" """Set provider provides."""
if not asyncio:
raise Error("Package asyncio is not available")
provides = _resolve_string_import(provides) provides = _resolve_string_import(provides)
if provides and not asyncio.iscoroutinefunction(provides): if provides and not asyncio.iscoroutinefunction(provides):
raise Error(f"Provider {_class_qualname(self)} expected to get coroutine function, " raise Error(f"Provider {_class_qualname(self)} expected to get coroutine function, "
@ -1671,7 +1672,7 @@ cdef class ConfigurationOption(Provider):
try: try:
parser = _parse_ini_file( parser = _parse_ini_file(
filepath, filepath,
envs_required if envs_required is not UNDEFINED else self._is_strict_mode_enabled(), envs_required=envs_required if envs_required is not UNDEFINED else self._is_strict_mode_enabled(),
) )
except IOError as exception: except IOError as exception:
if required is not False \ if required is not False \
@ -1730,11 +1731,10 @@ cdef class ConfigurationOption(Provider):
raise raise
return return
if envs_required is not None: config_content = _resolve_config_env_markers(
config_content = _resolve_config_env_markers( config_content,
config_content, envs_required=envs_required if envs_required is not UNDEFINED else self._is_strict_mode_enabled(),
envs_required if envs_required is not UNDEFINED else self._is_strict_mode_enabled(), )
)
config = yaml.load(config_content, loader) config = yaml.load(config_content, loader)
current_config = self.__call__() current_config = self.__call__()
@ -1769,11 +1769,10 @@ cdef class ConfigurationOption(Provider):
raise raise
return return
if envs_required is not None: config_content = _resolve_config_env_markers(
config_content = _resolve_config_env_markers( config_content,
config_content, envs_required=envs_required if envs_required is not UNDEFINED else self._is_strict_mode_enabled(),
envs_required if envs_required is not UNDEFINED else self._is_strict_mode_enabled(), )
)
config = json.loads(config_content) config = json.loads(config_content)
current_config = self.__call__() current_config = self.__call__()
@ -1787,20 +1786,36 @@ cdef class ConfigurationOption(Provider):
Loaded configuration is merged recursively over existing configuration. Loaded configuration is merged recursively over existing configuration.
:param settings: Pydantic settings instances. :param settings: Pydantic settings instances.
:type settings: :py:class:`pydantic.BaseSettings` (pydantic v1) or :type settings: :py:class:`pydantic.BaseSettings`
:py:class:`pydantic_settings.BaseSettings` (pydantic v2 and onwards)
:param required: When required is True, raise an exception if settings dict is empty. :param required: When required is True, raise an exception if settings dict is empty.
:type required: bool :type required: bool
:param kwargs: Keyword arguments forwarded to ``pydantic.BaseSettings.dict()`` or :param kwargs: Keyword arguments forwarded to ``pydantic.BaseSettings.dict()`` call.
``pydantic_settings.BaseSettings.model_dump()`` call (based on pydantic version).
:type kwargs: Dict[Any, Any] :type kwargs: Dict[Any, Any]
:rtype: None :rtype: None
""" """
if pydantic is None:
raise Error(
"Unable to load pydantic configuration - pydantic is not installed. "
"Install pydantic or install Dependency Injector with pydantic extras: "
"\"pip install dependency-injector[pydantic]\""
)
self.from_dict(pydantic_settings_to_dict(settings, kwargs), required=required) if isinstance(settings, CLASS_TYPES) and issubclass(settings, pydantic.BaseSettings):
raise Error(
"Got settings class, but expect instance: "
"instead \"{0}\" use \"{0}()\"".format(settings.__name__)
)
if not isinstance(settings, pydantic.BaseSettings):
raise Error(
"Unable to recognize settings instance, expect \"pydantic.BaseSettings\", "
"got {0} instead".format(settings)
)
self.from_dict(settings.dict(**kwargs), required=required)
def from_dict(self, options, required=UNDEFINED): def from_dict(self, options, required=UNDEFINED):
"""Load configuration from the dictionary. """Load configuration from the dictionary.
@ -2226,7 +2241,7 @@ cdef class Configuration(Object):
try: try:
parser = _parse_ini_file( parser = _parse_ini_file(
filepath, filepath,
envs_required if envs_required is not UNDEFINED else self._is_strict_mode_enabled(), envs_required=envs_required if envs_required is not UNDEFINED else self._is_strict_mode_enabled(),
) )
except IOError as exception: except IOError as exception:
if required is not False \ if required is not False \
@ -2285,11 +2300,10 @@ cdef class Configuration(Object):
raise raise
return return
if envs_required is not None: config_content = _resolve_config_env_markers(
config_content = _resolve_config_env_markers( config_content,
config_content, envs_required=envs_required if envs_required is not UNDEFINED else self._is_strict_mode_enabled(),
envs_required if envs_required is not UNDEFINED else self._is_strict_mode_enabled(), )
)
config = yaml.load(config_content, loader) config = yaml.load(config_content, loader)
current_config = self.__call__() current_config = self.__call__()
@ -2324,11 +2338,10 @@ cdef class Configuration(Object):
raise raise
return return
if envs_required is not None: config_content = _resolve_config_env_markers(
config_content = _resolve_config_env_markers( config_content,
config_content, envs_required=envs_required if envs_required is not UNDEFINED else self._is_strict_mode_enabled(),
envs_required if envs_required is not UNDEFINED else self._is_strict_mode_enabled(), )
)
config = json.loads(config_content) config = json.loads(config_content)
current_config = self.__call__() current_config = self.__call__()
@ -2342,8 +2355,7 @@ cdef class Configuration(Object):
Loaded configuration is merged recursively over existing configuration. Loaded configuration is merged recursively over existing configuration.
:param settings: Pydantic settings instances. :param settings: Pydantic settings instances.
:type settings: :py:class:`pydantic.BaseSettings` (pydantic v1) or :type settings: :py:class:`pydantic.BaseSettings`
:py:class:`pydantic_settings.BaseSettings` (pydantic v2 and onwards)
:param required: When required is True, raise an exception if settings dict is empty. :param required: When required is True, raise an exception if settings dict is empty.
:type required: bool :type required: bool
@ -2353,8 +2365,26 @@ cdef class Configuration(Object):
:rtype: None :rtype: None
""" """
if pydantic is None:
raise Error(
"Unable to load pydantic configuration - pydantic is not installed. "
"Install pydantic or install Dependency Injector with pydantic extras: "
"\"pip install dependency-injector[pydantic]\""
)
self.from_dict(pydantic_settings_to_dict(settings, kwargs), required=required) if isinstance(settings, CLASS_TYPES) and issubclass(settings, pydantic.BaseSettings):
raise Error(
"Got settings class, but expect instance: "
"instead \"{0}\" use \"{0}()\"".format(settings.__name__)
)
if not isinstance(settings, pydantic.BaseSettings):
raise Error(
"Unable to recognize settings instance, expect \"pydantic.BaseSettings\", "
"got {0} instead".format(settings)
)
self.from_dict(settings.dict(**kwargs), required=required)
def from_dict(self, options, required=UNDEFINED): def from_dict(self, options, required=UNDEFINED):
"""Load configuration from the dictionary. """Load configuration from the dictionary.
@ -2502,8 +2532,8 @@ cdef class Factory(Provider):
copied = _memorized_duplicate(self, memo) copied = _memorized_duplicate(self, memo)
copied.set_provides(_copy_if_provider(self.provides, memo)) copied.set_provides(_copy_if_provider(self.provides, memo))
copied.set_args(*deepcopy_args(self, self.args, memo)) copied.set_args(*deepcopy(self.args, memo))
copied.set_kwargs(**deepcopy_kwargs(self, self.kwargs, memo)) copied.set_kwargs(**deepcopy(self.kwargs, memo))
copied.set_attributes(**deepcopy(self.attributes, memo)) copied.set_attributes(**deepcopy(self.attributes, memo))
self._copy_overridings(copied, memo) self._copy_overridings(copied, memo)
return copied return copied
@ -2801,8 +2831,8 @@ cdef class BaseSingleton(Provider):
copied = _memorized_duplicate(self, memo) copied = _memorized_duplicate(self, memo)
copied.set_provides(_copy_if_provider(self.provides, memo)) copied.set_provides(_copy_if_provider(self.provides, memo))
copied.set_args(*deepcopy_args(self, self.args, memo)) copied.set_args(*deepcopy(self.args, memo))
copied.set_kwargs(**deepcopy_kwargs(self, self.kwargs, memo)) copied.set_kwargs(**deepcopy(self.kwargs, memo))
copied.set_attributes(**deepcopy(self.attributes, memo)) copied.set_attributes(**deepcopy(self.attributes, memo))
self._copy_overridings(copied, memo) self._copy_overridings(copied, memo)
return copied return copied
@ -3184,8 +3214,8 @@ cdef class ThreadLocalSingleton(BaseSingleton):
return future_result return future_result
self._storage.instance = instance self._storage.instance = instance
finally:
return instance return instance
def _async_init_instance(self, future_result, result): def _async_init_instance(self, future_result, result):
try: try:
@ -3414,7 +3444,7 @@ cdef class List(Provider):
return copied return copied
copied = _memorized_duplicate(self, memo) copied = _memorized_duplicate(self, memo)
copied.set_args(*deepcopy_args(self, self.args, memo)) copied.set_args(*deepcopy(self.args, memo))
self._copy_overridings(copied, memo) self._copy_overridings(copied, memo)
return copied return copied
@ -3637,8 +3667,8 @@ cdef class Resource(Provider):
copied = _memorized_duplicate(self, memo) copied = _memorized_duplicate(self, memo)
copied.set_provides(_copy_if_provider(self.provides, memo)) copied.set_provides(_copy_if_provider(self.provides, memo))
copied.set_args(*deepcopy_args(self, self.args, memo)) copied.set_args(*deepcopy(self.args, memo))
copied.set_kwargs(**deepcopy_kwargs(self, self.kwargs, memo)) copied.set_kwargs(**deepcopy(self.kwargs, memo))
self._copy_overridings(copied, memo) self._copy_overridings(copied, memo)
@ -3937,14 +3967,18 @@ cdef class Resource(Provider):
@staticmethod @staticmethod
def _is_resource_subclass(instance): def _is_resource_subclass(instance):
if not isinstance(instance, type): if sys.version_info < (3, 5):
return False
if not isinstance(instance, CLASS_TYPES):
return return
from . import resources from . import resources
return issubclass(instance, resources.Resource) return issubclass(instance, resources.Resource)
@staticmethod @staticmethod
def _is_async_resource_subclass(instance): def _is_async_resource_subclass(instance):
if not isinstance(instance, type): if sys.version_info < (3, 5):
return False
if not isinstance(instance, CLASS_TYPES):
return return
from . import resources from . import resources
return issubclass(instance, resources.AsyncResource) return issubclass(instance, resources.AsyncResource)
@ -4484,8 +4518,8 @@ cdef class MethodCaller(Provider):
copied = _memorized_duplicate(self, memo) copied = _memorized_duplicate(self, memo)
copied.set_provides(_copy_if_provider(self.provides, memo)) copied.set_provides(_copy_if_provider(self.provides, memo))
copied.set_args(*deepcopy_args(self, self.args, memo)) copied.set_args(*deepcopy(self.args, memo))
copied.set_kwargs(**deepcopy_kwargs(self, self.kwargs, memo)) copied.set_kwargs(**deepcopy(self.kwargs, memo))
self._copy_overridings(copied, memo) self._copy_overridings(copied, memo)
return copied return copied
@ -4602,7 +4636,7 @@ cdef class MethodCaller(Provider):
future_result.set_result(result) future_result.set_result(result)
cdef class Injection: cdef class Injection(object):
"""Abstract injection class.""" """Abstract injection class."""
@ -4729,7 +4763,7 @@ cpdef tuple parse_named_injections(dict kwargs):
return tuple(injections) return tuple(injections)
cdef class OverridingContext: cdef class OverridingContext(object):
"""Provider overriding context. """Provider overriding context.
:py:class:`OverridingContext` is used by :py:meth:`Provider.override` for :py:class:`OverridingContext` is used by :py:meth:`Provider.override` for
@ -4765,7 +4799,7 @@ cdef class OverridingContext:
self._overridden.reset_last_overriding() self._overridden.reset_last_overriding()
cdef class BaseSingletonResetContext: cdef class BaseSingletonResetContext(object):
def __init__(self, Provider provider): def __init__(self, Provider provider):
self._singleton = provider self._singleton = provider
@ -4801,7 +4835,7 @@ cpdef bint is_provider(object instance):
:rtype: bool :rtype: bool
""" """
return (not isinstance(instance, type) and return (not isinstance(instance, CLASS_TYPES) and
getattr(instance, "__IS_PROVIDER__", False) is True) getattr(instance, "__IS_PROVIDER__", False) is True)
@ -4829,7 +4863,7 @@ cpdef bint is_delegated(object instance):
:rtype: bool :rtype: bool
""" """
return (not isinstance(instance, type) and return (not isinstance(instance, CLASS_TYPES) and
getattr(instance, "__IS_DELEGATED__", False) is True) getattr(instance, "__IS_DELEGATED__", False) is True)
@ -4860,7 +4894,7 @@ cpdef bint is_container_instance(object instance):
:rtype: bool :rtype: bool
""" """
return (not isinstance(instance, type) and return (not isinstance(instance, CLASS_TYPES) and
getattr(instance, "__IS_CONTAINER__", False) is True) getattr(instance, "__IS_CONTAINER__", False) is True)
@ -4872,7 +4906,7 @@ cpdef bint is_container_class(object instance):
:rtype: bool :rtype: bool
""" """
return (isinstance(instance, type) and return (isinstance(instance, CLASS_TYPES) and
getattr(instance, "__IS_CONTAINER__", False) is True) getattr(instance, "__IS_CONTAINER__", False) is True)
@ -4886,48 +4920,6 @@ cpdef object deepcopy(object instance, dict memo=None):
return copy.deepcopy(instance, memo) return copy.deepcopy(instance, memo)
cpdef tuple deepcopy_args(
Provider provider,
tuple args,
dict[int, object] memo = None,
):
"""A wrapper for deepcopy for positional arguments.
Used to improve debugability of objects that cannot be deep-copied.
"""
cdef list[object] out = []
for i, arg in enumerate(args):
try:
out.append(copy.deepcopy(arg, memo))
except Exception as e:
raise NonCopyableArgumentError(provider, index=i) from e
return tuple(out)
cpdef dict[str, object] deepcopy_kwargs(
Provider provider,
dict[str, object] kwargs,
dict[int, object] memo = None,
):
"""A wrapper for deepcopy for keyword arguments.
Used to improve debugability of objects that cannot be deep-copied.
"""
cdef dict[str, object] out = {}
for name, arg in kwargs.items():
try:
out[name] = copy.deepcopy(arg, memo)
except Exception as e:
raise NonCopyableArgumentError(provider, keyword=name) from e
return out
def __add_sys_streams(memo): def __add_sys_streams(memo):
"""Add system streams to memo dictionary. """Add system streams to memo dictionary.

View File

@ -10,14 +10,18 @@ T = TypeVar("T")
class Resource(Generic[T], metaclass=abc.ABCMeta): class Resource(Generic[T], metaclass=abc.ABCMeta):
@abc.abstractmethod @abc.abstractmethod
def init(self, *args, **kwargs) -> Optional[T]: ... def init(self, *args, **kwargs) -> Optional[T]:
...
def shutdown(self, resource: Optional[T]) -> None: ... def shutdown(self, resource: Optional[T]) -> None:
...
class AsyncResource(Generic[T], metaclass=abc.ABCMeta): class AsyncResource(Generic[T], metaclass=abc.ABCMeta):
@abc.abstractmethod @abc.abstractmethod
async def init(self, *args, **kwargs) -> Optional[T]: ... async def init(self, *args, **kwargs) -> Optional[T]:
...
async def shutdown(self, resource: Optional[T]) -> None: ... async def shutdown(self, resource: Optional[T]) -> None:
...

View File

@ -27,9 +27,9 @@ class SchemaProcessorV1:
return self._container.providers return self._container.providers
def _create_providers( def _create_providers(
self, self,
provider_schema: ProviderSchema, provider_schema: ProviderSchema,
container: Optional[containers.Container] = None, container: Optional[containers.Container] = None,
) -> None: ) -> None:
if container is None: if container is None:
container = self._container container = self._container
@ -57,9 +57,9 @@ class SchemaProcessorV1:
self._create_providers(provider_schema=data, container=provider) self._create_providers(provider_schema=data, container=provider)
def _setup_injections( # noqa: C901 def _setup_injections( # noqa: C901
self, self,
provider_schema: ProviderSchema, provider_schema: ProviderSchema,
container: Optional[containers.Container] = None, container: Optional[containers.Container] = None,
) -> None: ) -> None:
if container is None: if container is None:
container = self._container container = self._container
@ -72,7 +72,7 @@ class SchemaProcessorV1:
provides = data.get("provides") provides = data.get("provides")
if provides: if provides:
if isinstance(provides, str) and provides.startswith("container."): if isinstance(provides, str) and provides.startswith("container."):
provides = self._resolve_provider(provides[len("container.") :]) provides = self._resolve_provider(provides[len("container."):])
else: else:
provides = _import_string(provides) provides = _import_string(provides)
provider.set_provides(provides) provider.set_provides(provides)
@ -83,7 +83,7 @@ class SchemaProcessorV1:
injection = None injection = None
if isinstance(arg, str) and arg.startswith("container."): if isinstance(arg, str) and arg.startswith("container."):
injection = self._resolve_provider(arg[len("container.") :]) injection = self._resolve_provider(arg[len("container."):])
# TODO: refactoring # TODO: refactoring
if isinstance(arg, dict): if isinstance(arg, dict):
@ -91,23 +91,16 @@ class SchemaProcessorV1:
provider_type = _get_provider_cls(arg.get("provider")) provider_type = _get_provider_cls(arg.get("provider"))
provides = arg.get("provides") provides = arg.get("provides")
if provides: if provides:
if isinstance(provides, str) and provides.startswith( if isinstance(provides, str) and provides.startswith("container."):
"container." provides = self._resolve_provider(provides[len("container."):])
):
provides = self._resolve_provider(
provides[len("container.") :]
)
else: else:
provides = _import_string(provides) provides = _import_string(provides)
provider_args.append(provides) provider_args.append(provides)
for provider_arg in arg.get("args", []): for provider_arg in arg.get("args", []):
if isinstance( if isinstance(provider_arg, str) \
provider_arg, str and provider_arg.startswith("container."):
) and provider_arg.startswith("container."):
provider_args.append( provider_args.append(
self._resolve_provider( self._resolve_provider(provider_arg[len("container."):]),
provider_arg[len("container.") :]
),
) )
injection = provider_type(*provider_args) injection = provider_type(*provider_args)
@ -124,7 +117,7 @@ class SchemaProcessorV1:
injection = None injection = None
if isinstance(arg, str) and arg.startswith("container."): if isinstance(arg, str) and arg.startswith("container."):
injection = self._resolve_provider(arg[len("container.") :]) injection = self._resolve_provider(arg[len("container."):])
# TODO: refactoring # TODO: refactoring
if isinstance(arg, dict): if isinstance(arg, dict):
@ -132,23 +125,16 @@ class SchemaProcessorV1:
provider_type = _get_provider_cls(arg.get("provider")) provider_type = _get_provider_cls(arg.get("provider"))
provides = arg.get("provides") provides = arg.get("provides")
if provides: if provides:
if isinstance(provides, str) and provides.startswith( if isinstance(provides, str) and provides.startswith("container."):
"container." provides = self._resolve_provider(provides[len("container."):])
):
provides = self._resolve_provider(
provides[len("container.") :]
)
else: else:
provides = _import_string(provides) provides = _import_string(provides)
provider_args.append(provides) provider_args.append(provides)
for provider_arg in arg.get("args", []): for provider_arg in arg.get("args", []):
if isinstance( if isinstance(provider_arg, str) \
provider_arg, str and provider_arg.startswith("container."):
) and provider_arg.startswith("container."):
provider_args.append( provider_args.append(
self._resolve_provider( self._resolve_provider(provider_arg[len("container."):]),
provider_arg[len("container.") :]
),
) )
injection = provider_type(*provider_args) injection = provider_type(*provider_args)
@ -172,7 +158,7 @@ class SchemaProcessorV1:
for segment in segments[1:]: for segment in segments[1:]:
parentheses = "" parentheses = ""
if "(" in segment and ")" in segment: if "(" in segment and ")" in segment:
parentheses = segment[segment.find("(") : segment.rfind(")") + 1] parentheses = segment[segment.find("("):segment.rfind(")")+1]
segment = segment.replace(parentheses, "") segment = segment.replace(parentheses, "")
try: try:
@ -204,12 +190,10 @@ def _get_provider_cls(provider_cls_name: str) -> Type[providers.Provider]:
if custom_provider_type: if custom_provider_type:
return custom_provider_type return custom_provider_type
raise SchemaError(f'Undefined provider class "{provider_cls_name}"') raise SchemaError(f"Undefined provider class \"{provider_cls_name}\"")
def _fetch_provider_cls_from_std( def _fetch_provider_cls_from_std(provider_cls_name: str) -> Optional[Type[providers.Provider]]:
provider_cls_name: str,
) -> Optional[Type[providers.Provider]]:
return getattr(providers, provider_cls_name, None) return getattr(providers, provider_cls_name, None)
@ -217,16 +201,12 @@ def _import_provider_cls(provider_cls_name: str) -> Optional[Type[providers.Prov
try: try:
cls = _import_string(provider_cls_name) cls = _import_string(provider_cls_name)
except (ImportError, ValueError) as exception: except (ImportError, ValueError) as exception:
raise SchemaError( raise SchemaError(f"Can not import provider \"{provider_cls_name}\"") from exception
f'Can not import provider "{provider_cls_name}"'
) from exception
except AttributeError: except AttributeError:
return None return None
else: else:
if isinstance(cls, type) and not issubclass(cls, providers.Provider): if isinstance(cls, type) and not issubclass(cls, providers.Provider):
raise SchemaError( raise SchemaError(f"Provider class \"{cls}\" is not a subclass of providers base class")
f'Provider class "{cls}" is not a subclass of providers base class'
)
return cls return cls

View File

@ -1,28 +1,34 @@
"""Wiring module.""" """Wiring module."""
import functools import functools
import inspect
import importlib import importlib
import importlib.machinery import importlib.machinery
import inspect
import pkgutil import pkgutil
import warnings
import sys import sys
from types import ModuleType from types import ModuleType
from typing import ( from typing import (
Any, Optional,
Callable,
Dict,
Generic,
Iterable, Iterable,
Iterator, Iterator,
Optional, Callable,
Set, Any,
Tuple, Tuple,
Type, Dict,
Generic,
TypeVar, TypeVar,
Type,
Union, Union,
Set,
cast, cast,
) )
if sys.version_info < (3, 7):
from typing import GenericMeta
else:
class GenericMeta(type):
...
# Hotfix, see: https://github.com/ets-labs/python-dependency-injector/issues/362 # Hotfix, see: https://github.com/ets-labs/python-dependency-injector/issues/362
if sys.version_info >= (3, 9): if sys.version_info >= (3, 9):
@ -30,21 +36,6 @@ if sys.version_info >= (3, 9):
else: else:
GenericAlias = None GenericAlias = None
if sys.version_info >= (3, 9):
from typing import Annotated, get_args, get_origin
else:
try:
from typing_extensions import Annotated, get_args, get_origin
except ImportError:
Annotated = object()
# For preventing NameError. Never executes
def get_args(hint):
return ()
def get_origin(tp):
return None
try: try:
import fastapi.params import fastapi.params
@ -66,6 +57,13 @@ except ImportError:
from . import providers from . import providers
if sys.version_info[:2] == (3, 5):
warnings.warn(
"Dependency Injector will drop support of Python 3.5 after Jan 1st of 2022. "
"This does not mean that there will be any immediate breaking changes, "
"but tests will no longer be executed on Python 3.5, and bugs will not be addressed.",
category=DeprecationWarning,
)
__all__ = ( __all__ = (
"wire", "wire",
@ -101,9 +99,7 @@ class PatchedRegistry:
def register_callable(self, patched: "PatchedCallable") -> None: def register_callable(self, patched: "PatchedCallable") -> None:
self._callables[patched.patched] = patched self._callables[patched.patched] = patched
def get_callables_from_module( def get_callables_from_module(self, module: ModuleType) -> Iterator[Callable[..., Any]]:
self, module: ModuleType
) -> Iterator[Callable[..., Any]]:
for patched_callable in self._callables.values(): for patched_callable in self._callables.values():
if not patched_callable.is_in_module(module): if not patched_callable.is_in_module(module):
continue continue
@ -118,9 +114,7 @@ class PatchedRegistry:
def register_attribute(self, patched: "PatchedAttribute") -> None: def register_attribute(self, patched: "PatchedAttribute") -> None:
self._attributes.add(patched) self._attributes.add(patched)
def get_attributes_from_module( def get_attributes_from_module(self, module: ModuleType) -> Iterator["PatchedAttribute"]:
self, module: ModuleType
) -> Iterator["PatchedAttribute"]:
for attribute in self._attributes: for attribute in self._attributes:
if not attribute.is_in_module(module): if not attribute.is_in_module(module):
continue continue
@ -145,11 +139,11 @@ class PatchedCallable:
) )
def __init__( def __init__(
self, self,
patched: Optional[Callable[..., Any]] = None, patched: Optional[Callable[..., Any]] = None,
original: Optional[Callable[..., Any]] = None, original: Optional[Callable[..., Any]] = None,
reference_injections: Optional[Dict[Any, Any]] = None, reference_injections: Optional[Dict[Any, Any]] = None,
reference_closing: Optional[Dict[Any, Any]] = None, reference_closing: Optional[Dict[Any, Any]] = None,
) -> None: ) -> None:
self.patched = patched self.patched = patched
self.original = original self.original = original
@ -220,21 +214,18 @@ class ProvidersMap:
) )
def resolve_provider( def resolve_provider(
self, self,
provider: Union[providers.Provider, str], provider: Union[providers.Provider, str],
modifier: Optional["Modifier"] = None, modifier: Optional["Modifier"] = None,
) -> Optional[providers.Provider]: ) -> Optional[providers.Provider]:
if isinstance(provider, providers.Delegate): if isinstance(provider, providers.Delegate):
return self._resolve_delegate(provider) return self._resolve_delegate(provider)
elif isinstance( elif isinstance(provider, (
provider, providers.ProvidedInstance,
( providers.AttributeGetter,
providers.ProvidedInstance, providers.ItemGetter,
providers.AttributeGetter, providers.MethodCaller,
providers.ItemGetter, )):
providers.MethodCaller,
),
):
return self._resolve_provided_instance(provider) return self._resolve_provided_instance(provider)
elif isinstance(provider, providers.ConfigurationOption): elif isinstance(provider, providers.ConfigurationOption):
return self._resolve_config_option(provider) return self._resolve_config_option(provider)
@ -246,9 +237,9 @@ class ProvidersMap:
return self._resolve_provider(provider) return self._resolve_provider(provider)
def _resolve_string_id( def _resolve_string_id(
self, self,
id: str, id: str,
modifier: Optional["Modifier"] = None, modifier: Optional["Modifier"] = None,
) -> Optional[providers.Provider]: ) -> Optional[providers.Provider]:
if id == self.CONTAINER_STRING_ID: if id == self.CONTAINER_STRING_ID:
return self._container.__self__ return self._container.__self__
@ -265,19 +256,16 @@ class ProvidersMap:
return provider return provider
def _resolve_provided_instance( def _resolve_provided_instance(
self, self,
original: providers.Provider, original: providers.Provider,
) -> Optional[providers.Provider]: ) -> Optional[providers.Provider]:
modifiers = [] modifiers = []
while isinstance( while isinstance(original, (
original,
(
providers.ProvidedInstance, providers.ProvidedInstance,
providers.AttributeGetter, providers.AttributeGetter,
providers.ItemGetter, providers.ItemGetter,
providers.MethodCaller, providers.MethodCaller,
), )):
):
modifiers.insert(0, original) modifiers.insert(0, original)
original = original.provides original = original.provides
@ -301,8 +289,8 @@ class ProvidersMap:
return new return new
def _resolve_delegate( def _resolve_delegate(
self, self,
original: providers.Delegate, original: providers.Delegate,
) -> Optional[providers.Provider]: ) -> Optional[providers.Provider]:
provider = self._resolve_provider(original.provides) provider = self._resolve_provider(original.provides)
if provider: if provider:
@ -310,9 +298,9 @@ class ProvidersMap:
return provider return provider
def _resolve_config_option( def _resolve_config_option(
self, self,
original: providers.ConfigurationOption, original: providers.ConfigurationOption,
as_: Any = None, as_: Any = None,
) -> Optional[providers.Provider]: ) -> Optional[providers.Provider]:
original_root = original.root original_root = original.root
new = self._resolve_provider(original_root) new = self._resolve_provider(original_root)
@ -336,8 +324,8 @@ class ProvidersMap:
return new return new
def _resolve_provider( def _resolve_provider(
self, self,
original: providers.Provider, original: providers.Provider,
) -> Optional[providers.Provider]: ) -> Optional[providers.Provider]:
try: try:
return self._map[original] return self._map[original]
@ -346,9 +334,9 @@ class ProvidersMap:
@classmethod @classmethod
def _create_providers_map( def _create_providers_map(
cls, cls,
current_container: Container, current_container: Container,
original_container: Container, original_container: Container,
) -> Dict[providers.Provider, providers.Provider]: ) -> Dict[providers.Provider, providers.Provider]:
current_providers = current_container.providers current_providers = current_container.providers
current_providers["__self__"] = current_container.__self__ current_providers["__self__"] = current_container.__self__
@ -361,9 +349,8 @@ class ProvidersMap:
original_provider = original_providers[provider_name] original_provider = original_providers[provider_name]
providers_map[original_provider] = current_provider providers_map[original_provider] = current_provider
if isinstance(current_provider, providers.Container) and isinstance( if isinstance(current_provider, providers.Container) \
original_provider, providers.Container and isinstance(original_provider, providers.Container):
):
subcontainer_map = cls._create_providers_map( subcontainer_map = cls._create_providers_map(
current_container=current_provider.container, current_container=current_provider.container,
original_container=original_provider.container, original_container=original_provider.container,
@ -389,21 +376,19 @@ class InspectFilter:
return werkzeug and isinstance(instance, werkzeug.local.LocalProxy) return werkzeug and isinstance(instance, werkzeug.local.LocalProxy)
def _is_starlette_request_cls(self, instance: object) -> bool: def _is_starlette_request_cls(self, instance: object) -> bool:
return ( return starlette \
starlette and isinstance(instance, type) \
and isinstance(instance, type) and _safe_is_subclass(instance, starlette.requests.Request)
and _safe_is_subclass(instance, starlette.requests.Request)
)
def _is_builtin(self, instance: object) -> bool: def _is_builtin(self, instance: object) -> bool:
return inspect.isbuiltin(instance) return inspect.isbuiltin(instance)
def wire( # noqa: C901 def wire( # noqa: C901
container: Container, container: Container,
*, *,
modules: Optional[Iterable[ModuleType]] = None, modules: Optional[Iterable[ModuleType]] = None,
packages: Optional[Iterable[ModuleType]] = None, packages: Optional[Iterable[ModuleType]] = None,
) -> None: ) -> None:
"""Wire container providers with provided packages and modules.""" """Wire container providers with provided packages and modules."""
modules = [*modules] if modules else [] modules = [*modules] if modules else []
@ -433,22 +418,18 @@ def wire( # noqa: C901
else: else:
for cls_member_name, cls_member in cls_members: for cls_member_name, cls_member in cls_members:
if _is_marker(cls_member): if _is_marker(cls_member):
_patch_attribute( _patch_attribute(cls, cls_member_name, cls_member, providers_map)
cls, cls_member_name, cls_member, providers_map
)
elif _is_method(cls_member): elif _is_method(cls_member):
_patch_method( _patch_method(cls, cls_member_name, cls_member, providers_map)
cls, cls_member_name, cls_member, providers_map
)
for patched in _patched_registry.get_callables_from_module(module): for patched in _patched_registry.get_callables_from_module(module):
_bind_injections(patched, providers_map) _bind_injections(patched, providers_map)
def unwire( # noqa: C901 def unwire( # noqa: C901
*, *,
modules: Optional[Iterable[ModuleType]] = None, modules: Optional[Iterable[ModuleType]] = None,
packages: Optional[Iterable[ModuleType]] = None, packages: Optional[Iterable[ModuleType]] = None,
) -> None: ) -> None:
"""Wire provided packages and modules with previous wired providers.""" """Wire provided packages and modules with previous wired providers."""
modules = [*modules] if modules else [] modules = [*modules] if modules else []
@ -462,9 +443,7 @@ def unwire( # noqa: C901
if inspect.isfunction(member): if inspect.isfunction(member):
_unpatch(module, name, member) _unpatch(module, name, member)
elif inspect.isclass(member): elif inspect.isclass(member):
for method_name, method in inspect.getmembers( for method_name, method in inspect.getmembers(member, inspect.isfunction):
member, inspect.isfunction
):
_unpatch(member, method_name, method) _unpatch(member, method_name, method)
for patched in _patched_registry.get_callables_from_module(module): for patched in _patched_registry.get_callables_from_module(module):
@ -483,10 +462,10 @@ def inject(fn: F) -> F:
def _patch_fn( def _patch_fn(
module: ModuleType, module: ModuleType,
name: str, name: str,
fn: Callable[..., Any], fn: Callable[..., Any],
providers_map: ProvidersMap, providers_map: ProvidersMap,
) -> None: ) -> None:
if not _is_patched(fn): if not _is_patched(fn):
reference_injections, reference_closing = _fetch_reference_injections(fn) reference_injections, reference_closing = _fetch_reference_injections(fn)
@ -500,16 +479,14 @@ def _patch_fn(
def _patch_method( def _patch_method(
cls: Type, cls: Type,
name: str, name: str,
method: Callable[..., Any], method: Callable[..., Any],
providers_map: ProvidersMap, providers_map: ProvidersMap,
) -> None: ) -> None:
if ( if hasattr(cls, "__dict__") \
hasattr(cls, "__dict__") and name in cls.__dict__ \
and name in cls.__dict__ and isinstance(cls.__dict__[name], (classmethod, staticmethod)):
and isinstance(cls.__dict__[name], (classmethod, staticmethod))
):
method = cls.__dict__[name] method = cls.__dict__[name]
fn = method.__func__ fn = method.__func__
else: else:
@ -530,15 +507,13 @@ def _patch_method(
def _unpatch( def _unpatch(
module: ModuleType, module: ModuleType,
name: str, name: str,
fn: Callable[..., Any], fn: Callable[..., Any],
) -> None: ) -> None:
if ( if hasattr(module, "__dict__") \
hasattr(module, "__dict__") and name in module.__dict__ \
and name in module.__dict__ and isinstance(module.__dict__[name], (classmethod, staticmethod)):
and isinstance(module.__dict__[name], (classmethod, staticmethod))
):
method = module.__dict__[name] method = module.__dict__[name]
fn = method.__func__ fn = method.__func__
@ -549,10 +524,10 @@ def _unpatch(
def _patch_attribute( def _patch_attribute(
member: Any, member: Any,
name: str, name: str,
marker: "_Marker", marker: "_Marker",
providers_map: ProvidersMap, providers_map: ProvidersMap,
) -> None: ) -> None:
provider = providers_map.resolve_provider(marker.provider, marker.modifier) provider = providers_map.resolve_provider(marker.provider, marker.modifier)
if provider is None: if provider is None:
@ -573,33 +548,16 @@ def _unpatch_attribute(patched: PatchedAttribute) -> None:
setattr(patched.member, patched.name, patched.marker) setattr(patched.member, patched.name, patched.marker)
def _extract_marker(parameter: inspect.Parameter) -> Optional["_Marker"]:
if get_origin(parameter.annotation) is Annotated:
marker = get_args(parameter.annotation)[1]
else:
marker = parameter.default
if not isinstance(marker, _Marker) and not _is_fastapi_depends(marker):
return None
if _is_fastapi_depends(marker):
marker = marker.dependency
if not isinstance(marker, _Marker):
return None
return marker
def _fetch_reference_injections( # noqa: C901 def _fetch_reference_injections( # noqa: C901
fn: Callable[..., Any], fn: Callable[..., Any],
) -> Tuple[Dict[str, Any], Dict[str, Any]]: ) -> Tuple[Dict[str, Any], Dict[str, Any]]:
# Hotfix, see: # Hotfix, see:
# - https://github.com/ets-labs/python-dependency-injector/issues/362 # - https://github.com/ets-labs/python-dependency-injector/issues/362
# - https://github.com/ets-labs/python-dependency-injector/issues/398 # - https://github.com/ets-labs/python-dependency-injector/issues/398
if GenericAlias and any( if GenericAlias and any((
(fn is GenericAlias, getattr(fn, "__func__", None) is GenericAlias) fn is GenericAlias,
): getattr(fn, "__func__", None) is GenericAlias
)):
fn = fn.__init__ fn = fn.__init__
try: try:
@ -615,11 +573,18 @@ def _fetch_reference_injections( # noqa: C901
injections = {} injections = {}
closing = {} closing = {}
for parameter_name, parameter in signature.parameters.items(): for parameter_name, parameter in signature.parameters.items():
marker = _extract_marker(parameter) if not isinstance(parameter.default, _Marker) \
and not _is_fastapi_depends(parameter.default):
if marker is None:
continue continue
marker = parameter.default
if _is_fastapi_depends(marker):
marker = marker.dependency
if not isinstance(marker, _Marker):
continue
if isinstance(marker, Closing): if isinstance(marker, Closing):
marker = marker.provider marker = marker.provider
closing[parameter_name] = marker closing[parameter_name] = marker
@ -628,19 +593,20 @@ def _fetch_reference_injections( # noqa: C901
return injections, closing return injections, closing
def _locate_dependent_closing_args( def _locate_dependent_closing_args(provider: providers.Provider) -> Dict[str, providers.Provider]:
provider: providers.Provider, closing_deps: Dict[str, providers.Provider] if not hasattr(provider, "args"):
) -> Dict[str, providers.Provider]: return {}
for arg in [
*getattr(provider, "args", []),
*getattr(provider, "kwargs", {}).values(),
]:
if not isinstance(arg, providers.Provider):
continue
if isinstance(arg, providers.Resource):
closing_deps[str(id(arg))] = arg
_locate_dependent_closing_args(arg, closing_deps) closing_deps = {}
for arg in provider.args:
if not isinstance(arg, providers.Provider) or not hasattr(arg, "args"):
continue
if not arg.args and isinstance(arg, providers.Resource):
return {str(id(arg)): arg}
else:
closing_deps += _locate_dependent_closing_args(arg)
return closing_deps
def _bind_injections(fn: Callable[..., Any], providers_map: ProvidersMap) -> None: def _bind_injections(fn: Callable[..., Any], providers_map: ProvidersMap) -> None:
@ -664,8 +630,7 @@ def _bind_injections(fn: Callable[..., Any], providers_map: ProvidersMap) -> Non
if injection in patched_callable.reference_closing: if injection in patched_callable.reference_closing:
patched_callable.add_closing(injection, provider) patched_callable.add_closing(injection, provider)
deps = {} deps = _locate_dependent_closing_args(provider)
_locate_dependent_closing_args(provider, deps)
for key, dep in deps.items(): for key, dep in deps.items():
patched_callable.add_closing(key, dep) patched_callable.add_closing(key, dep)
@ -682,8 +647,8 @@ def _fetch_modules(package):
if not hasattr(package, "__path__") or not hasattr(package, "__name__"): if not hasattr(package, "__path__") or not hasattr(package, "__name__"):
return modules return modules
for module_info in pkgutil.walk_packages( for module_info in pkgutil.walk_packages(
path=package.__path__, path=package.__path__,
prefix=package.__name__ + ".", prefix=package.__name__ + ".",
): ):
module = importlib.import_module(module_info.name) module = importlib.import_module(module_info.name)
modules.append(module) modules.append(module)
@ -699,9 +664,9 @@ def _is_marker(member) -> bool:
def _get_patched( def _get_patched(
fn: F, fn: F,
reference_injections: Dict[Any, Any], reference_injections: Dict[Any, Any],
reference_closing: Dict[Any, Any], reference_closing: Dict[Any, Any],
) -> F: ) -> F:
patched_object = PatchedCallable( patched_object = PatchedCallable(
original=fn, original=fn,
@ -729,11 +694,9 @@ def _is_patched(fn) -> bool:
def _is_declarative_container(instance: Any) -> bool: def _is_declarative_container(instance: Any) -> bool:
return ( return (isinstance(instance, type)
isinstance(instance, type) and getattr(instance, "__IS_CONTAINER__", False) is True
and getattr(instance, "__IS_CONTAINER__", False) is True and getattr(instance, "declarative_parent", None) is None)
and getattr(instance, "declarative_parent", None) is None
)
def _safe_is_subclass(instance: Any, cls: Type) -> bool: def _safe_is_subclass(instance: Any, cls: Type) -> bool:
@ -746,10 +709,11 @@ def _safe_is_subclass(instance: Any, cls: Type) -> bool:
class Modifier: class Modifier:
def modify( def modify(
self, self,
provider: providers.ConfigurationOption, provider: providers.ConfigurationOption,
providers_map: ProvidersMap, providers_map: ProvidersMap,
) -> providers.Provider: ... ) -> providers.Provider:
...
class TypeModifier(Modifier): class TypeModifier(Modifier):
@ -758,9 +722,9 @@ class TypeModifier(Modifier):
self.type_ = type_ self.type_ = type_
def modify( def modify(
self, self,
provider: providers.ConfigurationOption, provider: providers.ConfigurationOption,
providers_map: ProvidersMap, providers_map: ProvidersMap,
) -> providers.Provider: ) -> providers.Provider:
return provider.as_(self.type_) return provider.as_(self.type_)
@ -798,9 +762,9 @@ class RequiredModifier(Modifier):
return self return self
def modify( def modify(
self, self,
provider: providers.ConfigurationOption, provider: providers.ConfigurationOption,
providers_map: ProvidersMap, providers_map: ProvidersMap,
) -> providers.Provider: ) -> providers.Provider:
provider = provider.required() provider = provider.required()
if self.type_modifier: if self.type_modifier:
@ -819,9 +783,9 @@ class InvariantModifier(Modifier):
self.id = id self.id = id
def modify( def modify(
self, self,
provider: providers.ConfigurationOption, provider: providers.ConfigurationOption,
providers_map: ProvidersMap, providers_map: ProvidersMap,
) -> providers.Provider: ) -> providers.Provider:
invariant_segment = providers_map.resolve_provider(self.id) invariant_segment = providers_map.resolve_provider(self.id)
return provider[invariant_segment] return provider[invariant_segment]
@ -854,9 +818,9 @@ class ProvidedInstance(Modifier):
return self return self
def modify( def modify(
self, self,
provider: providers.Provider, provider: providers.Provider,
providers_map: ProvidersMap, providers_map: ProvidersMap,
) -> providers.Provider: ) -> providers.Provider:
provider = provider.provided provider = provider.provided
for type_, value in self.segments: for type_, value in self.segments:
@ -874,14 +838,22 @@ def provided() -> ProvidedInstance:
return ProvidedInstance() return ProvidedInstance()
class _Marker(Generic[T]): class ClassGetItemMeta(GenericMeta):
def __getitem__(cls, item):
# Spike for Python 3.6
if isinstance(item, tuple):
return cls(*item)
return cls(item)
class _Marker(Generic[T], metaclass=ClassGetItemMeta):
__IS_MARKER__ = True __IS_MARKER__ = True
def __init__( def __init__(
self, self,
provider: Union[providers.Provider, Container, str], provider: Union[providers.Provider, Container, str],
modifier: Optional[Modifier] = None, modifier: Optional[Modifier] = None,
) -> None: ) -> None:
if _is_declarative_container(provider): if _is_declarative_container(provider):
provider = provider.__self__ provider = provider.__self__
@ -897,13 +869,16 @@ class _Marker(Generic[T]):
return self return self
class Provide(_Marker): ... class Provide(_Marker):
...
class Provider(_Marker): ... class Provider(_Marker):
...
class Closing(_Marker): ... class Closing(_Marker):
...
class AutoLoader: class AutoLoader:
@ -953,7 +928,8 @@ class AutoLoader:
super().exec_module(module) super().exec_module(module)
loader.wire_module(module) loader.wire_module(module)
class ExtensionFileLoader(importlib.machinery.ExtensionFileLoader): ... class ExtensionFileLoader(importlib.machinery.ExtensionFileLoader):
...
loader_details = [ loader_details = [
(SourcelessFileLoader, importlib.machinery.BYTECODE_SUFFIXES), (SourcelessFileLoader, importlib.machinery.BYTECODE_SUFFIXES),
@ -1006,7 +982,7 @@ _inspect_filter = InspectFilter()
_loader = AutoLoader() _loader = AutoLoader()
# Optimizations # Optimizations
from ._cwiring import _sync_inject # noqa from ._cwiring import _get_sync_patched # noqa
from ._cwiring import _async_inject # noqa from ._cwiring import _async_inject # noqa
@ -1022,18 +998,4 @@ def _get_async_patched(fn: F, patched: PatchedCallable) -> F:
patched.injections, patched.injections,
patched.closing, patched.closing,
) )
return _patched
return cast(F, _patched)
def _get_sync_patched(fn: F, patched: PatchedCallable) -> F:
@functools.wraps(fn)
def _patched(*args, **kwargs):
return _sync_inject(
fn,
args,
kwargs,
patched.injections,
patched.closing,
)
return cast(F, _patched)

View File

@ -0,0 +1,10 @@
[pytest]
testpaths = tests/unit/
python_files = test_*_py2_py3.py
asyncio_mode = auto
filterwarnings =
ignore:Module \"dependency_injector.ext.aiohttp\" is deprecated since version 4\.0\.0:DeprecationWarning
ignore:Module \"dependency_injector.ext.flask\" is deprecated since version 4\.0\.0:DeprecationWarning
ignore:Please use \`.*?\` from the \`scipy.*?\`(.*?)namespace is deprecated\.:DeprecationWarning
ignore:The \`scipy(.*?)\` namespace is deprecated(.*):DeprecationWarning
ignore:ssl\.PROTOCOL_TLS is deprecated:DeprecationWarning:botocore.*

View File

@ -0,0 +1,10 @@
[pytest]
testpaths = tests/unit/
python_files = test_*_py3.py
asyncio_mode = auto
filterwarnings =
ignore:Module \"dependency_injector.ext.aiohttp\" is deprecated since version 4\.0\.0:DeprecationWarning
ignore:Module \"dependency_injector.ext.flask\" is deprecated since version 4\.0\.0:DeprecationWarning
ignore:Please use \`.*?\` from the \`scipy.*?\`(.*?)namespace is deprecated\.:DeprecationWarning
ignore:The \`scipy(.*?)\` namespace is deprecated(.*):DeprecationWarning
ignore:ssl\.PROTOCOL_TLS is deprecated:DeprecationWarning:botocore.*

View File

@ -2,8 +2,6 @@
testpaths = tests/unit/ testpaths = tests/unit/
python_files = test_*_py3*.py python_files = test_*_py3*.py
asyncio_mode = auto asyncio_mode = auto
markers =
pydantic: Tests with Pydantic as a dependency
filterwarnings = filterwarnings =
ignore:Module \"dependency_injector.ext.aiohttp\" is deprecated since version 4\.0\.0:DeprecationWarning ignore:Module \"dependency_injector.ext.aiohttp\" is deprecated since version 4\.0\.0:DeprecationWarning
ignore:Module \"dependency_injector.ext.flask\" is deprecated since version 4\.0\.0:DeprecationWarning ignore:Module \"dependency_injector.ext.flask\" is deprecated since version 4\.0\.0:DeprecationWarning

View File

@ -34,7 +34,7 @@ kwargs4: Dict[str, Any] = provider4.kwargs
# Test 5: to check the provided instance interface # Test 5: to check the provided instance interface
provider5 = providers.Callable(Animal) provider5 = providers.Callable(Animal)
provided5: Animal = provider5.provided() provided5: providers.ProvidedInstance = provider5.provided
attr_getter5: providers.AttributeGetter = provider5.provided.attr attr_getter5: providers.AttributeGetter = provider5.provided.attr
item_getter5: providers.ItemGetter = provider5.provided["item"] item_getter5: providers.ItemGetter = provider5.provided["item"]
method_caller: providers.MethodCaller = provider5.provided.method.call(123, arg=324) method_caller: providers.MethodCaller = provider5.provided.method.call(123, arg=324)

View File

@ -2,7 +2,7 @@ from pathlib import Path
from typing import Any from typing import Any
from dependency_injector import providers from dependency_injector import providers
from pydantic_settings import BaseSettings as PydanticSettings from pydantic import BaseSettings as PydanticSettings
# Test 1: to check the getattr # Test 1: to check the getattr

View File

@ -34,7 +34,7 @@ provider5 = providers.Dict(
a1=providers.Factory(object), a1=providers.Factory(object),
a2=providers.Factory(object), a2=providers.Factory(object),
) )
provided5: dict[Any, Any] = provider5.provided() provided5: providers.ProvidedInstance = provider5.provided
# Test 6: to check the return type with await # Test 6: to check the return type with await

View File

@ -37,7 +37,7 @@ attributes4: Dict[str, Any] = provider4.attributes
# Test 5: to check the provided instance interface # Test 5: to check the provided instance interface
provider5 = providers.Factory(Animal) provider5 = providers.Factory(Animal)
provided5: Animal = provider5.provided() provided5: providers.ProvidedInstance = provider5.provided
attr_getter5: providers.AttributeGetter = provider5.provided.attr attr_getter5: providers.AttributeGetter = provider5.provided.attr
item_getter5: providers.ItemGetter = provider5.provided["item"] item_getter5: providers.ItemGetter = provider5.provided["item"]
method_caller5: providers.MethodCaller = provider5.provided.method.call(123, arg=324) method_caller5: providers.MethodCaller = provider5.provided.method.call(123, arg=324)

View File

@ -23,7 +23,7 @@ provider3 = providers.List(
providers.Factory(object), providers.Factory(object),
providers.Factory(object), providers.Factory(object),
) )
provided3: List[Any] = provider3.provided() provided3: providers.ProvidedInstance = provider3.provided
attr_getter3: providers.AttributeGetter = provider3.provided.attr attr_getter3: providers.AttributeGetter = provider3.provided.attr
item_getter3: providers.ItemGetter = provider3.provided["item"] item_getter3: providers.ItemGetter = provider3.provided["item"]
method_caller3: providers.MethodCaller = provider3.provided.method.call(123, arg=324) method_caller3: providers.MethodCaller = provider3.provided.method.call(123, arg=324)

View File

@ -9,7 +9,7 @@ var1: int = provider1()
# Test 2: to check the provided instance interface # Test 2: to check the provided instance interface
provider2 = providers.Object(int) provider2 = providers.Object(int)
provided2: Type[int] = provider2.provided() provided2: providers.ProvidedInstance = provider2.provided
attr_getter2: providers.AttributeGetter = provider2.provided.attr attr_getter2: providers.AttributeGetter = provider2.provided.attr
item_getter2: providers.ItemGetter = provider2.provided["item"] item_getter2: providers.ItemGetter = provider2.provided["item"]
method_caller2: providers.MethodCaller = provider2.provided.method.call(123, arg=324) method_caller2: providers.MethodCaller = provider2.provided.method.call(123, arg=324)

View File

@ -3,8 +3,7 @@ from dependency_injector import providers
# Test 1: to check .provided attribute # Test 1: to check .provided attribute
provider1: providers.Provider[int] = providers.Object(1) provider1: providers.Provider[int] = providers.Object(1)
provided: int = provider1.provided() provided: providers.ProvidedInstance = provider1.provided
provider1_delegate: providers.Provider[int] = provider1.provider
# Test 2: to check async mode API # Test 2: to check async mode API
provider2: providers.Provider = providers.Provider() provider2: providers.Provider = providers.Provider()

View File

@ -37,7 +37,7 @@ attributes4: Dict[str, Any] = provider4.attributes
# Test 5: to check the provided instance interface # Test 5: to check the provided instance interface
provider5 = providers.Singleton(Animal) provider5 = providers.Singleton(Animal)
provided5: Animal = provider5.provided() provided5: providers.ProvidedInstance = provider5.provided
attr_getter5: providers.AttributeGetter = provider5.provided.attr attr_getter5: providers.AttributeGetter = provider5.provided.attr
item_getter5: providers.ItemGetter = provider5.provided["item"] item_getter5: providers.ItemGetter = provider5.provided["item"]
method_caller5: providers.MethodCaller = provider5.provided.method.call(123, arg=324) method_caller5: providers.MethodCaller = provider5.provided.method.call(123, arg=324)

View File

@ -1,41 +0,0 @@
from typing import AsyncIterator, Iterator
from unittest.mock import ANY
from pytest import mark
from dependency_injector.containers import DeclarativeContainer
from dependency_injector.ext.starlette import Lifespan
from dependency_injector.providers import Resource
class TestLifespan:
@mark.parametrize("sync", [False, True])
@mark.asyncio
async def test_context_manager(self, sync: bool) -> None:
init, shutdown = False, False
def sync_resource() -> Iterator[None]:
nonlocal init, shutdown
init = True
yield
shutdown = True
async def async_resource() -> AsyncIterator[None]:
nonlocal init, shutdown
init = True
yield
shutdown = True
class Container(DeclarativeContainer):
x = Resource(sync_resource if sync else async_resource)
container = Container()
lifespan = Lifespan(container)
async with lifespan(ANY) as scope:
assert scope is None
assert init
assert shutdown

View File

@ -5,23 +5,6 @@ import os
from pytest import mark, raises from pytest import mark, raises
def test_no_env_variable_interpolation(config, ini_config_file_3):
config.from_ini(ini_config_file_3, envs_required=None)
assert config() == {
"section1": {
"value1": "${CONFIG_TEST_ENV}",
"value2": "${CONFIG_TEST_PATH}/path",
},
}
assert config.section1() == {
"value1": "${CONFIG_TEST_ENV}",
"value2": "${CONFIG_TEST_PATH}/path",
}
assert config.section1.value1() == "${CONFIG_TEST_ENV}"
assert config.section1.value2() == "${CONFIG_TEST_PATH}/path"
def test_env_variable_interpolation(config, ini_config_file_3): def test_env_variable_interpolation(config, ini_config_file_3):
config.from_ini(ini_config_file_3) config.from_ini(ini_config_file_3)

View File

@ -6,23 +6,6 @@ import os
from pytest import mark, raises from pytest import mark, raises
def test_no_env_variable_interpolation(config, json_config_file_3):
config.from_json(json_config_file_3, envs_required=None)
assert config() == {
"section1": {
"value1": "${CONFIG_TEST_ENV}",
"value2": "${CONFIG_TEST_PATH}/path",
},
}
assert config.section1() == {
"value1": "${CONFIG_TEST_ENV}",
"value2": "${CONFIG_TEST_PATH}/path",
}
assert config.section1.value1() == "${CONFIG_TEST_ENV}"
assert config.section1.value2() == "${CONFIG_TEST_PATH}/path"
def test_env_variable_interpolation(config, json_config_file_3): def test_env_variable_interpolation(config, json_config_file_3):
config.from_json(json_config_file_3) config.from_json(json_config_file_3)

View File

@ -1,60 +1,41 @@
"""Configuration.from_pydantic() tests.""" """Configuration.from_pydantic() tests."""
from pydantic import BaseModel import pydantic
from dependency_injector import providers, errors
try:
from pydantic_settings import (
BaseSettings, # type: ignore[import-not-found,unused-ignore]
)
except ImportError:
try:
from pydantic import BaseSettings # type: ignore[no-redef,unused-ignore]
except ImportError:
class BaseSettings: # type: ignore[no-redef]
"""No-op fallback"""
from pytest import fixture, mark, raises from pytest import fixture, mark, raises
from dependency_injector import errors, providers
pytestmark = mark.pydantic class Section11(pydantic.BaseModel):
value1 = 1
class Section11(BaseModel): class Section12(pydantic.BaseModel):
value1: int = 1 value2 = 2
class Section12(BaseModel): class Settings1(pydantic.BaseSettings):
value2: int = 2 section1 = Section11()
section2 = Section12()
class Settings1(BaseSettings): class Section21(pydantic.BaseModel):
section1: Section11 = Section11() value1 = 11
section2: Section12 = Section12() value11 = 11
class Section21(BaseModel): class Section3(pydantic.BaseModel):
value1: int = 11 value3 = 3
value11: int = 11
class Section3(BaseModel): class Settings2(pydantic.BaseSettings):
value3: int = 3 section1 = Section21()
section3 = Section3()
class Settings2(BaseSettings):
section1: Section21 = Section21()
section3: Section3 = Section3()
@fixture @fixture
def no_pydantic_module_installed(): def no_pydantic_module_installed():
has_pydantic_settings = providers.has_pydantic_settings providers.pydantic = None
providers.has_pydantic_settings = False
yield yield
providers.has_pydantic_settings = has_pydantic_settings providers.pydantic = pydantic
def test(config): def test(config):
@ -101,70 +82,66 @@ def test_merge(config):
def test_empty_settings(config): def test_empty_settings(config):
config.from_pydantic(BaseSettings()) config.from_pydantic(pydantic.BaseSettings())
assert config() == {} assert config() == {}
@mark.parametrize("config_type", ["strict"]) @mark.parametrize("config_type", ["strict"])
def test_empty_settings_strict_mode(config): def test_empty_settings_strict_mode(config):
with raises(ValueError): with raises(ValueError):
config.from_pydantic(BaseSettings()) config.from_pydantic(pydantic.BaseSettings())
def test_option_empty_settings(config): def test_option_empty_settings(config):
config.option.from_pydantic(BaseSettings()) config.option.from_pydantic(pydantic.BaseSettings())
assert config.option() == {} assert config.option() == {}
@mark.parametrize("config_type", ["strict"]) @mark.parametrize("config_type", ["strict"])
def test_option_empty_settings_strict_mode(config): def test_option_empty_settings_strict_mode(config):
with raises(ValueError): with raises(ValueError):
config.option.from_pydantic(BaseSettings()) config.option.from_pydantic(pydantic.BaseSettings())
def test_required_empty_settings(config): def test_required_empty_settings(config):
with raises(ValueError): with raises(ValueError):
config.from_pydantic(BaseSettings(), required=True) config.from_pydantic(pydantic.BaseSettings(), required=True)
def test_required_option_empty_settings(config): def test_required_option_empty_settings(config):
with raises(ValueError): with raises(ValueError):
config.option.from_pydantic(BaseSettings(), required=True) config.option.from_pydantic(pydantic.BaseSettings(), required=True)
@mark.parametrize("config_type", ["strict"]) @mark.parametrize("config_type", ["strict"])
def test_not_required_empty_settings_strict_mode(config): def test_not_required_empty_settings_strict_mode(config):
config.from_pydantic(BaseSettings(), required=False) config.from_pydantic(pydantic.BaseSettings(), required=False)
assert config() == {} assert config() == {}
@mark.parametrize("config_type", ["strict"]) @mark.parametrize("config_type", ["strict"])
def test_not_required_option_empty_settings_strict_mode(config): def test_not_required_option_empty_settings_strict_mode(config):
config.option.from_pydantic(BaseSettings(), required=False) config.option.from_pydantic(pydantic.BaseSettings(), required=False)
assert config.option() == {} assert config.option() == {}
assert config() == {"option": {}} assert config() == {"option": {}}
def test_not_instance_of_settings(config): def test_not_instance_of_settings(config):
with raises( with raises(errors.Error) as error:
errors.Error,
match=(
r"Unable to recognize settings instance, expect \"pydantic(?:_settings)?\.BaseSettings\", "
r"got {0} instead".format({})
),
):
config.from_pydantic({}) config.from_pydantic({})
assert error.value.args[0] == (
"Unable to recognize settings instance, expect \"pydantic.BaseSettings\", "
"got {0} instead".format({})
)
def test_option_not_instance_of_settings(config): def test_option_not_instance_of_settings(config):
with raises( with raises(errors.Error) as error:
errors.Error,
match=(
r"Unable to recognize settings instance, expect \"pydantic(?:_settings)?\.BaseSettings\", "
"got {0} instead".format({})
),
):
config.option.from_pydantic({}) config.option.from_pydantic({})
assert error.value.args[0] == (
"Unable to recognize settings instance, expect \"pydantic.BaseSettings\", "
"got {0} instead".format({})
)
def test_subclass_instead_of_instance(config): def test_subclass_instead_of_instance(config):
@ -187,25 +164,21 @@ def test_option_subclass_instead_of_instance(config):
@mark.usefixtures("no_pydantic_module_installed") @mark.usefixtures("no_pydantic_module_installed")
def test_no_pydantic_installed(config): def test_no_pydantic_installed(config):
with raises( with raises(errors.Error) as error:
errors.Error,
match=(
r"Unable to load pydantic configuration - pydantic(?:_settings)? is not installed\. "
r"Install pydantic or install Dependency Injector with pydantic extras: "
r"\"pip install dependency-injector\[pydantic2?\]\""
),
):
config.from_pydantic(Settings1()) config.from_pydantic(Settings1())
assert error.value.args[0] == (
"Unable to load pydantic configuration - pydantic is not installed. "
"Install pydantic or install Dependency Injector with pydantic extras: "
"\"pip install dependency-injector[pydantic]\""
)
@mark.usefixtures("no_pydantic_module_installed") @mark.usefixtures("no_pydantic_module_installed")
def test_option_no_pydantic_installed(config): def test_option_no_pydantic_installed(config):
with raises( with raises(errors.Error) as error:
errors.Error,
match=(
r"Unable to load pydantic configuration - pydantic(?:_settings)? is not installed\. "
r"Install pydantic or install Dependency Injector with pydantic extras: "
r"\"pip install dependency-injector\[pydantic2?\]\""
),
):
config.option.from_pydantic(Settings1()) config.option.from_pydantic(Settings1())
assert error.value.args[0] == (
"Unable to load pydantic configuration - pydantic is not installed. "
"Install pydantic or install Dependency Injector with pydantic extras: "
"\"pip install dependency-injector[pydantic]\""
)

View File

@ -6,23 +6,6 @@ import yaml
from pytest import mark, raises from pytest import mark, raises
def test_no_env_variable_interpolation(config, yaml_config_file_3):
config.from_yaml(yaml_config_file_3, envs_required=None)
assert config() == {
"section1": {
"value1": "${CONFIG_TEST_ENV}",
"value2": "${CONFIG_TEST_PATH}/path",
},
}
assert config.section1() == {
"value1": "${CONFIG_TEST_ENV}",
"value2": "${CONFIG_TEST_PATH}/path",
}
assert config.section1.value1() == "${CONFIG_TEST_ENV}"
assert config.section1.value2() == "${CONFIG_TEST_PATH}/path"
def test_env_variable_interpolation(config, yaml_config_file_3): def test_env_variable_interpolation(config, yaml_config_file_3):
config.from_yaml(yaml_config_file_3) config.from_yaml(yaml_config_file_3)

View File

@ -1,52 +1,35 @@
"""Configuration.from_pydantic() tests.""" """Configuration.from_pydantic() tests."""
from pydantic import BaseModel import pydantic
from dependency_injector import providers
try:
from pydantic_settings import (
BaseSettings, # type: ignore[import-not-found,unused-ignore]
)
except ImportError:
try:
from pydantic import BaseSettings # type: ignore[no-redef,unused-ignore]
except ImportError:
class BaseSettings: # type: ignore[no-redef]
"""No-op fallback"""
from pytest import fixture, mark, raises from pytest import fixture, mark, raises
from dependency_injector import providers
pytestmark = mark.pydantic class Section11(pydantic.BaseModel):
class Section11(BaseModel):
value1: int = 1 value1: int = 1
class Section12(BaseModel): class Section12(pydantic.BaseModel):
value2: int = 2 value2: int = 2
class Settings1(BaseSettings): class Settings1(pydantic.BaseSettings):
section1: Section11 = Section11() section1: Section11 = Section11()
section2: Section12 = Section12() section2: Section12 = Section12()
class Section21(BaseModel): class Section21(pydantic.BaseModel):
value1: int = 11 value1: int = 11
value11: int = 11 value11: int = 11
class Section3(BaseModel): class Section3(pydantic.BaseModel):
value3: int = 3 value3: int = 3
class Settings2(BaseSettings): class Settings2(pydantic.BaseSettings):
section1: Section21 = Section21() section1: Section21 = Section21()
section3: Section3 = Section3() section3: Section3= Section3()
@fixture @fixture
@ -103,10 +86,10 @@ def test_copy(config, pydantic_settings_1, pydantic_settings_2):
def test_set_pydantic_settings(config): def test_set_pydantic_settings(config):
class Settings3(BaseSettings): class Settings3(pydantic.BaseSettings):
... ...
class Settings4(BaseSettings): class Settings4(pydantic.BaseSettings):
... ...
settings_3 = Settings3() settings_3 = Settings3()
@ -117,27 +100,27 @@ def test_set_pydantic_settings(config):
def test_file_does_not_exist(config): def test_file_does_not_exist(config):
config.set_pydantic_settings([BaseSettings()]) config.set_pydantic_settings([pydantic.BaseSettings()])
config.load() config.load()
assert config() == {} assert config() == {}
@mark.parametrize("config_type", ["strict"]) @mark.parametrize("config_type", ["strict"])
def test_file_does_not_exist_strict_mode(config): def test_file_does_not_exist_strict_mode(config):
config.set_pydantic_settings([BaseSettings()]) config.set_pydantic_settings([pydantic.BaseSettings()])
with raises(ValueError): with raises(ValueError):
config.load() config.load()
assert config() == {} assert config() == {}
def test_required_file_does_not_exist(config): def test_required_file_does_not_exist(config):
config.set_pydantic_settings([BaseSettings()]) config.set_pydantic_settings([pydantic.BaseSettings()])
with raises(ValueError): with raises(ValueError):
config.load(required=True) config.load(required=True)
@mark.parametrize("config_type", ["strict"]) @mark.parametrize("config_type", ["strict"])
def test_not_required_file_does_not_exist_strict_mode(config): def test_not_required_file_does_not_exist_strict_mode(config):
config.set_pydantic_settings([BaseSettings()]) config.set_pydantic_settings([pydantic.BaseSettings()])
config.load(required=False) config.load(required=False)
assert config() == {} assert config() == {}

View File

@ -1,5 +1,4 @@
"""Coroutine provider tests.""" """Coroutine provider tests."""
import sys
from dependency_injector import providers, errors from dependency_injector import providers, errors
from pytest import mark, raises from pytest import mark, raises
@ -209,17 +208,3 @@ def test_repr():
"<dependency_injector.providers." "<dependency_injector.providers."
"Coroutine({0}) at {1}>".format(repr(example), hex(id(provider))) "Coroutine({0}) at {1}>".format(repr(example), hex(id(provider)))
) )
@mark.skipif(sys.version_info > (3, 15), reason="requires Python<3.16")
def test_asyncio_iscoroutinefunction() -> None:
from asyncio.coroutines import iscoroutinefunction
assert iscoroutinefunction(providers.Coroutine(example))
@mark.skipif(sys.version_info < (3, 12), reason="requires Python>=3.12")
def test_inspect_iscoroutinefunction() -> None:
from inspect import iscoroutinefunction
assert iscoroutinefunction(providers.Coroutine(example))

View File

@ -1,20 +0,0 @@
import pytest
from dependency_injector.containers import Container
from dependency_injector.providers import ThreadLocalSingleton
class FailingClass:
def __init__(self):
raise ValueError("FAILING CLASS")
class TestContainer(Container):
failing_class = ThreadLocalSingleton(FailingClass)
def test_on_failure_value_error_is_raised():
container = TestContainer()
with pytest.raises(ValueError, match="FAILING CLASS"):
container.failing_class()

View File

@ -1,65 +0,0 @@
import sys
from typing import Any, Dict, NoReturn
from pytest import raises
from dependency_injector.errors import NonCopyableArgumentError
from dependency_injector.providers import (
Provider,
deepcopy,
deepcopy_args,
deepcopy_kwargs,
)
class NonCopiable:
def __deepcopy__(self, memo: Dict[int, Any]) -> NoReturn:
raise NotImplementedError
def test_deepcopy_streams_not_copied() -> None:
l = [sys.stdin, sys.stdout, sys.stderr]
assert deepcopy(l) == l
def test_deepcopy_args() -> None:
provider = Provider[None]()
copiable = NonCopiable()
memo: Dict[int, Any] = {id(copiable): copiable}
assert deepcopy_args(provider, (1, copiable), memo) == (1, copiable)
def test_deepcopy_args_non_copiable() -> None:
provider = Provider[None]()
copiable = NonCopiable()
memo: Dict[int, Any] = {id(copiable): copiable}
with raises(
NonCopyableArgumentError,
match=r"^Couldn't copy argument at index 3 for provider ",
):
deepcopy_args(provider, (1, copiable, object(), NonCopiable()), memo)
def test_deepcopy_kwargs() -> None:
provider = Provider[None]()
copiable = NonCopiable()
memo: Dict[int, Any] = {id(copiable): copiable}
assert deepcopy_kwargs(provider, {"x": 1, "y": copiable}, memo) == {
"x": 1,
"y": copiable,
}
def test_deepcopy_kwargs_non_copiable() -> None:
provider = Provider[None]()
copiable = NonCopiable()
memo: Dict[int, Any] = {id(copiable): copiable}
with raises(
NonCopyableArgumentError,
match=r"^Couldn't copy keyword argument z for provider ",
):
deepcopy_kwargs(provider, {"x": 1, "y": copiable, "z": NonCopiable()}, memo)

View File

@ -1,11 +1,7 @@
import sys import sys
from typing_extensions import Annotated
from fastapi import FastAPI, Depends from fastapi import FastAPI, Depends
from fastapi import ( from fastapi import Request # See: https://github.com/ets-labs/python-dependency-injector/issues/398
Request,
) # See: https://github.com/ets-labs/python-dependency-injector/issues/398
from fastapi.security import HTTPBasic, HTTPBasicCredentials from fastapi.security import HTTPBasic, HTTPBasicCredentials
from dependency_injector import containers, providers from dependency_injector import containers, providers
from dependency_injector.wiring import inject, Provide from dependency_injector.wiring import inject, Provide
@ -32,16 +28,11 @@ async def index(service: Service = Depends(Provide[Container.service])):
return {"result": result} return {"result": result}
@app.api_route("/annotated")
@inject
async def annotated(service: Annotated[Service, Depends(Provide[Container.service])]):
result = await service.process()
return {"result": result}
@app.get("/auth") @app.get("/auth")
@inject @inject
def read_current_user(credentials: HTTPBasicCredentials = Depends(security)): def read_current_user(
credentials: HTTPBasicCredentials = Depends(security)
):
return {"username": credentials.username, "password": credentials.password} return {"username": credentials.username, "password": credentials.password}

View File

@ -1,11 +1,11 @@
from typing_extensions import Annotated
from flask import Flask, jsonify, request, current_app, session, g from flask import Flask, jsonify, request, current_app, session, g
from flask import _request_ctx_stack, _app_ctx_stack
from dependency_injector import containers, providers from dependency_injector import containers, providers
from dependency_injector.wiring import inject, Provide from dependency_injector.wiring import inject, Provide
# This is here for testing wiring bypasses these objects without crashing # This is here for testing wiring bypasses these objects without crashing
request, current_app, session, g # noqa request, current_app, session, g # noqa
_request_ctx_stack, _app_ctx_stack # noqa
class Service: class Service:
@ -28,12 +28,5 @@ def index(service: Service = Provide[Container.service]):
return jsonify({"result": result}) return jsonify({"result": result})
@app.route("/annotated")
@inject
def annotated(service: Annotated[Service, Provide[Container.service]]):
result = service.process()
return jsonify({"result": result})
container = Container() container = Container()
container.wire(modules=[__name__]) container.wire(modules=[__name__])

View File

@ -1,80 +1,41 @@
from typing import Any, Dict, List, Optional
from dependency_injector import containers, providers from dependency_injector import containers, providers
from dependency_injector.wiring import Closing, Provide, inject from dependency_injector.wiring import inject, Provide, Closing
class Counter:
def __init__(self) -> None:
self._init = 0
self._shutdown = 0
def init(self) -> None:
self._init += 1
def shutdown(self) -> None:
self._shutdown += 1
def reset(self) -> None:
self._init = 0
self._shutdown = 0
class Service: class Service:
def __init__(self, counter: Optional[Counter] = None, **dependencies: Any) -> None: init_counter: int = 0
self.counter = counter or Counter() shutdown_counter: int = 0
self.dependencies = dependencies
def init(self) -> None: @classmethod
self.counter.init() def reset_counter(cls):
cls.init_counter = 0
cls.shutdown_counter = 0
def shutdown(self) -> None: @classmethod
self.counter.shutdown() def init(cls):
cls.init_counter += 1
@property @classmethod
def init_counter(self) -> int: def shutdown(cls):
return self.counter._init cls.shutdown_counter += 1
@property
def shutdown_counter(self) -> int:
return self.counter._shutdown
class FactoryService: class FactoryService:
def __init__(self, service: Service, service2: Service): def __init__(self, service: Service):
self.service = service self.service = service
self.service2 = service2
class NestedService: def init_service():
def __init__(self, factory_service: FactoryService): service = Service()
self.factory_service = factory_service
def init_service(counter: Counter, _list: List[int], _dict: Dict[str, int]):
service = Service(counter, _list=_list, _dict=_dict)
service.init() service.init()
yield service yield service
service.shutdown() service.shutdown()
class Container(containers.DeclarativeContainer): class Container(containers.DeclarativeContainer):
counter = providers.Singleton(Counter)
_list = providers.List( service = providers.Resource(init_service)
providers.Callable(lambda a: a, a=1), providers.Callable(lambda b: b, 2) factory_service = providers.Factory(FactoryService, service)
)
_dict = providers.Dict(
a=providers.Callable(lambda a: a, a=3), b=providers.Callable(lambda b: b, 4)
)
service = providers.Resource(init_service, counter, _list, _dict=_dict)
service2 = providers.Resource(init_service, counter, _list, _dict=_dict)
factory_service = providers.Factory(FactoryService, service, service2)
factory_service_kwargs = providers.Factory(
FactoryService,
service=service,
service2=service2,
)
nested_service = providers.Factory(NestedService, factory_service)
@inject @inject
@ -83,21 +44,5 @@ def test_function(service: Service = Closing[Provide["service"]]):
@inject @inject
def test_function_dependency( def test_function_dependency(factory: FactoryService = Closing[Provide["factory_service"]]):
factory: FactoryService = Closing[Provide["factory_service"]],
):
return factory return factory
@inject
def test_function_dependency_kwargs(
factory: FactoryService = Closing[Provide["factory_service_kwargs"]],
):
return factory
@inject
def test_function_nested_dependency(
nested: NestedService = Closing[Provide["nested_service"]],
):
return nested

View File

@ -2,13 +2,13 @@
from decimal import Decimal from decimal import Decimal
from pytest import fixture, mark, raises
from samples.wiringstringids import module, package, resourceclosing
from samples.wiringstringids.container import Container, SubContainer
from samples.wiringstringids.service import Service
from dependency_injector import errors from dependency_injector import errors
from dependency_injector.wiring import Closing, Provide, Provider, wire from dependency_injector.wiring import Closing, Provide, Provider, wire
from pytest import fixture, mark, raises
from samples.wiringstringids import module, package, resourceclosing
from samples.wiringstringids.service import Service
from samples.wiringstringids.container import Container, SubContainer
@fixture(autouse=True) @fixture(autouse=True)
@ -34,11 +34,10 @@ def subcontainer():
@fixture @fixture
def resourceclosing_container(request): def resourceclosing_container():
container = resourceclosing.Container() container = resourceclosing.Container()
container.wire(modules=[resourceclosing]) container.wire(modules=[resourceclosing])
with container.reset_singletons(): yield container
yield container
container.unwire() container.unwire()
@ -275,65 +274,42 @@ def test_wire_multiple_containers():
@mark.usefixtures("resourceclosing_container") @mark.usefixtures("resourceclosing_container")
def test_closing_resource(): def test_closing_resource():
resourceclosing.Service.reset_counter()
result_1 = resourceclosing.test_function() result_1 = resourceclosing.test_function()
assert isinstance(result_1, resourceclosing.Service) assert isinstance(result_1, resourceclosing.Service)
assert result_1.init_counter == 1 assert result_1.init_counter == 1
assert result_1.shutdown_counter == 1 assert result_1.shutdown_counter == 1
assert result_1.dependencies == {"_list": [1, 2], "_dict": {"a": 3, "b": 4}}
result_2 = resourceclosing.test_function() result_2 = resourceclosing.test_function()
assert isinstance(result_2, resourceclosing.Service) assert isinstance(result_2, resourceclosing.Service)
assert result_2.init_counter == 2 assert result_2.init_counter == 2
assert result_2.shutdown_counter == 2 assert result_2.shutdown_counter == 2
assert result_1.dependencies == {"_list": [1, 2], "_dict": {"a": 3, "b": 4}}
assert result_1 is not result_2 assert result_1 is not result_2
@mark.usefixtures("resourceclosing_container") @mark.usefixtures("resourceclosing_container")
def test_closing_dependency_resource(): def test_closing_dependency_resource():
resourceclosing.Service.reset_counter()
result_1 = resourceclosing.test_function_dependency() result_1 = resourceclosing.test_function_dependency()
assert isinstance(result_1, resourceclosing.FactoryService) assert isinstance(result_1, resourceclosing.FactoryService)
assert result_1.service.init_counter == 2 assert result_1.service.init_counter == 1
assert result_1.service.shutdown_counter == 2 assert result_1.service.shutdown_counter == 1
result_2 = resourceclosing.test_function_dependency() result_2 = resourceclosing.test_function_dependency()
assert isinstance(result_2, resourceclosing.FactoryService) assert isinstance(result_2, resourceclosing.FactoryService)
assert result_2.service.init_counter == 4 assert result_2.service.init_counter == 2
assert result_2.service.shutdown_counter == 4 assert result_2.service.shutdown_counter == 2
@mark.usefixtures("resourceclosing_container")
def test_closing_dependency_resource_kwargs():
result_1 = resourceclosing.test_function_dependency_kwargs()
assert isinstance(result_1, resourceclosing.FactoryService)
assert result_1.service.init_counter == 2
assert result_1.service.shutdown_counter == 2
result_2 = resourceclosing.test_function_dependency_kwargs()
assert isinstance(result_2, resourceclosing.FactoryService)
assert result_2.service.init_counter == 4
assert result_2.service.shutdown_counter == 4
@mark.usefixtures("resourceclosing_container")
def test_closing_nested_dependency_resource():
result_1 = resourceclosing.test_function_nested_dependency()
assert isinstance(result_1, resourceclosing.NestedService)
assert result_1.factory_service.service.init_counter == 2
assert result_1.factory_service.service.shutdown_counter == 2
result_2 = resourceclosing.test_function_nested_dependency()
assert isinstance(result_2, resourceclosing.NestedService)
assert result_2.factory_service.service.init_counter == 4
assert result_2.factory_service.service.shutdown_counter == 4
assert result_1 is not result_2 assert result_1 is not result_2
@mark.usefixtures("resourceclosing_container") @mark.usefixtures("resourceclosing_container")
def test_closing_resource_bypass_marker_injection(): def test_closing_resource_bypass_marker_injection():
resourceclosing.Service.reset_counter()
result_1 = resourceclosing.test_function(service=Closing[Provide["service"]]) result_1 = resourceclosing.test_function(service=Closing[Provide["service"]])
assert isinstance(result_1, resourceclosing.Service) assert isinstance(result_1, resourceclosing.Service)
assert result_1.init_counter == 1 assert result_1.init_counter == 1
@ -349,6 +325,7 @@ def test_closing_resource_bypass_marker_injection():
@mark.usefixtures("resourceclosing_container") @mark.usefixtures("resourceclosing_container")
def test_closing_resource_context(): def test_closing_resource_context():
resourceclosing.Service.reset_counter()
service = resourceclosing.Service() service = resourceclosing.Service()
result_1 = resourceclosing.test_function(service=service) result_1 = resourceclosing.test_function(service=service)

View File

@ -1,20 +1,16 @@
from httpx import ASGITransport, AsyncClient from httpx import AsyncClient
from pytest import fixture, mark from pytest import fixture, mark
from pytest_asyncio import fixture as aio_fixture from pytest_asyncio import fixture as aio_fixture
# Runtime import to avoid syntax errors in samples on Python < 3.5 and reach top-dir # Runtime import to avoid syntax errors in samples on Python < 3.5 and reach top-dir
import os import os
_SAMPLES_DIR = os.path.abspath( _SAMPLES_DIR = os.path.abspath(
os.path.sep.join( os.path.sep.join((
( os.path.dirname(__file__),
os.path.dirname(__file__), "../samples/",
"../samples/", )),
)
),
) )
import sys import sys
sys.path.append(_SAMPLES_DIR) sys.path.append(_SAMPLES_DIR)
@ -23,7 +19,7 @@ from wiringfastapi import web
@aio_fixture @aio_fixture
async def async_client(): async def async_client():
client = AsyncClient(transport=ASGITransport(app=web.app), base_url="http://test") client = AsyncClient(app=web.app, base_url="http://test")
yield client yield client
await client.aclose() await client.aclose()
@ -41,19 +37,6 @@ async def test_depends_marker_injection(async_client: AsyncClient):
assert response.json() == {"result": "Foo"} assert response.json() == {"result": "Foo"}
@mark.asyncio
async def test_depends_with_annotated(async_client: AsyncClient):
class ServiceMock:
async def process(self):
return "Foo"
with web.container.service.override(ServiceMock()):
response = await async_client.get("/")
assert response.status_code == 200
assert response.json() == {"result": "Foo"}
@mark.asyncio @mark.asyncio
async def test_depends_injection(async_client: AsyncClient): async def test_depends_injection(async_client: AsyncClient):
response = await async_client.get("/auth", auth=("john_smith", "secret")) response = await async_client.get("/auth", auth=("john_smith", "secret"))

View File

@ -2,25 +2,19 @@ import json
# Runtime import to avoid syntax errors in samples on Python < 3.5 and reach top-dir # Runtime import to avoid syntax errors in samples on Python < 3.5 and reach top-dir
import os import os
_TOP_DIR = os.path.abspath( _TOP_DIR = os.path.abspath(
os.path.sep.join( os.path.sep.join((
( os.path.dirname(__file__),
os.path.dirname(__file__), "../",
"../", )),
)
),
) )
_SAMPLES_DIR = os.path.abspath( _SAMPLES_DIR = os.path.abspath(
os.path.sep.join( os.path.sep.join((
( os.path.dirname(__file__),
os.path.dirname(__file__), "../samples/",
"../samples/", )),
)
),
) )
import sys import sys
sys.path.append(_TOP_DIR) sys.path.append(_TOP_DIR)
sys.path.append(_SAMPLES_DIR) sys.path.append(_SAMPLES_DIR)
@ -35,13 +29,3 @@ def test_wiring_with_flask():
assert response.status_code == 200 assert response.status_code == 200
assert json.loads(response.data) == {"result": "OK"} assert json.loads(response.data) == {"result": "OK"}
def test_wiring_with_annotated():
client = web.app.test_client()
with web.app.app_context():
response = client.get("/annotated")
assert response.status_code == 200
assert json.loads(response.data) == {"result": "OK"}

Some files were not shown because too many files have changed in this diff Show More