Compare commits

..

99 Commits

Author SHA1 Message Date
ZipFile
2c0aede4aa Merge branch 'release/4.48.1' 2025-06-20 10:49:54 +00:00
ZipFile
84a14f2ca7 Update changelog 2025-06-20 10:03:00 +00:00
ZipFile
0c58064a36 Make wiring inspect exclsuions extensible 2025-06-20 10:03:00 +00:00
ZipFile
eb74b1e9d0 Minor improvements for _cwiring.DependencyResolver code generation
* Remove KWPair
* Avoid type checks around _is_injectable
2025-06-20 10:03:00 +00:00
ZipFile
be7d25518d Add typing-extensions as a dependency for older Python versions 2025-06-20 10:03:00 +00:00
ZipFile
04b5907f21 Add warning on extra @inject 2025-06-20 10:03:00 +00:00
ZipFile
e6cc12762f Add support for resource_type in Lifespans 2025-06-18 21:58:00 +00:00
ZipFile
bf2ddbce32 Upgrade cibuildwheel 2025-06-16 10:51:39 +00:00
ZipFile
9d6994391f Merge branch 'release/4.48.0' 2025-06-16 09:03:49 +00:00
ZipFile
dd84a1b5d6 Bump version 2025-06-16 08:53:36 +00:00
ZipFile
31a98f7731 Update changelog 2025-06-16 08:51:10 +00:00
ZipFile
b261251b34 Fix Sphinx warning 2025-06-16 08:50:00 +00:00
Aran Moncusí Ramírez
4bfe64563e
Add resource type parameter to init and shutdown resources using specialized providers (#858) 2025-06-16 11:34:02 +03:00
AndrianEquestrian
b411807572
Add support for Fast Stream Depends (#898) 2025-06-16 10:37:31 +03:00
ZipFile
f2da51e0d4 Use typing_extensions.Self as fallback (fixes #902) 2025-06-05 16:26:40 +00:00
ZipFile
2293251986 Add docs for ASGI Lifespan support 2025-06-03 20:43:06 +00:00
ZipFile
1b4b3d349f Fix some more Sphinx warnings 2025-06-03 20:33:13 +00:00
ZipFile
d8e49f7dd5
Add support for async generator injections (#900) 2025-06-03 21:45:43 +03:00
ZipFile
c1f14a876a Expose null awaitables 2025-06-02 22:46:57 +00:00
ZipFile
c97a0cc515 Fix mypy warnings in dependency_injector.ext 2025-06-01 18:57:47 +00:00
ZipFile
0ada62acbf Add .editorconfig 2025-06-01 18:50:07 +00:00
ZipFile
67827a36d1 Fix mypy warnigns in containers.pyi 2025-06-01 18:46:30 +00:00
ZipFile
ceed6a8222 Add combine_as_imports = true isort option 2025-06-01 18:45:47 +00:00
ZipFile
6766ef3eba Remove __init__.pyi 2025-06-01 18:45:12 +00:00
ZipFile
a8914e54e0 Fix Sphinx warnings 2025-06-01 18:08:37 +00:00
ZipFile
cdd9ce5048 Add doc section on wire() caching 2025-06-01 17:51:04 +00:00
ZipFile
51c7db771d Fix csv newline handling in movie-lister example 2025-06-01 17:35:32 +00:00
ZipFile
a322584308 Add context manager support to Resource provider 2025-06-01 15:48:57 +00:00
ZipFile
4b3476cb48 Use cache in _fetch_reference_injections() 2025-05-31 12:31:54 +00:00
ZipFile
8460465b5e Merge branch 'release/4.47.1' 2025-05-30 19:52:18 +00:00
ZipFile
16f444b230 Bump version 2025-05-30 19:50:21 +00:00
ZipFile
1ae96e3eeb Use windows-2022 GHA image for building wheels 2025-05-30 19:44:47 +00:00
ZipFile
7fcf1ac7ad Make mypy --strict tests/typing passable 2025-05-30 19:31:44 +00:00
ZipFile
1271d0fa79 Add type info for _cwiring module 2025-05-30 19:29:11 +00:00
ZipFile
b9df88eea7 Fix typing for wiring marker 2025-05-30 19:28:49 +00:00
ZipFile
99489afa3f Strip debug symbols when building wheels 2025-05-28 20:35:04 +00:00
ZipFile
193249f7ec Merge branch 'release/4.47.0' 2025-05-28 19:09:56 +00:00
ZipFile
01349c43e1 Bump version 2025-05-28 19:05:05 +00:00
ZipFile
41ed07a210 Update changelog 2025-05-28 19:03:52 +00:00
ZipFile
8be79126ad Enable ABI3 for regular tests 2025-05-28 16:51:39 +00:00
ZipFile
dfee54932b Migrate to OIDC publishing 2025-05-28 13:50:02 +00:00
ZipFile
a61749c68d Enable ABI3 builds 2025-05-28 13:50:02 +00:00
ZipFile
14be69371b Limit ABI3 builds to CPython only 2025-05-28 13:50:02 +00:00
ZipFile
cfeb018ca7 Fix pytest-asyncio warning 2025-05-28 13:50:02 +00:00
ZipFile
183b2ae7ff Require Cython>=3.1.1 2025-05-28 13:50:02 +00:00
ZipFile
561ff46658 Add wheelhouse to .gitignore 2025-05-28 13:50:02 +00:00
ZipFile
49cc8ed827 Fix PyPy test runs in tox 2025-05-28 13:50:02 +00:00
ZipFile
383e95faed Fix file inclusion warnings from MANIFEST.in 2025-05-28 13:50:02 +00:00
Roman Mogylatov
e7e64f6ae0
Update coding-guide.mdc 2025-05-22 18:29:37 -04:00
Roman Mogylatov
f50cc95405 Add Cursor rules 2025-05-21 16:19:08 -04:00
Roman Mogylatov
8814db3fb3
Fix annotated attribute injection (#889)
* Add example for Annotated attribute injection for module/class attributes

* Fix attribute injection with Annotated types

* Add unit tests for Annotated attribute and argument injection in wiring

* Add .cursor to .gitignore

* Style: add blank lines between class definitions and attributes in annotated attribute example

* Docs: clarify and format module/class attribute injection for classic and Annotated forms

* Changelog: add note and discussion link for Annotated attribute injection support

* Fix nls

* Fix CI checks and Python 3.8 tests

* Fix PR issues

* Fix Python 3.8 tests

* Fix flake8 issues

* Fix: robust Annotated detection for wiring across Python versions

* Refactor: extract annotation retrieval and improve typing for Python 3.9 compatibility

* Update src/dependency_injector/wiring.py

Co-authored-by: ZipFile <zipfile.d@protonmail.com>

---------

Co-authored-by: ZipFile <zipfile.d@protonmail.com>
2025-05-21 16:13:37 -04:00
ZipFile
8bf9ed04c8
Update Cython to v3.1 (#887) 2025-05-18 13:55:06 +03:00
ZipFile
dbf86e4eb4
Do not override methods without patching (#886) 2025-05-18 12:17:54 +03:00
ZipFile
9a08bfcede
Drop Python 3.7 support (#885) 2025-05-18 12:06:33 +03:00
ZipFile
4ae79ac21f
Remove unused root property from ConfigurationOption (#875)
fixes #874
2025-04-07 14:57:45 +03:00
ZipFile
35bfafdfe2
Move pytest config to pyproject.toml (#876)
* Move pytest config to pyproject.toml

* Fix pytest warning
2025-04-07 14:57:13 +03:00
ZipFile
6685c5a141 Fix infinite loop with Closing+ConfigurationOption 2025-03-10 20:35:37 +00:00
ZipFile
57123cebaa Merge branch 'master' into develop 2025-03-02 12:37:21 +00:00
ZipFile
6e4794bab1
Remove code for EOL Python versions (#864) 2025-03-02 14:33:31 +02:00
ZipFile
f3b3b1baa4 Merge branch 'release/4.46.0' into master 2025-02-25 15:14:15 +00:00
ZipFile
9b66d4bf16 Bump version to v4.46.0 2025-02-23 17:22:13 +00:00
ZipFile
7d4ebecd19
Add option to disable env var interpolation in configs (#861) 2025-02-23 19:01:01 +02:00
ZipFile
09efbffab1
Fix Closing dependency resolution (#852)
Co-authored-by: federinik <federico.tomasi@outlook.com>
Co-authored-by: jazzthief <mynameisyegor@gmail.com>
2025-02-23 18:31:34 +02:00
ZipFile
8b625d81ad
Use Annotated for DI in FastAPI examples (#853) 2025-02-23 18:21:31 +02:00
ZipFile
23acf01c15
Add support for inspect.iscoroutinefunction() in Coroutine provider (#830) 2025-02-23 18:20:38 +02:00
Martin Lafrance
0d6fdb5b78
Fix broken wiring of sync inject-decorated methods (#673)
Co-authored-by: Martin Lafrance <mlafrance@cae.com>
Co-authored-by: ZipFile <zipfile.d@protonmail.com>
2025-02-23 18:17:45 +02:00
Taein Min
2330122de6
Add support for typing.Annotated (#721) 2025-01-20 17:37:28 +02:00
ZipFile
29ae3e1337 Make flake8 config black-compatible 2025-01-18 17:39:07 +00:00
ZipFile
50643e0dfb Run black 2025-01-18 17:02:55 +00:00
ZipFile
3893e1df81 Use native GHA ubuntu-24.04-arm image for building wheels 2025-01-16 19:15:49 +00:00
ZipFile
0fd35baee6 Use ubuntu-24.04 GHA image 2025-01-16 19:13:11 +00:00
Ilya Kazakov
3df95847d5
[movie-lister] Added test fixture and updated documentation (#747)
* test: add fixture for finder mock

* docs: update tests code example, emphasize-lines & test coverage report results
2025-01-12 15:46:29 +02:00
ZipFile
6d9d34c0f6 Add test case for Provider.provider type propagation 2025-01-12 12:18:21 +00:00
Philip Bjorge
de50666a13
fix: type provider (#744) 2025-01-12 14:14:12 +02:00
ZipFile
ccbd5bbb80 Migrate CI pipeline to actions/upload-artifact@v4 2025-01-08 13:07:04 +00:00
Philip Bjorge
00326e9a22
fix: type propogation through provided (#733)
Co-authored-by: Gonzalo Martinez <gonzarmv@gmail.com>
2025-01-08 13:31:00 +02:00
Roman Mogylatov
46646b1acf Merge branch 'release/4.45.0' into master 2025-01-05 15:20:12 -05:00
Roman Mogylatov
9f38db6ef3 Bump version to 4.45.0 2025-01-05 15:19:57 -05:00
Roman Mogylatov
9f4e2839d2
Remove unused imports from the starlette extension (#846) 2025-01-05 14:57:55 -05:00
ZipFile
41e18dfa90
Add Starlette lifespan handler implementation (#683) 2025-01-05 14:39:26 -05:00
František Trebuňa
f9db578c59
🎨 Raise exception instead of hiding it in finally (#845) 2025-01-05 14:33:09 -05:00
ZipFile
d82d9fb822
Improve debugability of deepcopy errors (#839) 2025-01-01 21:22:29 +02:00
ZipFile
3ba4704bc1 Remove six 2024-12-14 13:24:28 +00:00
JC (Jonathan Chen)
aa56b70dc8
docs: fix grammar (#709) 2024-12-09 10:54:30 +02:00
ZipFile
7f586246b4
Update examples (#838) 2024-12-08 18:53:29 +02:00
ZipFile
87741edb53
Upgrade testing deps (#837) 2024-12-08 18:53:08 +02:00
Roman Mogylatov
be7abb3ec7 Merge branch 'release/4.44.0' into master 2024-12-07 13:44:32 -05:00
ZipFile
15400dea7d
Fix sdist build for publishing (#836) 2024-12-07 13:42:27 -05:00
Roman Mogylatov
704e36a642 Merge branch 'release/4.44.0' into master 2024-12-07 11:52:05 -05:00
Roman Mogylatov
83d71acb70 Bump version to 4.44.0 2024-12-07 11:51:44 -05:00
ZipFile
c61fc16b8d
Yet another Pydantic 2 support (#832)
* Add support for Pydantic v2 settings

* Configure pipeline to run tests against different pydantic versions

* Update Pydantic docs and examples for v2

* Fix compatibility with httpx v0.27.0
2024-12-07 11:38:08 -05:00
Roman Mogylatov
cab75cb9c7 Update changelog 2024-11-10 00:05:25 -05:00
ZipFile
494c457643
PEP-517 (#829)
* Convert to PEP-517 project

* Move pylint and coverage configs to pyproject.toml

* Remove autogenerated C files
2024-11-10 00:01:30 -05:00
Roman Mogylatov
abf2a2577c Merge branch 'release/4.43.0' into master 2024-11-04 00:03:25 -05:00
Roman Mogylatov
3777a947ea Update version to 4.43.0 2024-11-04 00:02:52 -05:00
Roman Mogylatov
c92129dcb0
Add support for Python 3.13 (#828)
* Update tests pipeline and setup.py

* Update tox coverage command

* Add setuptools to the dev requirements file

* Enforce coverage version in tox

* Leave coveralls CI/CD job on Python 3.12 because coveralls 4.0.1 doesn't support Python 3.13

* Update changelog and publishing jobs
2024-11-04 00:01:28 -05:00
Roman Mogylatov
37486900cd Add ZipFile to the list of contributors and update changelog 2024-11-03 21:01:45 -05:00
Roman Mogylatov
9071583981 Pin Cython version 2024-11-03 20:55:53 -05:00
ZipFile
595daebd3a
Migrate to Cython3 (#813)
* Fix asyncio tests

* Convert class-private attributes to just private

* Upgrade to Cython 3

* Regenerate C files

* Fix tox coverage report
2024-11-03 20:48:40 -05:00
149 changed files with 4255 additions and 211234 deletions

View File

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

View File

@ -0,0 +1,29 @@
---
description: Code in Python and Cython
globs:
alwaysApply: false
---
- Follow PEP 8 rules
- When you write imports, split system, 3rd-party, and local imports with a new line
- Have two empty lines between the import block and the rest of the code
- Have an empty line (\n) at the end of every file
- If a file is supposed to be run, always add ``if __name__ == 'main'``
- Always follow a consistent pattern of using double or single quotes
- When there is a class without a docblock, leave one blank line before its members, e.g.:
```python
class Container(containers.DeclarativeContainer):
service = providers.Factory(Service)
```
- Avoid shortcuts in names unless absolutely necessary, exceptions:
```
arg
args
kwarg
kwargs
obj
cls
```
- Avoid inline comments unless absolutely necessary

View File

@ -0,0 +1,7 @@
---
description: Build and run tests
globs:
alwaysApply: false
---
- Use Makefile commands to build, test, lint and other similar operations when they are available.
- Activate virtualenv before running any commands by ``. venv/bin/actvate``

View File

@ -0,0 +1,8 @@
---
description: Run examples
globs:
alwaysApply: false
---
- When you run an example from the ``examples/`` folder, switch to the example folder and run it from there.
- If there are instructions on running the examples or its tests in readme, follow them
- Activate virtualenv before running any commands by ``. venv/bin/actvate``

9
.editorconfig Normal file
View File

@ -0,0 +1,9 @@
root = true
[*]
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true
[*.{py,pyi,pxd,pyx}]
ij_visual_guides = 80,88

View File

@ -10,20 +10,20 @@ jobs:
tests:
name: Run tests
runs-on: ubuntu-22.04
runs-on: ubuntu-24.04
steps:
- uses: actions/checkout@v3
- uses: actions/setup-python@v4
with:
python-version: 3.12
python-version: 3.13
- run: pip install tox
- run: tox
env:
TOXENV: 3.12
TOXENV: 3.13
linters:
name: Run linters
runs-on: ubuntu-22.04
runs-on: ubuntu-24.04
strategy:
matrix:
toxenv: [flake8, pydocstyle, mypy, pylint]
@ -31,7 +31,7 @@ jobs:
- uses: actions/checkout@v3
- uses: actions/setup-python@v4
with:
python-version: 3.12
python-version: 3.13
- run: pip install tox
- run: tox
env:
@ -40,17 +40,18 @@ jobs:
build-sdist:
name: Build source tarball
needs: [tests, linters]
runs-on: ubuntu-22.04
runs-on: ubuntu-24.04
steps:
- uses: actions/checkout@v3
- uses: actions/setup-python@v4
with:
python-version: 3.12
python-version: 3.13
- run: |
python -m pip install --upgrade pip setuptools
python setup.py sdist
- uses: actions/upload-artifact@v3
python -m pip install --upgrade build
python -m build --sdist
- uses: actions/upload-artifact@v4
with:
name: cibw-sdist
path: ./dist/*
build-wheels:
@ -59,62 +60,63 @@ jobs:
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-22.04, windows-2019, macos-14]
os: [ubuntu-24.04, ubuntu-24.04-arm, windows-2022, macos-14]
env:
CIBW_SKIP: cp27-* cp313-*
CIBW_ENABLE: pypy
CIBW_ENVIRONMENT: >-
PIP_CONFIG_SETTINGS="build_ext=-j4"
DEPENDENCY_INJECTOR_LIMITED_API="1"
CFLAGS="-g0"
steps:
- uses: actions/checkout@v3
- name: Build wheels
uses: pypa/cibuildwheel@v2.20.0
- uses: actions/upload-artifact@v3
uses: pypa/cibuildwheel@v3.0.0
- uses: actions/upload-artifact@v4
with:
name: cibw-wheels-${{ matrix.os }}-${{ strategy.job-index }}
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-* cp313-*
test-publish:
name: Upload release to TestPyPI
needs: [build-sdist, build-wheels]
runs-on: ubuntu-latest
environment: test-pypi
permissions:
id-token: write
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
- uses: actions/download-artifact@v4
with:
path: ./wheelhouse/*.whl
publish:
name: Publish on PyPI
needs: [build-sdist, build-wheels, build-wheels-linux-aarch64]
runs-on: ubuntu-22.04
steps:
- uses: actions/download-artifact@v3
with:
name: artifact
pattern: cibw-*
path: dist
merge-multiple: true
- uses: pypa/gh-action-pypi-publish@release/v1
with:
user: __token__
password: ${{ secrets.PYPI_API_TOKEN }}
# For publishing to Test PyPI, uncomment next two lines:
# password: ${{ secrets.TEST_PYPI_API_TOKEN }}
# repository_url: https://test.pypi.org/legacy/
repository-url: https://test.pypi.org/legacy/
publish:
name: Upload release to PyPI
needs: [build-sdist, build-wheels, test-publish]
runs-on: ubuntu-latest
environment: pypi
permissions:
id-token: write
steps:
- uses: actions/download-artifact@v4
with:
pattern: cibw-*
path: dist
merge-multiple: true
- uses: pypa/gh-action-pypi-publish@release/v1
publish-docs:
name: Publish docs
needs: [publish]
runs-on: ubuntu-22.04
runs-on: ubuntu-24.04
steps:
- uses: actions/checkout@v3
- uses: actions/setup-python@v4
with:
python-version: 3.12
python-version: 3.13
- run: pip install awscli
- run: pip install -r requirements-doc.txt
- run: pip install -e .

View File

@ -4,28 +4,12 @@ on: [push, pull_request, workflow_dispatch]
jobs:
tests-on-legacy-versions:
name: Run tests on legacy versions
runs-on: ubuntu-20.04
strategy:
matrix:
python-version: [3.7]
steps:
- uses: actions/checkout@v3
- uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}
- run: pip install tox
- run: tox
env:
TOXENV: ${{ matrix.python-version }}
test-on-different-versions:
name: Run tests
runs-on: ubuntu-latest
strategy:
matrix:
python-version: [3.8, 3.9, "3.10", 3.11, 3.12]
python-version: ["3.8", "3.9", "3.10", "3.11", "3.12", "3.13"]
steps:
- uses: actions/checkout@v3
- uses: actions/setup-python@v4
@ -34,14 +18,26 @@ jobs:
- run: pip install tox
- run: tox
env:
DEPENDENCY_INJECTOR_LIMITED_API: 1
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:
name: Run tests with coverage
runs-on: ubuntu-latest
env:
# Cython's version <3 issue with tracing: "error: no member named 'use_tracing' in 'struct _PyCFrame'"
# DEPENDENCY_INJECTOR_DEBUG_MODE: 1
DEPENDENCY_INJECTOR_DEBUG_MODE: 1
PIP_VERBOSE: 1
COVERALLS_REPO_TOKEN: ${{ secrets.COVERALLS_REPO_TOKEN }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
steps:
@ -49,9 +45,8 @@ jobs:
- uses: actions/setup-python@v4
with:
python-version: 3.12
- run: pip install tox cython==0.29.37
- run: make cythonize
- run: tox
- run: pip install tox
- run: tox -vv
env:
TOXENV: coveralls
@ -65,7 +60,7 @@ jobs:
- uses: actions/checkout@v3
- uses: actions/setup-python@v4
with:
python-version: 3.12
python-version: 3.13
- run: pip install tox
- run: tox
env:

13
.gitignore vendored
View File

@ -15,6 +15,7 @@ lib64/
parts/
sdist/
var/
wheelhouse/
*.egg-info/
.installed.cfg
*.egg
@ -63,13 +64,11 @@ venv*/
# Vim Rope
.ropeproject/
# C extensions
src/dependency_injector/*.h
src/dependency_injector/*.so
src/dependency_injector/containers/*.h
src/dependency_injector/containers/*.so
src/dependency_injector/providers/*.h
src/dependency_injector/providers/*.so
# Cython artifacts
src/**/*.c
src/**/*.h
src/**/*.so
src/**/*.html
# Workspace for samples
.workspace/

View File

@ -1,49 +0,0 @@
[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

@ -21,3 +21,4 @@ Dependency Injector Contributors
+ Thiago Hiromi (thiromi)
+ Felipe Rubio (krouw)
+ Anton Petrov (anton-petrov)
+ ZipFile (ZipFile)

View File

@ -1,9 +1,7 @@
recursive-include src/dependency_injector *.py* *.c
recursive-include src/dependency_injector *.py* *.c py.typed
recursive-include tests *.py
include README.rst
include CONTRIBUTORS.rst
include LICENSE.rst
include requirements.txt
include setup.py
include tox.ini
include py.typed

View File

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

View File

@ -0,0 +1,9 @@
dependency_injector.ext.starlette
=================================
.. automodule:: dependency_injector.ext.starlette
:members:
:inherited-members:
:show-inheritance:
.. disqus::

View File

@ -9,3 +9,4 @@ API Documentation
containers
wiring
errors
asgi-lifespan

View File

@ -72,7 +72,7 @@ release = version
#
# This is also used if you do content translation via gettext catalogs.
# Usually you set "language" from the command line for these cases.
language = None
language = "en"
# There are two options for replacing |today|: either, you set today to some
# non-false value, then it is used:

View File

@ -78,7 +78,7 @@ Container is wired to the ``views`` module in the app config ``web/apps.py``:
.. literalinclude:: ../../examples/miniapps/django/web/apps.py
:language: python
:emphasize-lines: 13
:emphasize-lines: 12
Tests
-----

View File

@ -0,0 +1,48 @@
.. _fastdepends-example:
FastDepends example
===================
.. meta::
:keywords: Python,Dependency Injection,FastDepends,Example
:description: This example demonstrates a usage of the FastDepends and Dependency Injector.
This example demonstrates how to use ``Dependency Injector`` with `FastDepends <https://github.com/Lancetnik/FastDepends>`_, a lightweight dependency injection framework inspired by FastAPI's dependency system, but without the web framework components.
Basic Usage
-----------
The integration between FastDepends and Dependency Injector is straightforward. Simply use Dependency Injector's ``Provide`` marker within FastDepends' ``Depends`` function:
.. code-block:: python
import sys
from dependency_injector import containers, providers
from dependency_injector.wiring import inject, Provide
from fast_depends import Depends
class CoefficientService:
@staticmethod
def get_coefficient() -> float:
return 1.2
class Container(containers.DeclarativeContainer):
service = providers.Factory(CoefficientService)
@inject
def apply_coefficient(
a: int,
coefficient_provider: CoefficientService = Depends(Provide[Container.service]),
) -> float:
return a * coefficient_provider.get_coefficient()
container = Container()
container.wire(modules=[sys.modules[__name__]])
apply_coefficient(100) == 120.0

View File

@ -22,5 +22,6 @@ Explore the examples to see the ``Dependency Injector`` in action.
fastapi
fastapi-redis
fastapi-sqlalchemy
fastdepends
.. disqus::

View File

@ -31,7 +31,7 @@ Key features of the ``Dependency Injector``:
The framework stands on the `PEP20 (The Zen of Python) <https://www.python.org/dev/peps/pep-0020/>`_ principle:
.. code-block:: plain
.. code-block:: text
Explicit is better than implicit

View File

@ -7,6 +7,90 @@ that were made in every particular version.
From version 0.7.6 *Dependency Injector* framework strictly
follows `Semantic versioning`_
4.48.1
------
* Improve performance of ``dependency_injector._cwiring.DependencyResolver``
* Add ``typing-extensions`` as a dependency for older Python versions (<3.11)
* Produce warning on ``@inject``s without ``Provide[...]`` marks
* Add support for `resource_type` in ``Lifespan``s
4.48.0
------
- Improve performance of wiring (`#897 <https://github.com/ets-labs/python-dependency-injector/pull/897>`_)
- Add Context Manager support to Resource provider (`#899 <https://github.com/ets-labs/python-dependency-injector/pull/899>`_)
- Add support for async generator injections (`#900 <https://github.com/ets-labs/python-dependency-injector/pull/900>`_)
- Fix unintended dependency on ``typing_extensions`` (`#902 <https://github.com/ets-labs/python-dependency-injector/pull/902>`_)
- Add support for Fast Depends (`#898 <https://github.com/ets-labs/python-dependency-injector/pull/898>`_)
- Add ``resource_type`` parameter to init and shutdown resources using specialized providers (`#858 <https://github.com/ets-labs/python-dependency-injector/pull/858>`_)
4.47.1
------
- Fix typing for wiring marker (`#892 <https://github.com/ets-labs/python-dependency-injector/pull/896>`_)
- Strip debug symbols in wheels
4.47.0
------
- Add support for ``Annotated`` type for module and class attribute injection in wiring,
with updated documentation and examples.
See discussion:
https://github.com/ets-labs/python-dependency-injector/pull/721#issuecomment-2025263718
- Fix ``root`` property shadowing in ``ConfigurationOption`` (`#875 <https://github.com/ets-labs/python-dependency-injector/pull/875>`_)
- Fix incorrect monkeypatching during ``wire()`` that could violate MRO in some classes (`#886 <https://github.com/ets-labs/python-dependency-injector/pull/886>`_)
- ABI3 wheels are now published for CPython.
- Drop support of Python 3.7.
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
--------
- Add support for Python 3.13.
- Migrate to Cython 3 (version 3.0.11). Many thanks to `ZipFile <https://github.com/ZipFile>`_ for
this contribution `#813 <https://github.com/ets-labs/python-dependency-injector/pull/813>`_.
4.42.0
--------
- Promote release ``4.42.0b1`` to a production release.

View File

@ -183,22 +183,22 @@ See also: :ref:`configuration-envs-interpolation`.
Loading from a Pydantic settings
--------------------------------
``Configuration`` provider can load configuration from a ``pydantic`` settings object using the
``Configuration`` provider can load configuration from a ``pydantic_settings.BaseSettings`` object using the
:py:meth:`Configuration.from_pydantic` method:
.. literalinclude:: ../../examples/providers/configuration/configuration_pydantic.py
:language: python
:lines: 3-
:emphasize-lines: 31
:emphasize-lines: 32
To get the data from pydantic settings ``Configuration`` provider calls ``Settings.dict()`` method.
To get the data from pydantic settings ``Configuration`` provider calls its ``model_dump()`` method.
If you need to pass an argument to this call, use ``.from_pydantic()`` keyword arguments.
.. code-block:: python
container.config.from_pydantic(Settings(), exclude={"optional"})
Alternatively, you can provide a ``pydantic`` settings object over the configuration provider argument. In that case,
Alternatively, you can provide a ``pydantic_settings.BaseSettings`` object over the configuration provider argument. In that case,
the container will call ``config.from_pydantic()`` automatically:
.. code-block:: python
@ -215,18 +215,23 @@ the container will call ``config.from_pydantic()`` automatically:
.. note::
``Dependency Injector`` doesn't install ``pydantic`` by default.
``Dependency Injector`` doesn't install ``pydantic-settings`` by default.
You can install the ``Dependency Injector`` with an extra dependency::
pip install dependency-injector[pydantic]
pip install dependency-injector[pydantic2]
or install ``pydantic`` directly::
or install ``pydantic-settings`` directly::
pip install pydantic
pip install pydantic-settings
*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
-------------------------
@ -361,6 +366,19 @@ See also: :ref:`configuration-strict-mode`.
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
------------------------------

View File

@ -61,11 +61,12 @@ When you call ``.shutdown()`` method on a resource provider, it will remove the
if any, and switch to uninitialized state. Some of resource initializer types support specifying custom
resource shutdown.
Resource provider supports 3 types of initializers:
Resource provider supports 4 types of initializers:
- Function
- Generator
- Subclass of ``resources.Resource``
- Context Manager
- Generator (legacy)
- Subclass of ``resources.Resource`` (legacy)
Function initializer
--------------------
@ -103,8 +104,44 @@ you configure global resource:
Function initializer does not provide a way to specify custom resource shutdown.
Generator initializer
---------------------
Context Manager initializer
---------------------------
This is an extension to the Function initializer. Resource provider automatically detects if the initializer returns a
context manager and uses it to manage the resource lifecycle.
.. code-block:: python
from dependency_injector import containers, providers
class DatabaseConnection:
def __init__(self, host, port, user, password):
self.host = host
self.port = port
self.user = user
self.password = password
def __enter__(self):
print(f"Connecting to {self.host}:{self.port} as {self.user}")
return self
def __exit__(self, exc_type, exc_val, exc_tb):
print("Closing connection")
class Container(containers.DeclarativeContainer):
config = providers.Configuration()
db = providers.Resource(
DatabaseConnection,
host=config.db.host,
port=config.db.port,
user=config.db.user,
password=config.db.password,
)
Generator initializer (legacy)
------------------------------
Resource provider can use 2-step generators:
@ -154,8 +191,13 @@ object is not mandatory. You can leave ``yield`` statement empty:
argument2=...,
)
Subclass initializer
--------------------
.. note::
Generator initializers are automatically wrapped with ``contextmanager`` or ``asynccontextmanager`` decorator when
provided to a ``Resource`` provider.
Subclass initializer (legacy)
-----------------------------
You can create resource initializer by implementing a subclass of the ``resources.Resource``:
@ -210,6 +252,72 @@ first argument.
.. _resource-provider-wiring-closing:
Scoping Resources using specialized subclasses
----------------------------------------------
You can use specialized subclasses of ``Resource`` provider to initialize and shutdown resources by type.
Allowing for example to only initialize a subgroup of resources.
.. code-block:: python
class ScopedResource(resources.Resource):
pass
def init_service(name) -> Service:
print(f"Init {name}")
yield Service()
print(f"Shutdown {name}")
class Container(containers.DeclarativeContainer):
scoped = ScopedResource(
init_service,
"scoped",
)
generic = providers.Resource(
init_service,
"generic",
)
To initialize resources by type you can use ``init_resources(resource_type)`` and ``shutdown_resources(resource_type)``
methods adding the resource type as an argument:
.. code-block:: python
def main():
container = Container()
container.init_resources(ScopedResource)
# Generates:
# >>> Init scoped
container.shutdown_resources(ScopedResource)
# Generates:
# >>> Shutdown scoped
And to initialize all resources you can use ``init_resources()`` and ``shutdown_resources()`` without arguments:
.. code-block:: python
def main():
container = Container()
container.init_resources()
# Generates:
# >>> Init scoped
# >>> Init generic
container.shutdown_resources()
# Generates:
# >>> Shutdown scoped
# >>> Shutdown generic
It works using the ``traverse()`` method to find all resources of the specified type, selecting all resources
which are instances of the specified type.
Resources, wiring, and per-function execution scope
---------------------------------------------------
@ -263,10 +371,11 @@ Asynchronous function initializer:
argument2=...,
)
Asynchronous generator initializer:
Asynchronous Context Manager initializer:
.. code-block:: python
@asynccontextmanager
async def init_async_resource(argument1=..., argument2=...):
connection = await connect()
yield connection
@ -358,5 +467,54 @@ See also:
- Wiring :ref:`async-injections-wiring`
- :ref:`fastapi-redis-example`
ASGI Lifespan Protocol Support
------------------------------
The :mod:`dependency_injector.ext.starlette` module provides a :class:`~dependency_injector.ext.starlette.Lifespan`
class that integrates resource providers with ASGI applications using the `Lifespan Protocol`_. This allows resources to
be automatically initialized at application startup and properly shut down when the application stops.
.. code-block:: python
from contextlib import asynccontextmanager
from dependency_injector import containers, providers
from dependency_injector.wiring import Provide, inject
from dependency_injector.ext.starlette import Lifespan
from fastapi import FastAPI, Request, Depends, APIRouter
class Connection: ...
@asynccontextmanager
async def init_database():
print("opening database connection")
yield Connection()
print("closing database connection")
router = APIRouter()
@router.get("/")
@inject
async def index(request: Request, db: Connection = Depends(Provide["db"])):
# use the database connection here
return "OK!"
class Container(containers.DeclarativeContainer):
__self__ = providers.Self()
db = providers.Resource(init_database)
lifespan = providers.Singleton(Lifespan, __self__)
app = providers.Singleton(FastAPI, lifespan=lifespan)
_include_router = providers.Resource(
app.provided.include_router.call(),
router,
)
if __name__ == "__main__":
import uvicorn
container = Container()
app = container.app()
uvicorn.run(app, host="localhost", port=8000)
.. _Lifespan Protocol: https://asgi.readthedocs.io/en/latest/specs/lifespan.html
.. disqus::

View File

@ -24,7 +24,7 @@ returns it on the rest of the calls.
.. note::
``Singleton`` provider makes dependencies injection only when creates an object. When an object
``Singleton`` provider makes dependencies injection only when it creates an object. When an object
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

View File

@ -257,7 +257,7 @@ Let's check that it works. Open another terminal session and use ``httpie``:
You should see:
.. code-block:: json
.. code-block:: http
HTTP/1.1 200 OK
Content-Length: 844
@ -596,7 +596,7 @@ and make a request to the API in the terminal:
You should see:
.. code-block:: json
.. code-block:: http
HTTP/1.1 200 OK
Content-Length: 492

View File

@ -18,7 +18,7 @@ In this tutorial we will use:
- Python 3
- Docker
- Docker-compose
- Docker Compose
Start from the scratch or jump to the section:
@ -47,28 +47,27 @@ response it will log:
Prerequisites
-------------
We will use `Docker <https://www.docker.com/>`_ and
`docker-compose <https://docs.docker.com/compose/>`_ in this tutorial. Let's check the versions:
We will use `docker compose <https://docs.docker.com/compose/>`_ in this tutorial. Let's check the versions:
.. code-block:: bash
docker --version
docker-compose --version
docker compose version
The output should look something like:
.. code-block:: bash
Docker version 20.10.5, build 55c4c88
docker-compose version 1.29.0, build 07737305
Docker version 27.3.1, build ce12230
Docker Compose version v2.29.7
.. 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:
- `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.
@ -129,13 +128,13 @@ Put next lines into the ``requirements.txt`` file:
pytest-cov
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.9-buster`` as a base image.
specify how to run it. We will use ``python:3.13-bookworm`` as a base image.
Put next lines into the ``Dockerfile`` file:
.. code-block:: bash
FROM python:3.10-buster
FROM python:3.13-bookworm
ENV PYTHONUNBUFFERED=1
@ -155,8 +154,6 @@ Put next lines into the ``docker-compose.yml`` file:
.. code-block:: yaml
version: "3.7"
services:
monitor:
@ -171,7 +168,7 @@ Run in the terminal:
.. 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:
@ -184,7 +181,7 @@ After the build is done run the container:
.. code-block:: bash
docker-compose up
docker compose up
The output should look like:
@ -461,7 +458,7 @@ Run in the terminal:
.. code-block:: bash
docker-compose up
docker compose up
The output should look like:
@ -705,7 +702,7 @@ Run in the terminal:
.. code-block:: bash
docker-compose up
docker compose up
You should see:
@ -813,7 +810,7 @@ Run in the terminal:
.. code-block:: bash
docker-compose up
docker compose up
You should see:
@ -965,15 +962,16 @@ Run in the terminal:
.. 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:
.. code-block:: bash
platform linux -- Python 3.10.0, pytest-6.2.5, py-1.10.0, pluggy-1.0.0
platform linux -- Python 3.13.1, pytest-8.3.4, pluggy-1.5.0
rootdir: /code
plugins: asyncio-0.16.0, cov-3.0.0
plugins: cov-6.0.0, asyncio-0.24.0
asyncio: mode=Mode.STRICT, default_loop_scope=None
collected 2 items
monitoringdaemon/tests.py .. [100%]

View File

@ -84,7 +84,7 @@ Create next structure in the project root directory. All files are empty. That's
Initial project layout:
.. code-block:: bash
.. code-block:: text
./
├── movies/
@ -109,7 +109,7 @@ Now it's time to install the project requirements. We will use next packages:
Put next lines into the ``requirements.txt`` file:
.. code-block:: bash
.. code-block:: text
dependency-injector
pyyaml
@ -134,7 +134,7 @@ We will create a script that creates database files.
First add the folder ``data/`` in the root of the project and then add the file
``fixtures.py`` inside of it:
.. code-block:: bash
.. code-block:: text
:emphasize-lines: 2-3
./
@ -205,13 +205,13 @@ Now run in the terminal:
You should see:
.. code-block:: bash
.. code-block:: text
OK
Check that files ``movies.csv`` and ``movies.db`` have appeared in the ``data/`` folder:
.. code-block:: bash
.. code-block:: text
:emphasize-lines: 4-5
./
@ -289,7 +289,7 @@ After each step we will add the provider to the container.
Create the ``entities.py`` in the ``movies`` package:
.. code-block:: bash
.. code-block:: text
:emphasize-lines: 10
./
@ -356,7 +356,7 @@ Let's move on to the finders.
Create the ``finders.py`` in the ``movies`` package:
.. code-block:: bash
.. code-block:: text
:emphasize-lines: 11
./
@ -465,7 +465,7 @@ The configuration file is ready. Move on to the lister.
Create the ``listers.py`` in the ``movies`` package:
.. code-block:: bash
.. code-block:: text
:emphasize-lines: 12
./
@ -613,7 +613,7 @@ Run in the terminal:
You should see:
.. code-block:: plain
.. code-block:: text
Francis Lawrence movies:
- Movie(title='The Hunger Games: Mockingjay - Part 2', year=2015, director='Francis Lawrence')
@ -752,7 +752,7 @@ Run in the terminal:
You should see:
.. code-block:: plain
.. code-block:: text
Francis Lawrence movies:
- Movie(title='The Hunger Games: Mockingjay - Part 2', year=2015, director='Francis Lawrence')
@ -868,7 +868,7 @@ Run in the terminal line by line:
The output should be similar for each command:
.. code-block:: plain
.. code-block:: text
Francis Lawrence movies:
- Movie(title='The Hunger Games: Mockingjay - Part 2', year=2015, director='Francis Lawrence')
@ -888,7 +888,7 @@ We will use `pytest <https://docs.pytest.org/en/stable/>`_ and
Create ``tests.py`` in the ``movies`` package:
.. code-block:: bash
.. code-block:: text
:emphasize-lines: 13
./
@ -911,7 +911,7 @@ Create ``tests.py`` in the ``movies`` package:
and put next into it:
.. code-block:: python
:emphasize-lines: 36,51
:emphasize-lines: 41,50
"""Tests module."""
@ -941,13 +941,18 @@ and put next into it:
return container
def test_movies_directed_by(container):
@pytest.fixture
def finder_mock(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"),
]
return finder_mock
def test_movies_directed_by(container, finder_mock):
with container.finder.override(finder_mock):
lister = container.lister()
movies = lister.movies_directed_by("Jon Favreau")
@ -956,13 +961,7 @@ and put next into it:
assert movies[0].title == "The Jungle Book"
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"),
]
def test_movies_released_in(container, finder_mock):
with container.finder.override(finder_mock):
lister = container.lister()
movies = lister.movies_released_in(2015)
@ -978,7 +977,7 @@ Run in the terminal:
You should see:
.. code-block::
.. code-block:: text
platform darwin -- Python 3.10.0, pytest-6.2.5, py-1.10.0, pluggy-1.0.0
plugins: cov-3.0.0
@ -995,9 +994,9 @@ You should see:
movies/entities.py 7 1 86%
movies/finders.py 26 13 50%
movies/listers.py 8 0 100%
movies/tests.py 23 0 100%
movies/tests.py 24 0 100%
------------------------------------------
TOTAL 89 30 66%
TOTAL 90 30 67%
.. note::

View File

@ -280,7 +280,7 @@ Now let's fill in the layout.
Put next into the ``base.html``:
.. code-block:: html
.. code-block:: jinja
<!doctype html>
<html lang="en">
@ -313,7 +313,7 @@ And put something to the index page.
Put next into the ``index.html``:
.. code-block:: html
.. code-block:: jinja
{% extends "base.html" %}

View File

@ -64,7 +64,7 @@ FastAPI example:
@app.api_route("/")
@inject
async def index(service: Service = Depends(Provide[Container.service])):
async def index(service: Annotated[Service, Depends(Provide[Container.service])]):
value = await service.process()
return {"result": value}
@ -127,6 +127,7 @@ To inject the provider itself use ``Provide[foo.provider]``:
def foo(bar_provider: Factory[Bar] = Provide[Container.bar.provider]):
bar = bar_provider(argument="baz")
...
You can also use ``Provider[foo]`` for injecting the provider itself:
.. code-block:: python
@ -254,13 +255,43 @@ To inject a container use special identifier ``<container>``:
Making injections into modules and class attributes
---------------------------------------------------
You can use wiring to make injections into modules and class attributes.
You can use wiring to make injections into modules and class attributes. Both the classic marker
syntax and the ``Annotated`` form are supported.
Classic marker syntax:
.. code-block:: python
service: Service = Provide[Container.service]
class Main:
service: Service = Provide[Container.service]
Full example of the classic marker syntax:
.. literalinclude:: ../examples/wiring/example_attribute.py
:language: python
:lines: 3-
:emphasize-lines: 14,19
Annotated form (Python 3.9+):
.. code-block:: python
from typing import Annotated
service: Annotated[Service, Provide[Container.service]]
class Main:
service: Annotated[Service, Provide[Container.service]]
Full example of the annotated form:
.. literalinclude:: ../examples/wiring/example_attribute_annotated.py
:language: python
:lines: 3-
:emphasize-lines: 16,21
You could also use string identifiers to avoid a dependency on a container:
.. code-block:: python
@ -601,6 +632,36 @@ or with a single container ``register_loader_containers(container)`` multiple ti
To unregister a container use ``unregister_loader_containers(container)``.
Wiring module will uninstall the import hook when unregister last container.
Few notes on performance
------------------------
``.wire()`` utilize caching to speed up the wiring process. At the end it clears the cache to avoid memory leaks.
But this may not always be desirable, when you want to keep the cache for the next wiring
(e.g. due to usage of multiple containers or during unit tests).
To keep the cache after wiring, you can set flag ``keep_cache=True`` (works with ``WiringConfiguration`` too):
.. code-block:: python
container1.wire(
modules=["yourapp.module1", "yourapp.module2"],
keep_cache=True,
)
container2.wire(
modules=["yourapp.module2", "yourapp.module3"],
keep_cache=True,
)
...
and then clear it manually when you need it:
.. code-block:: python
from dependency_injector.wiring import clear_cache
clear_cache()
Integration with other frameworks
---------------------------------
@ -632,5 +693,6 @@ Take a look at other application examples:
- :ref:`fastapi-example`
- :ref:`fastapi-redis-example`
- :ref:`fastapi-sqlalchemy-example`
- :ref:`fastdepends-example`
.. disqus::

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -12,13 +12,13 @@ Build the Docker image:
.. code-block:: bash
docker-compose build
docker compose build
Run the docker-compose environment:
.. code-block:: bash
docker-compose up
docker compose up
The output should be something like:
@ -54,16 +54,16 @@ To run the tests do:
.. 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:
.. code-block::
platform linux -- Python 3.10.9, pytest-7.2.0, pluggy-1.0.0
platform linux -- Python 3.13.1, pytest-8.3.4, pluggy-1.5.0
rootdir: /code
plugins: cov-4.0.0, asyncio-0.20.3
collected 1 item
plugins: cov-6.0.0, asyncio-0.24.0, anyio-4.7.0
asyncio: mode=Mode.STRICT, default_loop_scope=None
fastapiredis/tests.py . [100%]

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -1,11 +1,14 @@
"""Endpoints module."""
from typing import Annotated
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 .services import UserService
from .repositories import NotFoundError
from .services import UserService
router = APIRouter()
@ -13,7 +16,7 @@ router = APIRouter()
@router.get("/users")
@inject
def get_list(
user_service: UserService = Depends(Provide[Container.user_service]),
user_service: Annotated[UserService, Depends(Provide[Container.user_service])],
):
return user_service.get_users()
@ -22,7 +25,7 @@ def get_list(
@inject
def get_by_id(
user_id: int,
user_service: UserService = Depends(Provide[Container.user_service]),
user_service: Annotated[UserService, Depends(Provide[Container.user_service])],
):
try:
return user_service.get_user_by_id(user_id)
@ -33,7 +36,7 @@ def get_by_id(
@router.post("/users", status_code=status.HTTP_201_CREATED)
@inject
def add(
user_service: UserService = Depends(Provide[Container.user_service]),
user_service: Annotated[UserService, Depends(Provide[Container.user_service])],
):
return user_service.create_user()
@ -42,8 +45,8 @@ def add(
@inject
def remove(
user_id: int,
user_service: UserService = Depends(Provide[Container.user_service]),
):
user_service: Annotated[UserService, Depends(Provide[Container.user_service])],
) -> Response:
try:
user_service.delete_user_by_id(user_id)
except NotFoundError:

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -18,10 +18,9 @@ SQLITE_FILE = DIR / "movies.db"
def create_csv(movies_data, path):
with open(path, "w") as opened_file:
with open(path, "w", newline="") as opened_file:
writer = csv.writer(opened_file)
for row in movies_data:
writer.writerow(row)
writer.writerows(movies_data)
def create_sqlite(movies_data, path):

View File

@ -29,7 +29,7 @@ class CsvMovieFinder(MovieFinder):
super().__init__(movie_factory)
def find_all(self) -> List[Movie]:
with open(self._csv_file_path) as csv_file:
with open(self._csv_file_path, newline="") as csv_file:
csv_reader = csv.reader(csv_file, delimiter=self._delimiter)
return [self._movie_factory(*row) for row in csv_reader]

View File

@ -26,13 +26,18 @@ def container():
return container
def test_movies_directed_by(container):
@pytest.fixture
def finder_mock(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"),
]
return finder_mock
def test_movies_directed_by(container, finder_mock):
with container.finder.override(finder_mock):
lister = container.lister()
movies = lister.movies_directed_by("Jon Favreau")
@ -41,13 +46,7 @@ def test_movies_directed_by(container):
assert movies[0].title == "The Jungle Book"
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"),
]
def test_movies_released_in(container, finder_mock):
with container.finder.override(finder_mock):
lister = container.lister()
movies = lister.movies_released_in(2015)

View File

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

View File

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

View File

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

View File

@ -0,0 +1,39 @@
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

@ -0,0 +1,59 @@
#!/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

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

View File

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

View File

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

View File

@ -3,10 +3,12 @@
import sys
import logging
from concurrent.futures import ThreadPoolExecutor
from contextlib import contextmanager
from dependency_injector import containers, providers
@contextmanager
def init_thread_pool(max_workers: int):
thread_pool = ThreadPoolExecutor(max_workers=max_workers)
yield thread_pool

View File

@ -2,10 +2,10 @@
from dependency_injector import containers, providers
from dependency_injector.wiring import Provide, inject
from typing import Annotated
class Service:
...
class Service: ...
class Container(containers.DeclarativeContainer):
@ -13,9 +13,16 @@ class Container(containers.DeclarativeContainer):
service = providers.Factory(Service)
# You can place marker on parameter default value
@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__":

View File

@ -0,0 +1,31 @@
"""Wiring attribute example with Annotated."""
from typing import Annotated
from dependency_injector import containers, providers
from dependency_injector.wiring import Provide
class Service:
...
class Container(containers.DeclarativeContainer):
service = providers.Factory(Service)
service: Annotated[Service, Provide[Container.service]]
class Main:
service: Annotated[Service, Provide[Container.service]]
if __name__ == "__main__":
container = Container()
container.wire(modules=[__name__])
assert isinstance(service, Service)
assert isinstance(Main.service, Service)

122
pyproject.toml Normal file
View File

@ -0,0 +1,122 @@
[build-system]
requires = ["setuptools", "Cython>=3.1.1"]
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.8"
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.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"]
dependencies = [
# typing.Annotated since v3.9
# typing.Self since v3.11
"typing-extensions; python_version<'3.11'",
]
[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"
combine_as_imports = true
[tool.pylint.main]
ignore = ["tests"]
[tool.pylint.design]
min-public-methods = 0
max-public-methods = 30
[tool.pytest.ini_options]
testpaths = ["tests/unit/"]
asyncio_mode = "auto"
asyncio_default_fixture_loop_scope = "function"
markers = [
"pydantic: Tests with Pydantic as a dependency",
]
filterwarnings = [
"ignore::dependency_injector.wiring.DIWiringWarning",
"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:Please import \\`.*?\\` from the \\`scipy(.*?)\\` namespace(.*):DeprecationWarning",
"ignore:\\`scipy(.*?)\\` is deprecated(.*):DeprecationWarning",
]

View File

@ -1,9 +1,11 @@
cython==0.29.37
cython==3.1.1
setuptools
pytest
pytest-asyncio
tox
coverage
flake8
flake8-pyproject
pydocstyle
sphinx_autobuild
pip
@ -11,10 +13,13 @@ mypy
pyyaml
httpx
fastapi
pydantic==1.10.17
pydantic
pydantic-settings
numpy
scipy
boto3
mypy_boto3_s3
typing_extensions
fast-depends
-r requirements-ext.txt

View File

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

View File

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

View File

@ -2,12 +2,13 @@
max_line_length = 120
max_complexity = 10
exclude = types.py
extend-ignore = E203,E701
per-file-ignores =
examples/demo/*: F841
examples/containers/traverse.py: E501
examples/providers/async.py: F841
examples/providers/async_overriding.py: F841
examples/wiring/*: F841
examples/wiring/*: F821,F841
[pydocstyle]
ignore = D100,D101,D102,D105,D106,D107,D203,D213

160
setup.py
View File

@ -1,127 +1,55 @@
"""`Dependency injector` setup script."""
import os
import re
import sys
from setuptools import setup, Extension
from Cython.Build import cythonize
from Cython.Compiler import Options
from setuptools import Extension, setup
def _open(filename):
if sys.version_info[0] == 2:
return open(filename)
return open(filename, encoding="utf-8")
# 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)
debug = os.environ.get("DEPENDENCY_INJECTOR_DEBUG_MODE") == "1"
limited_api = (
os.environ.get("DEPENDENCY_INJECTOR_LIMITED_API") == "1"
and sys.implementation.name == "cpython"
)
defined_macros = []
options = {}
compiler_directives = {
"language_level": 3,
"profile": debug,
"linetrace": debug,
}
Options.annotate = debug
# Adding debug options:
if os.environ.get("DEPENDENCY_INJECTOR_DEBUG_MODE") == "1":
defined_macros["CYTHON_TRACE"] = 1
defined_macros["CYTHON_TRACE_NOGIL"] = 1
defined_macros["CYTHON_CLINE_IN_TRACEBACK"] = 1
if debug:
limited_api = False # line tracing is not part of the Limited API
defined_macros.extend(
[
("CYTHON_TRACE", "1"),
("CYTHON_TRACE_NOGIL", "1"),
("CYTHON_CLINE_IN_TRACEBACK", "1"),
]
)
if limited_api:
options.setdefault("bdist_wheel", {})
options["bdist_wheel"]["py_limited_api"] = "cp38"
defined_macros.append(("Py_LIMITED_API", "0x03080000"))
setup(name="dependency-injector",
version=version,
description="Dependency injection framework for Python",
long_description=description,
author="Roman Mogylatov",
author_email="rmogilatov@gmail.com",
maintainer="Roman Mogylatov",
maintainer_email="rmogilatov@gmail.com",
url="https://github.com/ets-labs/python-dependency-injector",
download_url="https://pypi.python.org/pypi/dependency_injector",
packages=[
"dependency_injector",
"dependency_injector.ext",
setup(
options=options,
ext_modules=cythonize(
[
Extension(
"*",
["src/**/*.pyx"],
define_macros=defined_macros,
py_limited_api=limited_api,
),
],
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 :: 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",
])
annotate=debug,
show_all_warnings=True,
compiler_directives=compiler_directives,
),
)

View File

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

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,18 @@
from typing import Any, Dict
from .providers import Provider
class DependencyResolver:
def __init__(
self,
kwargs: Dict[str, Any],
injections: Dict[str, Provider[Any]],
closings: Dict[str, Provider[Any]],
/,
) -> None: ...
def __enter__(self) -> Dict[str, Any]: ...
def __exit__(self, *exc_info: Any) -> None: ...
async def __aenter__(self) -> Dict[str, Any]: ...
async def __aexit__(self, *exc_info: Any) -> None: ...
def _isawaitable(instance: Any) -> bool: ...

View File

@ -1,88 +1,93 @@
"""Wiring optimizations module."""
import asyncio
import collections.abc
import functools
import inspect
import types
from asyncio import gather
from collections.abc import Awaitable
from inspect import CO_ITERABLE_COROUTINE
from types import CoroutineType, GeneratorType
from . import providers
from .wiring import _Marker, PatchedCallable
from .providers cimport Provider
from .providers cimport Provider, Resource
from .wiring import _Marker
def _get_sync_patched(fn, patched: PatchedCallable):
@functools.wraps(fn)
def _patched(*args, **kwargs):
cdef object result
cdef inline bint _is_injectable(dict kwargs, object name):
return name not in kwargs or isinstance(kwargs[name], _Marker)
cdef class DependencyResolver:
cdef dict kwargs
cdef dict to_inject
cdef object arg_key
cdef dict injections
cdef dict closings
def __init__(self, dict kwargs, dict injections, dict closings, /):
self.kwargs = kwargs
self.to_inject = kwargs.copy()
self.injections = injections
self.closings = closings
async def _await_injection(self, name: str, value: object, /) -> None:
self.to_inject[name] = await value
cdef void _handle_injections_sync(self):
cdef Provider provider
to_inject = kwargs.copy()
for arg_key, provider in patched.injections.items():
if arg_key not in kwargs or isinstance(kwargs[arg_key], _Marker):
to_inject[arg_key] = provider()
for name, provider in self.injections.items():
if _is_injectable(self.kwargs, name):
self.to_inject[name] = provider()
result = fn(*args, **to_inject)
cdef list _handle_injections_async(self):
cdef list to_await = []
cdef Provider provider
if patched.closing:
for arg_key, provider in patched.closing.items():
if arg_key in kwargs and not isinstance(kwargs[arg_key], _Marker):
continue
if not isinstance(provider, providers.Resource):
continue
for name, provider in self.injections.items():
if _is_injectable(self.kwargs, name):
provide = provider()
if provider.is_async_mode_enabled() or _isawaitable(provide):
to_await.append(self._await_injection(name, provide))
else:
self.to_inject[name] = provide
return to_await
cdef void _handle_closings_sync(self):
cdef Provider provider
for name, provider in self.closings.items():
if _is_injectable(self.kwargs, name) and isinstance(provider, Resource):
provider.shutdown()
return result
return _patched
async def _async_inject(object fn, tuple args, dict kwargs, dict injections, dict closings):
cdef object result
cdef dict to_inject
cdef list to_inject_await = []
cdef list to_close_await = []
cdef object arg_key
cdef list _handle_closings_async(self):
cdef list to_await = []
cdef Provider provider
to_inject = kwargs.copy()
for arg_key, provider in injections.items():
if arg_key not in kwargs or isinstance(kwargs[arg_key], _Marker):
provide = provider()
if provider.is_async_mode_enabled():
to_inject_await.append((arg_key, provide))
elif _isawaitable(provide):
to_inject_await.append((arg_key, provide))
else:
to_inject[arg_key] = provide
for name, provider in self.closings.items():
if _is_injectable(self.kwargs, name) and isinstance(provider, Resource):
if _isawaitable(shutdown := provider.shutdown()):
to_await.append(shutdown)
if to_inject_await:
async_to_inject = await asyncio.gather(*(provide for _, provide in to_inject_await))
for provide, (injection, _) in zip(async_to_inject, to_inject_await):
to_inject[injection] = provide
return to_await
result = await fn(*args, **to_inject)
def __enter__(self):
self._handle_injections_sync()
return self.to_inject
if closings:
for arg_key, provider in closings.items():
if arg_key in kwargs and isinstance(kwargs[arg_key], _Marker):
continue
if not isinstance(provider, providers.Resource):
continue
shutdown = provider.shutdown()
if _isawaitable(shutdown):
to_close_await.append(shutdown)
def __exit__(self, *_):
self._handle_closings_sync()
await asyncio.gather(*to_close_await)
async def __aenter__(self):
if to_await := self._handle_injections_async():
await gather(*to_await)
return self.to_inject
return result
async def __aexit__(self, *_):
if to_await := self._handle_closings_async():
await gather(*to_await)
cdef bint _isawaitable(object instance):
"""Return true if object can be passed to an ``await`` expression."""
return (isinstance(instance, types.CoroutineType) or
isinstance(instance, types.GeneratorType) and
bool(instance.gi_code.co_flags & inspect.CO_ITERABLE_COROUTINE) or
isinstance(instance, collections.abc.Awaitable))
return (isinstance(instance, CoroutineType) or
isinstance(instance, GeneratorType) and
bool(instance.gi_code.co_flags & CO_ITERABLE_COROUTINE) or
isinstance(instance, Awaitable))

File diff suppressed because it is too large Load Diff

View File

@ -1,24 +1,28 @@
from pathlib import Path
from typing import (
Generic,
Type,
Dict,
List,
Tuple,
Optional,
Any,
Union,
ClassVar,
Awaitable,
Callable as _Callable,
ClassVar,
Dict,
Generic,
Iterable,
Iterator,
List,
Optional,
Tuple,
Type,
TypeVar,
Awaitable,
Union,
overload,
)
from .providers import Provider, Self, ProviderParent
try:
from typing import Self as _Self
except ImportError:
from typing_extensions import Self as _Self
from .providers import Provider, Resource, Self, ProviderParent
C_Base = TypeVar("C_Base", bound="Container")
C = TypeVar("C", bound="DeclarativeContainer")
@ -26,51 +30,66 @@ C_Overriding = TypeVar("C_Overriding", bound="DeclarativeContainer")
T = TypeVar("T")
TT = TypeVar("TT")
class WiringConfiguration:
modules: List[Any]
packages: List[Any]
from_package: Optional[str]
auto_wire: bool
def __init__(self, modules: Optional[Iterable[Any]] = None, packages: Optional[Iterable[Any]] = None, from_package: Optional[str] = None, auto_wire: bool = True) -> None: ...
keep_cache: bool
def __init__(
self,
modules: Optional[Iterable[Any]] = None,
packages: Optional[Iterable[Any]] = None,
from_package: Optional[str] = None,
auto_wire: bool = True,
keep_cache: bool = False,
) -> None: ...
class Container:
provider_type: Type[Provider] = Provider
providers: Dict[str, Provider]
dependencies: Dict[str, Provider]
overridden: Tuple[Provider]
provider_type: Type[Provider[Any]] = Provider
providers: Dict[str, Provider[Any]]
dependencies: Dict[str, Provider[Any]]
overridden: Tuple[Provider[Any], ...]
wiring_config: WiringConfiguration
auto_load_config: bool = True
__self__: Self
def __init__(self) -> None: ...
def __deepcopy__(self, memo: Optional[Dict[str, Any]]) -> Provider: ...
def __setattr__(self, name: str, value: Union[Provider, Any]) -> None: ...
def __getattr__(self, name: str) -> Provider: ...
def __deepcopy__(self, memo: Optional[Dict[str, Any]]) -> _Self: ...
def __setattr__(self, name: str, value: Union[Provider[Any], Any]) -> None: ...
def __getattr__(self, name: str) -> Provider[Any]: ...
def __delattr__(self, name: str) -> None: ...
def set_providers(self, **providers: Provider): ...
def set_provider(self, name: str, provider: Provider) -> None: ...
def set_providers(self, **providers: Provider[Any]) -> None: ...
def set_provider(self, name: str, provider: Provider[Any]) -> None: ...
def override(self, overriding: Union[Container, Type[Container]]) -> None: ...
def override_providers(self, **overriding_providers: Union[Provider, Any]) -> ProvidersOverridingContext[C_Base]: ...
def override_providers(
self, **overriding_providers: Union[Provider[Any], Any]
) -> ProvidersOverridingContext[C_Base]: ...
def reset_last_overriding(self) -> None: ...
def reset_override(self) -> None: ...
def is_auto_wiring_enabled(self) -> bool: ...
def wire(self, modules: Optional[Iterable[Any]] = None, packages: Optional[Iterable[Any]] = None, from_package: Optional[str] = None) -> None: ...
def wire(
self,
modules: Optional[Iterable[Any]] = None,
packages: Optional[Iterable[Any]] = None,
from_package: Optional[str] = None,
) -> None: ...
def unwire(self) -> None: ...
def init_resources(self) -> Optional[Awaitable]: ...
def shutdown_resources(self) -> Optional[Awaitable]: ...
def init_resources(self, resource_type: Type[Resource[Any]] = Resource) -> Optional[Awaitable[None]]: ...
def shutdown_resources(self, resource_type: Type[Resource[Any]] = Resource) -> Optional[Awaitable[None]]: ...
def load_config(self) -> None: ...
def apply_container_providers_overridings(self) -> None: ...
def reset_singletons(self) -> SingletonResetContext[C_Base]: ...
def check_dependencies(self) -> None: ...
def from_schema(self, schema: Dict[Any, Any]) -> None: ...
def from_yaml_schema(self, filepath: Union[Path, str], loader: Optional[Any]=None) -> None: ...
def from_yaml_schema(
self, filepath: Union[Path, str], loader: Optional[Any] = None
) -> None: ...
def from_json_schema(self, filepath: Union[Path, str]) -> None: ...
@overload
def resolve_provider_name(self, provider: Provider) -> str: ...
def resolve_provider_name(self, provider: Provider[Any]) -> str: ...
@classmethod
@overload
def resolve_provider_name(cls, provider: Provider) -> str: ...
def resolve_provider_name(cls, provider: Provider[Any]) -> str: ...
@property
def parent(self) -> Optional[ProviderParent]: ...
@property
@ -82,40 +101,37 @@ class Container:
@overload
def traverse(cls, types: Optional[Iterable[Type[TT]]] = None) -> Iterator[TT]: ...
class DynamicContainer(Container): ...
class DeclarativeContainer(Container):
cls_providers: ClassVar[Dict[str, Provider]]
inherited_providers: ClassVar[Dict[str, Provider]]
def __init__(self, **overriding_providers: Union[Provider, Any]) -> None: ...
cls_providers: ClassVar[Dict[str, Provider[Any]]]
inherited_providers: ClassVar[Dict[str, Provider[Any]]]
def __init__(self, **overriding_providers: Union[Provider[Any], Any]) -> None: ...
@classmethod
def override(cls, overriding: Union[Container, Type[Container]]) -> None: ...
@classmethod
def override_providers(cls, **overriding_providers: Union[Provider, Any]) -> ProvidersOverridingContext[C_Base]: ...
def override_providers(
cls, **overriding_providers: Union[Provider[Any], Any]
) -> ProvidersOverridingContext[C_Base]: ...
@classmethod
def reset_last_overriding(cls) -> None: ...
@classmethod
def reset_override(cls) -> None: ...
class ProvidersOverridingContext(Generic[T]):
def __init__(self, container: T, overridden_providers: Iterable[Union[Provider, Any]]) -> None: ...
def __init__(
self, container: T, overridden_providers: Iterable[Union[Provider[Any], Any]]
) -> None: ...
def __enter__(self) -> T: ...
def __exit__(self, *_: Any) -> None: ...
class SingletonResetContext(Generic[T]):
def __init__(self, container: T): ...
def __enter__(self) -> T: ...
def __exit__(self, *_: Any) -> None: ...
def override(container: Type[C]) -> _Callable[[Type[C_Overriding]], Type[C_Overriding]]: ...
def override(
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: ...

View File

@ -1,61 +1,37 @@
"""Containers module."""
import asyncio
import contextlib
import copy as copy_module
import json
import sys
import importlib
import inspect
import warnings
try:
import asyncio
except ImportError:
asyncio = None
try:
import yaml
except ImportError:
yaml = None
import six
from . import providers, errors
from .providers cimport __is_future_or_coroutine
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:
"""Container wiring configuration."""
def __init__(self, modules=None, packages=None, from_package=None, auto_wire=True):
def __init__(self, modules=None, packages=None, from_package=None, auto_wire=True, keep_cache=False):
self.modules = [*modules] if modules else []
self.packages = [*packages] if packages else []
self.from_package = from_package
self.auto_wire = auto_wire
self.keep_cache = keep_cache
def __deepcopy__(self, memo=None):
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, self.keep_cache)
class Container(object):
class Container:
"""Abstract container."""
@ -201,7 +177,7 @@ class DynamicContainer(Container):
:rtype: None
"""
for name, provider in six.iteritems(providers):
for name, provider in providers.items():
setattr(self, name, provider)
def set_provider(self, name, provider):
@ -234,7 +210,7 @@ class DynamicContainer(Container):
self.overridden += (overriding,)
for name, provider in six.iteritems(overriding.providers):
for name, provider in overriding.providers.items():
try:
getattr(self, name).override(provider)
except AttributeError:
@ -250,7 +226,7 @@ class DynamicContainer(Container):
:rtype: None
"""
overridden_providers = []
for name, overriding_provider in six.iteritems(overriding_providers):
for name, overriding_provider in overriding_providers.items():
container_provider = getattr(self, name)
container_provider.override(overriding_provider)
overridden_providers.append(container_provider)
@ -266,7 +242,7 @@ class DynamicContainer(Container):
self.overridden = self.overridden[:-1]
for provider in six.itervalues(self.providers):
for provider in self.providers.values():
provider.reset_last_overriding()
def reset_override(self):
@ -276,14 +252,14 @@ class DynamicContainer(Container):
"""
self.overridden = tuple()
for provider in six.itervalues(self.providers):
for provider in self.providers.values():
provider.reset_override()
def is_auto_wiring_enabled(self):
"""Check if auto wiring is needed."""
return self.wiring_config.auto_wire is True
def wire(self, modules=None, packages=None, from_package=None):
def wire(self, modules=None, packages=None, from_package=None, keep_cache=None):
"""Wire container providers with provided packages and modules.
:rtype: None
@ -314,10 +290,14 @@ class DynamicContainer(Container):
if not modules and not packages:
return
if keep_cache is None:
keep_cache = self.wiring_config.keep_cache
wire(
container=self,
modules=modules,
packages=packages,
keep_cache=keep_cache,
)
if modules:
@ -335,11 +315,15 @@ class DynamicContainer(Container):
self.wired_to_modules.clear()
self.wired_to_packages.clear()
def init_resources(self):
def init_resources(self, resource_type=providers.Resource):
"""Initialize all container resources."""
if not issubclass(resource_type, providers.Resource):
raise TypeError("resource_type must be a subclass of Resource provider")
futures = []
for provider in self.traverse(types=[providers.Resource]):
for provider in self.traverse(types=[resource_type]):
resource = provider.init()
if __is_future_or_coroutine(resource):
@ -348,8 +332,12 @@ class DynamicContainer(Container):
if futures:
return asyncio.gather(*futures)
def shutdown_resources(self):
def shutdown_resources(self, resource_type=providers.Resource):
"""Shutdown all container resources."""
if not issubclass(resource_type, providers.Resource):
raise TypeError("resource_type must be a subclass of Resource provider")
def _independent_resources(resources):
for resource in resources:
for other_resource in resources:
@ -380,7 +368,7 @@ class DynamicContainer(Container):
for resource in resources_to_shutdown:
resource.shutdown()
resources = list(self.traverse(types=[providers.Resource]))
resources = list(self.traverse(types=[resource_type]))
if any(resource.is_async_mode_enabled() for resource in resources):
return _async_ordered_shutdown(resources)
else:
@ -495,13 +483,13 @@ class DeclarativeContainerMetaClass(type):
containers = {
name: container
for name, container in six.iteritems(attributes)
for name, container in attributes.items()
if is_container(container)
}
cls_providers = {
name: provider
for name, provider in six.iteritems(attributes)
for name, provider in attributes.items()
if isinstance(provider, providers.Provider) and not isinstance(provider, providers.Self)
}
@ -509,7 +497,7 @@ class DeclarativeContainerMetaClass(type):
name: provider
for base in bases
if is_container(base) and base is not DynamicContainer
for name, provider in six.iteritems(base.providers)
for name, provider in base.providers.items()
}
all_providers = {}
@ -536,10 +524,10 @@ class DeclarativeContainerMetaClass(type):
self.set_container(cls)
cls.__self__ = self
for provider in six.itervalues(cls.providers):
for provider in cls.providers.values():
_check_provider_type(cls, provider)
for provider in six.itervalues(cls.cls_providers):
for provider in cls.cls_providers.values():
if isinstance(provider, providers.CHILD_PROVIDERS):
provider.assign_parent(cls)
@ -641,8 +629,7 @@ class DeclarativeContainerMetaClass(type):
return self
@six.add_metaclass(DeclarativeContainerMetaClass)
class DeclarativeContainer(Container):
class DeclarativeContainer(Container, metaclass=DeclarativeContainerMetaClass):
"""Declarative inversion of control container.
.. code-block:: python
@ -767,7 +754,7 @@ class DeclarativeContainer(Container):
cls.overridden += (overriding,)
for name, provider in six.iteritems(overriding.cls_providers):
for name, provider in overriding.cls_providers.items():
try:
getattr(cls, name).override(provider)
except AttributeError:
@ -784,7 +771,7 @@ class DeclarativeContainer(Container):
cls.overridden = cls.overridden[:-1]
for provider in six.itervalues(cls.providers):
for provider in cls.providers.values():
provider.reset_last_overriding()
@classmethod
@ -795,7 +782,7 @@ class DeclarativeContainer(Container):
"""
cls.overridden = tuple()
for provider in six.itervalues(cls.providers):
for provider in cls.providers.values():
provider.reset_override()
@ -858,7 +845,7 @@ def copy(object base_container):
"""
def _get_memo_for_matching_names(new_providers, base_providers):
memo = {}
for new_provider_name, new_provider in six.iteritems(new_providers):
for new_provider_name, new_provider in new_providers.items():
if new_provider_name not in base_providers:
continue
source_provider = base_providers[new_provider_name]
@ -877,7 +864,7 @@ def copy(object base_container):
new_providers.update(providers.deepcopy(base_container.providers, memo))
new_providers.update(providers.deepcopy(new_container.cls_providers, memo))
for name, provider in six.iteritems(new_providers):
for name, provider in new_providers.items():
setattr(new_container, name, provider)
return new_container

View File

@ -10,3 +10,24 @@ class Error(Exception):
class NoSuchProviderError(Error, AttributeError):
"""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

@ -7,7 +7,6 @@ import warnings
from dependency_injector import providers
warnings.warn(
'Module "dependency_injector.ext.aiohttp" is deprecated since '
'version 4.0.0. Use "dependency_injector.wiring" module instead.',
@ -38,9 +37,11 @@ class View(providers.Callable):
def as_view(self):
"""Return aiohttp view function."""
@functools.wraps(self.provides)
async def _view(request, *args, **kwargs):
return await self.__call__(request, *args, **kwargs)
return _view
@ -49,6 +50,8 @@ class ClassBasedView(providers.Factory):
def as_view(self):
"""Return aiohttp view function."""
async def _view(request, *args, **kwargs):
return await self.__call__(request, *args, **kwargs)
return _view

View File

@ -1,23 +1,16 @@
from typing import Awaitable as _Awaitable
from typing import Any, Awaitable as _Awaitable, TypeVar
from dependency_injector import providers
T = TypeVar("T")
class Application(providers.Singleton): ...
class Application(providers.Singleton[T]): ...
class Extension(providers.Singleton[T]): ...
class Middleware(providers.DelegatedCallable[T]): ...
class MiddlewareFactory(providers.Factory[T]): ...
class View(providers.Callable[T]):
def as_view(self) -> _Awaitable[T]: ...
class Extension(providers.Singleton): ...
class Middleware(providers.DelegatedCallable): ...
class MiddlewareFactory(providers.Factory): ...
class View(providers.Callable):
def as_view(self) -> _Awaitable: ...
class ClassBasedView(providers.Factory):
def as_view(self) -> _Awaitable: ...
class ClassBasedView(providers.Factory[T]):
def as_view(self) -> _Awaitable[T]: ...

View File

@ -1,12 +1,12 @@
"""Flask extension module."""
from __future__ import absolute_import
import warnings
from flask import request as flask_request
from dependency_injector import providers, errors
from dependency_injector import errors, providers
warnings.warn(
'Module "dependency_injector.ext.flask" is deprecated since '
@ -45,6 +45,7 @@ class ClassBasedView(providers.Factory):
def as_view(provider, name=None):
"""Transform class-based view provider to view function."""
if isinstance(provider, providers.Factory):
def view(*args, **kwargs):
self = provider()
return self.dispatch_request(*args, **kwargs)
@ -52,12 +53,13 @@ def as_view(provider, name=None):
assert name, 'Argument "endpoint" is required for class-based views'
view.__name__ = name
elif isinstance(provider, providers.Callable):
def view(*args, **kwargs):
return provider(*args, **kwargs)
view.__name__ = provider.provides.__name__
else:
raise errors.Error('Undefined provider type')
raise errors.Error("Undefined provider type")
view.__doc__ = provider.provides.__doc__
view.__module__ = provider.provides.__module__
@ -65,14 +67,14 @@ def as_view(provider, name=None):
if isinstance(provider.provides, type):
view.view_class = provider.provides
if hasattr(provider.provides, 'decorators'):
if hasattr(provider.provides, "decorators"):
for decorator in provider.provides.decorators:
view = decorator(view)
if hasattr(provider.provides, 'methods'):
if hasattr(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
return view

View File

@ -1,24 +1,21 @@
from typing import Union, Optional, Callable as _Callable, Any
from typing import Any, Callable as _Callable, Optional, TypeVar, Union
from flask.wrappers import Request
from flask import request as flask_request
from dependency_injector import providers
request: providers.Object[Request]
T = TypeVar("T")
request: providers.Object[flask_request]
class Application(providers.Singleton[T]): ...
class Extension(providers.Singleton[T]): ...
class View(providers.Callable[T]):
def as_view(self) -> _Callable[..., T]: ...
class Application(providers.Singleton): ...
class ClassBasedView(providers.Factory[T]):
def as_view(self, name: str) -> _Callable[..., T]: ...
class Extension(providers.Singleton): ...
class View(providers.Callable):
def as_view(self) -> _Callable[..., Any]: ...
class ClassBasedView(providers.Factory):
def as_view(self, name: str) -> _Callable[..., Any]: ...
def as_view(provider: Union[View, ClassBasedView], name: Optional[str] = None) -> _Callable[..., Any]: ...
def as_view(
provider: Union[View[T], ClassBasedView[T]], name: Optional[str] = None
) -> _Callable[..., T]: ...

View File

@ -0,0 +1,61 @@
import sys
from typing import Any, Type
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
from dependency_injector.providers import Resource
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
:param resource_type: A :py:class:`~dependency_injector.resources.Resource`
subclass. Limits the resources to be initialized and shutdown.
"""
container: Container
resource_type: Type[Resource[Any]]
def __init__(
self,
container: Container,
resource_type: Type[Resource[Any]] = Resource,
) -> None:
self.container = container
self.resource_type = resource_type
def __call__(self, app: Any) -> Self:
return self
async def __aenter__(self) -> None:
result = self.container.init_resources(self.resource_type)
if result is not None:
await result
async def __aexit__(self, *exc_info: Any) -> None:
result = self.container.shutdown_resources(self.resource_type)
if result is not None:
await result

File diff suppressed because it is too large Load Diff

View File

@ -1,10 +1,6 @@
"""Providers module."""
try:
import asyncio
except ImportError:
asyncio = None
import functools
cimport cython
@ -19,11 +15,11 @@ cdef tuple __COROUTINE_TYPES
# Base providers
cdef class Provider(object):
cdef tuple __overridden
cdef Provider __last_overriding
cdef tuple __overrides
cdef int __async_mode
cdef class Provider:
cdef tuple _overridden
cdef Provider _last_overriding
cdef tuple _overrides
cdef int _async_mode
cpdef bint is_async_mode_enabled(self)
cpdef bint is_async_mode_disabled(self)
@ -34,32 +30,32 @@ cdef class Provider(object):
cdef class Object(Provider):
cdef object __provides
cdef object _provides
cpdef object _provide(self, tuple args, dict kwargs)
cdef class Self(Provider):
cdef object __container
cdef tuple __alt_names
cdef object _container
cdef tuple _alt_names
cdef class Delegate(Provider):
cdef object __provides
cdef object _provides
cpdef object _provide(self, tuple args, dict kwargs)
cdef class Aggregate(Provider):
cdef dict __providers
cdef dict _providers
cdef Provider __get_provider(self, object provider_name)
cdef class Dependency(Provider):
cdef object __instance_of
cdef object __default
cdef object __parent
cdef object _instance_of
cdef object _default
cdef object _parent
cdef class ExternalDependency(Dependency):
@ -67,21 +63,21 @@ cdef class ExternalDependency(Dependency):
cdef class DependenciesContainer(Object):
cdef dict __providers
cdef object __parent
cdef dict _providers
cdef object _parent
cpdef object _override_providers(self, object container)
# Callable providers
cdef class Callable(Provider):
cdef object __provides
cdef object _provides
cdef tuple __args
cdef int __args_len
cdef tuple _args
cdef int _args_len
cdef tuple __kwargs
cdef int __kwargs_len
cdef tuple _kwargs
cdef int _kwargs_len
cpdef object _provide(self, tuple args, dict kwargs)
@ -117,11 +113,11 @@ cdef class CoroutineDelegate(Delegate):
# Configuration providers
cdef class ConfigurationOption(Provider):
cdef tuple __name
cdef Configuration __root
cdef dict __children
cdef bint __required
cdef object __cache
cdef tuple _name
cdef Configuration _root
cdef dict _children
cdef bint _required
cdef object _cache
cdef class TypedConfigurationOption(Callable):
@ -129,22 +125,22 @@ cdef class TypedConfigurationOption(Callable):
cdef class Configuration(Object):
cdef str __name
cdef str _name
cdef bint __strict
cdef dict __children
cdef list __ini_files
cdef list __yaml_files
cdef list __json_files
cdef list __pydantic_settings
cdef dict _children
cdef list _ini_files
cdef list _yaml_files
cdef list _json_files
cdef list _pydantic_settings
cdef object __weakref__
# Factory providers
cdef class Factory(Provider):
cdef Callable __instantiator
cdef Callable _instantiator
cdef tuple __attributes
cdef int __attributes_len
cdef tuple _attributes
cdef int _attributes_len
cpdef object _provide(self, tuple args, dict kwargs)
@ -167,8 +163,8 @@ cdef class FactoryAggregate(Aggregate):
# Singleton providers
cdef class BaseSingleton(Provider):
cdef Factory __instantiator
cdef object __storage
cdef Factory _instantiator
cdef object _storage
cdef class Singleton(BaseSingleton):
@ -181,7 +177,7 @@ cdef class DelegatedSingleton(Singleton):
cdef class ThreadSafeSingleton(BaseSingleton):
cdef object __storage_lock
cdef object _storage_lock
cpdef object _provide(self, tuple args, dict kwargs)
@ -215,87 +211,87 @@ cdef class SingletonDelegate(Delegate):
# Miscellaneous providers
cdef class List(Provider):
cdef tuple __args
cdef int __args_len
cdef tuple _args
cdef int _args_len
cpdef object _provide(self, tuple args, dict kwargs)
cdef class Dict(Provider):
cdef tuple __kwargs
cdef int __kwargs_len
cdef tuple _kwargs
cdef int _kwargs_len
cpdef object _provide(self, tuple args, dict kwargs)
cdef class Resource(Provider):
cdef object __provides
cdef bint __initialized
cdef object __shutdowner
cdef object __resource
cdef object _provides
cdef bint _initialized
cdef object _shutdowner
cdef object _resource
cdef tuple __args
cdef int __args_len
cdef tuple _args
cdef int _args_len
cdef tuple __kwargs
cdef int __kwargs_len
cdef tuple _kwargs
cdef int _kwargs_len
cpdef object _provide(self, tuple args, dict kwargs)
cdef class Container(Provider):
cdef object __container_cls
cdef dict __overriding_providers
cdef object __container
cdef object __parent
cdef object _container_cls
cdef dict _overriding_providers
cdef object _container
cdef object _parent
cpdef object _provide(self, tuple args, dict kwargs)
cdef class Selector(Provider):
cdef object __selector
cdef dict __providers
cdef object _selector
cdef dict _providers
cpdef object _provide(self, tuple args, dict kwargs)
# Provided instance
cdef class ProvidedInstance(Provider):
cdef object __provides
cdef object _provides
cpdef object _provide(self, tuple args, dict kwargs)
cdef class AttributeGetter(Provider):
cdef object __provides
cdef object __name
cdef object _provides
cdef object _name
cpdef object _provide(self, tuple args, dict kwargs)
cdef class ItemGetter(Provider):
cdef object __provides
cdef object __name
cdef object _provides
cdef object _name
cpdef object _provide(self, tuple args, dict kwargs)
cdef class MethodCaller(Provider):
cdef object __provides
cdef tuple __args
cdef int __args_len
cdef tuple __kwargs
cdef int __kwargs_len
cdef object _provides
cdef tuple _args
cdef int _args_len
cdef tuple _kwargs
cdef int _kwargs_len
cpdef object _provide(self, tuple args, dict kwargs)
# Injections
cdef class Injection(object):
cdef object __value
cdef int __is_provider
cdef int __is_delegated
cdef int __call
cdef class Injection:
cdef object _value
cdef int _is_provider
cdef int _is_delegated
cdef int _call
cdef class PositionalInjection(Injection):
@ -303,7 +299,7 @@ cdef class PositionalInjection(Injection):
cdef class NamedInjection(Injection):
cdef object __name
cdef object _name
cpdef tuple parse_positional_injections(tuple args)
@ -313,13 +309,13 @@ cpdef tuple parse_named_injections(dict kwargs)
# Utils
cdef class OverridingContext(object):
cdef Provider __overridden
cdef Provider __overriding
cdef class OverridingContext:
cdef Provider _overridden
cdef Provider _overriding
cdef class BaseSingletonResetContext(object):
cdef object __singleton
cdef class BaseSingletonResetContext:
cdef object _singleton
cdef class SingletonResetContext(BaseSingletonResetContext):
@ -356,19 +352,19 @@ cpdef object deepcopy(object instance, dict memo=*)
# Inline helper functions
cdef inline object __get_name(NamedInjection self):
return self.__name
return self._name
cdef inline object __get_value(Injection self):
if self.__call == 0:
return self.__value
return self.__value()
if self._call == 0:
return self._value
return self._value()
cdef inline object __get_value_kwargs(Injection self, dict kwargs):
if self.__call == 0:
return self.__value
return self.__value(**kwargs)
if self._call == 0:
return self._value
return self._value(**kwargs)
cdef inline tuple __separate_prefixed_kwargs(dict kwargs):
@ -633,14 +629,14 @@ cdef inline object __async_result_callback(object future_result, object future):
cdef inline object __callable_call(Callable self, tuple args, dict kwargs, ):
return __call(
self.__provides,
self._provides,
args,
self.__args,
self.__args_len,
self._args,
self._args_len,
kwargs,
self.__kwargs,
self.__kwargs_len,
self.__async_mode,
self._kwargs,
self._kwargs_len,
self._async_mode,
)
@ -648,18 +644,18 @@ cdef inline object __factory_call(Factory self, tuple args, dict kwargs):
cdef object instance
instance = __call(
self.__instantiator.__provides,
self._instantiator._provides,
args,
self.__instantiator.__args,
self.__instantiator.__args_len,
self._instantiator._args,
self._instantiator._args_len,
kwargs,
self.__instantiator.__kwargs,
self.__instantiator.__kwargs_len,
self.__async_mode,
self._instantiator._kwargs,
self._instantiator._kwargs_len,
self._async_mode,
)
if self.__attributes_len > 0:
attributes = __provide_attributes(self.__attributes, self.__attributes_len)
if self._attributes_len > 0:
attributes = __provide_attributes(self._attributes, self._attributes_len)
is_future_instance = __is_future_or_coroutine(instance)
is_future_attributes = __is_future_or_coroutine(attributes)
@ -701,3 +697,10 @@ cdef inline object __future_result(object instance):
future_result = asyncio.Future()
future_result.set_result(instance)
return future_result
cdef class NullAwaitable:
pass
cdef NullAwaitable NULL_AWAITABLE

View File

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

File diff suppressed because it is too large Load Diff

View File

@ -1,27 +1,54 @@
"""Resources module."""
import abc
from typing import TypeVar, Generic, Optional
from abc import ABCMeta, abstractmethod
from typing import Any, ClassVar, Generic, Optional, Tuple, TypeVar
T = TypeVar("T")
class Resource(Generic[T], metaclass=abc.ABCMeta):
class Resource(Generic[T], metaclass=ABCMeta):
__slots__: ClassVar[Tuple[str, ...]] = ("args", "kwargs", "obj")
@abc.abstractmethod
def init(self, *args, **kwargs) -> Optional[T]:
...
obj: Optional[T]
def shutdown(self, resource: Optional[T]) -> None:
...
def __init__(self, *args: Any, **kwargs: Any) -> None:
self.args = args
self.kwargs = kwargs
self.obj = None
@abstractmethod
def init(self, *args: Any, **kwargs: Any) -> Optional[T]: ...
def shutdown(self, resource: Optional[T]) -> None: ...
def __enter__(self) -> Optional[T]:
self.obj = obj = self.init(*self.args, **self.kwargs)
return obj
def __exit__(self, *exc_info: Any) -> None:
self.shutdown(self.obj)
self.obj = None
class AsyncResource(Generic[T], metaclass=abc.ABCMeta):
class AsyncResource(Generic[T], metaclass=ABCMeta):
__slots__: ClassVar[Tuple[str, ...]] = ("args", "kwargs", "obj")
@abc.abstractmethod
async def init(self, *args, **kwargs) -> Optional[T]:
...
obj: Optional[T]
async def shutdown(self, resource: Optional[T]) -> None:
...
def __init__(self, *args: Any, **kwargs: Any) -> None:
self.args = args
self.kwargs = kwargs
self.obj = None
@abstractmethod
async def init(self, *args: Any, **kwargs: Any) -> Optional[T]: ...
async def shutdown(self, resource: Optional[T]) -> None: ...
async def __aenter__(self) -> Optional[T]:
self.obj = obj = await self.init(*self.args, **self.kwargs)
return obj
async def __aexit__(self, *exc_info: Any) -> None:
await self.shutdown(self.obj)
self.obj = None

View File

@ -91,16 +91,23 @@ class SchemaProcessorV1:
provider_type = _get_provider_cls(arg.get("provider"))
provides = arg.get("provides")
if provides:
if isinstance(provides, str) and provides.startswith("container."):
provides = self._resolve_provider(provides[len("container."):])
if isinstance(provides, str) and provides.startswith(
"container."
):
provides = self._resolve_provider(
provides[len("container.") :]
)
else:
provides = _import_string(provides)
provider_args.append(provides)
for provider_arg in arg.get("args", []):
if isinstance(provider_arg, str) \
and provider_arg.startswith("container."):
if isinstance(
provider_arg, str
) and provider_arg.startswith("container."):
provider_args.append(
self._resolve_provider(provider_arg[len("container."):]),
self._resolve_provider(
provider_arg[len("container.") :]
),
)
injection = provider_type(*provider_args)
@ -125,16 +132,23 @@ class SchemaProcessorV1:
provider_type = _get_provider_cls(arg.get("provider"))
provides = arg.get("provides")
if provides:
if isinstance(provides, str) and provides.startswith("container."):
provides = self._resolve_provider(provides[len("container."):])
if isinstance(provides, str) and provides.startswith(
"container."
):
provides = self._resolve_provider(
provides[len("container.") :]
)
else:
provides = _import_string(provides)
provider_args.append(provides)
for provider_arg in arg.get("args", []):
if isinstance(provider_arg, str) \
and provider_arg.startswith("container."):
if isinstance(
provider_arg, str
) and provider_arg.startswith("container."):
provider_args.append(
self._resolve_provider(provider_arg[len("container."):]),
self._resolve_provider(
provider_arg[len("container.") :]
),
)
injection = provider_type(*provider_args)
@ -190,10 +204,12 @@ def _get_provider_cls(provider_cls_name: str) -> Type[providers.Provider]:
if 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(provider_cls_name: str) -> Optional[Type[providers.Provider]]:
def _fetch_provider_cls_from_std(
provider_cls_name: str,
) -> Optional[Type[providers.Provider]]:
return getattr(providers, provider_cls_name, None)
@ -201,12 +217,16 @@ def _import_provider_cls(provider_cls_name: str) -> Optional[Type[providers.Prov
try:
cls = _import_string(provider_cls_name)
except (ImportError, ValueError) as exception:
raise SchemaError(f"Can not import provider \"{provider_cls_name}\"") from exception
raise SchemaError(
f'Can not import provider "{provider_cls_name}"'
) from exception
except AttributeError:
return None
else:
if isinstance(cls, type) and not issubclass(cls, providers.Provider):
raise SchemaError(f"Provider class \"{cls}\" is not a subclass of providers base class")
raise SchemaError(
f'Provider class "{cls}" is not a subclass of providers base class'
)
return cls

View File

@ -1,34 +1,45 @@
"""Wiring module."""
import functools
import inspect
import importlib
import importlib.machinery
import inspect
import pkgutil
import warnings
import sys
from contextlib import suppress
from inspect import isbuiltin, isclass
from types import ModuleType
from typing import (
Optional,
TYPE_CHECKING,
Any,
AsyncIterator,
Callable,
Dict,
Iterable,
Iterator,
Callable,
Any,
Tuple,
Dict,
Generic,
TypeVar,
Type,
Union,
List,
Optional,
Protocol,
Set,
Tuple,
Type,
TypeVar,
Union,
cast,
)
from warnings import warn
if sys.version_info < (3, 7):
from typing import GenericMeta
else:
class GenericMeta(type):
...
try:
from typing import Self
except ImportError:
from typing_extensions import Self
try:
from functools import cache
except ImportError:
from functools import lru_cache
cache = lru_cache(maxsize=None)
# Hotfix, see: https://github.com/ets-labs/python-dependency-injector/issues/362
if sys.version_info >= (3, 9):
@ -36,34 +47,64 @@ if sys.version_info >= (3, 9):
else:
GenericAlias = None
if sys.version_info >= (3, 9):
from typing import Annotated, get_args, get_origin
else:
try:
import fastapi.params
from typing_extensions import Annotated, get_args, get_origin
except ImportError:
fastapi = None
Annotated = object()
# For preventing NameError. Never executes
def get_args(hint):
return ()
def get_origin(tp):
return None
try:
import starlette.requests
except ImportError:
starlette = None
MARKER_EXTRACTORS: List[Callable[[Any], Any]] = []
INSPECT_EXCLUSION_FILTERS: List[Callable[[Any], bool]] = [isbuiltin]
with suppress(ImportError):
from fastapi.params import Depends as FastAPIDepends
def extract_marker_from_fastapi(param: Any) -> Any:
if isinstance(param, FastAPIDepends):
return param.dependency
return None
MARKER_EXTRACTORS.append(extract_marker_from_fastapi)
with suppress(ImportError):
from fast_depends.dependencies import Depends as FastDepends
def extract_marker_from_fast_depends(param: Any) -> Any:
if isinstance(param, FastDepends):
return param.dependency
return None
MARKER_EXTRACTORS.append(extract_marker_from_fast_depends)
try:
import werkzeug.local
except ImportError:
werkzeug = None
with suppress(ImportError):
from starlette.requests import Request as StarletteRequest
def is_starlette_request_cls(obj: Any) -> bool:
return isclass(obj) and _safe_is_subclass(obj, StarletteRequest)
INSPECT_EXCLUSION_FILTERS.append(is_starlette_request_cls)
from . import providers
with suppress(ImportError):
from werkzeug.local import LocalProxy as WerkzeugLocalProxy
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,
)
def is_werkzeug_local_proxy(obj: Any) -> bool:
return isinstance(obj, WerkzeugLocalProxy)
INSPECT_EXCLUSION_FILTERS.append(is_werkzeug_local_proxy)
from . import providers # noqa: E402
__all__ = (
"wire",
@ -87,9 +128,17 @@ __all__ = (
T = TypeVar("T")
F = TypeVar("F", bound=Callable[..., Any])
if TYPE_CHECKING:
from .containers import Container
else:
Container = Any
class DIWiringWarning(RuntimeWarning):
"""Base class for all warnings raised by the wiring module."""
class PatchedRegistry:
def __init__(self) -> None:
@ -99,7 +148,9 @@ class PatchedRegistry:
def register_callable(self, patched: "PatchedCallable") -> None:
self._callables[patched.patched] = patched
def get_callables_from_module(self, module: ModuleType) -> Iterator[Callable[..., Any]]:
def get_callables_from_module(
self, module: ModuleType
) -> Iterator[Callable[..., Any]]:
for patched_callable in self._callables.values():
if not patched_callable.is_in_module(module):
continue
@ -114,7 +165,9 @@ class PatchedRegistry:
def register_attribute(self, patched: "PatchedAttribute") -> None:
self._attributes.add(patched)
def get_attributes_from_module(self, module: ModuleType) -> Iterator["PatchedAttribute"]:
def get_attributes_from_module(
self, module: ModuleType
) -> Iterator["PatchedAttribute"]:
for attribute in self._attributes:
if not attribute.is_in_module(module):
continue
@ -220,12 +273,15 @@ class ProvidersMap:
) -> Optional[providers.Provider]:
if isinstance(provider, providers.Delegate):
return self._resolve_delegate(provider)
elif isinstance(provider, (
elif isinstance(
provider,
(
providers.ProvidedInstance,
providers.AttributeGetter,
providers.ItemGetter,
providers.MethodCaller,
)):
),
):
return self._resolve_provided_instance(provider)
elif isinstance(provider, providers.ConfigurationOption):
return self._resolve_config_option(provider)
@ -260,12 +316,15 @@ class ProvidersMap:
original: providers.Provider,
) -> Optional[providers.Provider]:
modifiers = []
while isinstance(original, (
while isinstance(
original,
(
providers.ProvidedInstance,
providers.AttributeGetter,
providers.ItemGetter,
providers.MethodCaller,
)):
),
):
modifiers.insert(0, original)
original = original.provides
@ -302,7 +361,7 @@ class ProvidersMap:
original: providers.ConfigurationOption,
as_: Any = None,
) -> Optional[providers.Provider]:
original_root = original.root
original_root = original._get_root()
new = self._resolve_provider(original_root)
if new is None:
return None
@ -349,8 +408,9 @@ class ProvidersMap:
original_provider = original_providers[provider_name]
providers_map[original_provider] = current_provider
if isinstance(current_provider, providers.Container) \
and isinstance(original_provider, providers.Container):
if isinstance(current_provider, providers.Container) and isinstance(
original_provider, providers.Container
):
subcontainer_map = cls._create_providers_map(
current_container=current_provider.container,
original_container=original_provider.container,
@ -360,35 +420,19 @@ class ProvidersMap:
return providers_map
class InspectFilter:
def is_excluded(self, instance: object) -> bool:
if self._is_werkzeug_local_proxy(instance):
def is_excluded_from_inspect(obj: Any) -> bool:
for is_excluded in INSPECT_EXCLUSION_FILTERS:
if is_excluded(obj):
return True
elif self._is_starlette_request_cls(instance):
return True
elif self._is_builtin(instance):
return True
else:
return False
def _is_werkzeug_local_proxy(self, instance: object) -> bool:
return werkzeug and isinstance(instance, werkzeug.local.LocalProxy)
def _is_starlette_request_cls(self, instance: object) -> bool:
return starlette \
and isinstance(instance, type) \
and _safe_is_subclass(instance, starlette.requests.Request)
def _is_builtin(self, instance: object) -> bool:
return inspect.isbuiltin(instance)
def wire( # noqa: C901
container: Container,
*,
modules: Optional[Iterable[ModuleType]] = None,
packages: Optional[Iterable[ModuleType]] = None,
keep_cache: bool = False,
) -> None:
"""Wire container providers with provided packages and modules."""
modules = [*modules] if modules else []
@ -400,8 +444,8 @@ def wire( # noqa: C901
providers_map = ProvidersMap(container)
for module in modules:
for member_name, member in inspect.getmembers(module):
if _inspect_filter.is_excluded(member):
for member_name, member in _get_members_and_annotated(module):
if is_excluded_from_inspect(member):
continue
if _is_marker(member):
@ -411,20 +455,27 @@ def wire( # noqa: C901
elif inspect.isclass(member):
cls = member
try:
cls_members = inspect.getmembers(cls)
cls_members = _get_members_and_annotated(cls)
except Exception: # noqa
# Hotfix, see: https://github.com/ets-labs/python-dependency-injector/issues/441
continue
else:
for cls_member_name, cls_member in cls_members:
if _is_marker(cls_member):
_patch_attribute(cls, cls_member_name, cls_member, providers_map)
_patch_attribute(
cls, cls_member_name, cls_member, providers_map
)
elif _is_method(cls_member):
_patch_method(cls, cls_member_name, cls_member, providers_map)
_patch_method(
cls, cls_member_name, cls_member, providers_map
)
for patched in _patched_registry.get_callables_from_module(module):
_bind_injections(patched, providers_map)
if not keep_cache:
clear_cache()
def unwire( # noqa: C901
*,
@ -443,7 +494,9 @@ def unwire( # noqa: C901
if inspect.isfunction(member):
_unpatch(module, name, member)
elif inspect.isclass(member):
for method_name, method in inspect.getmembers(member, inspect.isfunction):
for method_name, method in inspect.getmembers(
member, inspect.isfunction
):
_unpatch(member, method_name, method)
for patched in _patched_registry.get_callables_from_module(module):
@ -457,6 +510,11 @@ def unwire( # noqa: C901
def inject(fn: F) -> F:
"""Decorate callable with injecting decorator."""
reference_injections, reference_closing = _fetch_reference_injections(fn)
if not reference_injections:
warn("@inject is not required here", DIWiringWarning, stacklevel=2)
return fn
patched = _get_patched(fn, reference_injections, reference_closing)
return cast(F, patched)
@ -484,9 +542,11 @@ def _patch_method(
method: Callable[..., Any],
providers_map: ProvidersMap,
) -> None:
if hasattr(cls, "__dict__") \
and name in cls.__dict__ \
and isinstance(cls.__dict__[name], (classmethod, staticmethod)):
if (
hasattr(cls, "__dict__")
and name in cls.__dict__
and isinstance(cls.__dict__[name], (classmethod, staticmethod))
):
method = cls.__dict__[name]
fn = method.__func__
else:
@ -500,6 +560,10 @@ def _patch_method(
_bind_injections(fn, providers_map)
if fn is method:
# Hotfix, see: https://github.com/ets-labs/python-dependency-injector/issues/884
return
if isinstance(method, (classmethod, staticmethod)):
fn = type(method)(fn)
@ -511,9 +575,11 @@ def _unpatch(
name: str,
fn: Callable[..., Any],
) -> None:
if hasattr(module, "__dict__") \
and name in module.__dict__ \
and isinstance(module.__dict__[name], (classmethod, staticmethod)):
if (
hasattr(module, "__dict__")
and name in module.__dict__
and isinstance(module.__dict__[name], (classmethod, staticmethod))
):
method = module.__dict__[name]
fn = method.__func__
@ -548,16 +614,37 @@ def _unpatch_attribute(patched: PatchedAttribute) -> None:
setattr(patched.member, patched.name, patched.marker)
def _extract_marker(parameter: inspect.Parameter) -> Optional["_Marker"]:
if get_origin(parameter.annotation) is Annotated:
args = get_args(parameter.annotation)
if len(args) > 1:
marker = args[1]
else:
marker = None
else:
marker = parameter.default
for marker_extractor in MARKER_EXTRACTORS:
if _marker := marker_extractor(marker):
marker = _marker
break
if not isinstance(marker, _Marker):
return None
return marker
@cache
def _fetch_reference_injections( # noqa: C901
fn: Callable[..., Any],
) -> Tuple[Dict[str, Any], Dict[str, Any]]:
# Hotfix, see:
# - https://github.com/ets-labs/python-dependency-injector/issues/362
# - https://github.com/ets-labs/python-dependency-injector/issues/398
if GenericAlias and any((
fn is GenericAlias,
getattr(fn, "__func__", None) is GenericAlias
)):
if GenericAlias and any(
(fn is GenericAlias, getattr(fn, "__func__", None) is GenericAlias)
):
fn = fn.__init__
try:
@ -573,16 +660,9 @@ def _fetch_reference_injections( # noqa: C901
injections = {}
closing = {}
for parameter_name, parameter in signature.parameters.items():
if not isinstance(parameter.default, _Marker) \
and not _is_fastapi_depends(parameter.default):
continue
marker = _extract_marker(parameter)
marker = parameter.default
if _is_fastapi_depends(marker):
marker = marker.dependency
if not isinstance(marker, _Marker):
if marker is None:
continue
if isinstance(marker, Closing):
@ -593,22 +673,6 @@ def _fetch_reference_injections( # noqa: C901
return injections, closing
def _locate_dependent_closing_args(provider: providers.Provider) -> Dict[str, providers.Provider]:
if not hasattr(provider, "args"):
return {}
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:
patched_callable = _patched_registry.get_callable(fn)
if patched_callable is None:
@ -630,9 +694,9 @@ def _bind_injections(fn: Callable[..., Any], providers_map: ProvidersMap) -> Non
if injection in patched_callable.reference_closing:
patched_callable.add_closing(injection, provider)
deps = _locate_dependent_closing_args(provider)
for key, dep in deps.items():
patched_callable.add_closing(key, dep)
for resource in provider.traverse(types=[providers.Resource]):
patched_callable.add_closing(str(id(resource)), resource)
def _unbind_injections(fn: Callable[..., Any]) -> None:
@ -676,6 +740,8 @@ def _get_patched(
if inspect.iscoroutinefunction(fn):
patched = _get_async_patched(fn, patched_object)
elif inspect.isasyncgenfunction(fn):
patched = _get_async_gen_patched(fn, patched_object)
else:
patched = _get_sync_patched(fn, patched_object)
@ -685,18 +751,16 @@ def _get_patched(
return patched
def _is_fastapi_depends(param: Any) -> bool:
return fastapi and isinstance(param, fastapi.params.Depends)
def _is_patched(fn) -> bool:
return _patched_registry.has_callable(fn)
def _is_declarative_container(instance: Any) -> bool:
return (isinstance(instance, type)
return (
isinstance(instance, type)
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:
@ -712,8 +776,7 @@ class Modifier:
self,
provider: providers.ConfigurationOption,
providers_map: ProvidersMap,
) -> providers.Provider:
...
) -> providers.Provider: ...
class TypeModifier(Modifier):
@ -749,15 +812,15 @@ class RequiredModifier(Modifier):
def __init__(self) -> None:
self.type_modifier = None
def as_int(self) -> "RequiredModifier":
def as_int(self) -> Self:
self.type_modifier = TypeModifier(int)
return self
def as_float(self) -> "RequiredModifier":
def as_float(self) -> Self:
self.type_modifier = TypeModifier(float)
return self
def as_(self, type_: Type) -> "RequiredModifier":
def as_(self, type_: Type) -> Self:
self.type_modifier = TypeModifier(type_)
return self
@ -805,15 +868,15 @@ class ProvidedInstance(Modifier):
def __init__(self) -> None:
self.segments = []
def __getattr__(self, item):
def __getattr__(self, item: str) -> Self:
self.segments.append((self.TYPE_ATTRIBUTE, item))
return self
def __getitem__(self, item):
def __getitem__(self, item) -> Self:
self.segments.append((self.TYPE_ITEM, item))
return self
def call(self):
def call(self) -> Self:
self.segments.append((self.TYPE_CALL, None))
return self
@ -838,15 +901,30 @@ def provided() -> ProvidedInstance:
return ProvidedInstance()
class ClassGetItemMeta(GenericMeta):
def __getitem__(cls, item):
# Spike for Python 3.6
if isinstance(item, tuple):
return cls(*item)
return cls(item)
MarkerItem = Union[
str,
providers.Provider[Any],
Tuple[str, TypeModifier],
Type[Container],
"_Marker",
]
class _Marker(Generic[T], metaclass=ClassGetItemMeta):
if TYPE_CHECKING:
class _Marker(Protocol):
__IS_MARKER__: bool
def __call__(self) -> Self: ...
def __getattr__(self, item: str) -> Self: ...
def __getitem__(self, item: Any) -> Any: ...
Provide: _Marker
Provider: _Marker
Closing: _Marker
else:
class _Marker:
__IS_MARKER__ = True
@ -860,25 +938,19 @@ class _Marker(Generic[T], metaclass=ClassGetItemMeta):
self.provider = provider
self.modifier = modifier
def __class_getitem__(cls, item) -> T:
def __class_getitem__(cls, item: MarkerItem) -> Self:
if isinstance(item, tuple):
return cls(*item)
return cls(item)
def __call__(self) -> T:
def __call__(self) -> Self:
return self
class Provide(_Marker): ...
class Provide(_Marker):
...
class Provider(_Marker): ...
class Provider(_Marker):
...
class Closing(_Marker):
...
class Closing(_Marker): ...
class AutoLoader:
@ -928,8 +1000,7 @@ class AutoLoader:
super().exec_module(module)
loader.wire_module(module)
class ExtensionFileLoader(importlib.machinery.ExtensionFileLoader):
...
class ExtensionFileLoader(importlib.machinery.ExtensionFileLoader): ...
loader_details = [
(SourcelessFileLoader, importlib.machinery.BYTECODE_SUFFIXES),
@ -978,24 +1049,71 @@ def is_loader_installed() -> bool:
_patched_registry = PatchedRegistry()
_inspect_filter = InspectFilter()
_loader = AutoLoader()
# Optimizations
from ._cwiring import _get_sync_patched # noqa
from ._cwiring import _async_inject # noqa
from ._cwiring import DependencyResolver # noqa: E402
# Wiring uses the following Python wrapper because there is
# no possibility to compile a first-type citizen coroutine in Cython.
def _get_async_patched(fn: F, patched: PatchedCallable) -> F:
@functools.wraps(fn)
async def _patched(*args, **kwargs):
return await _async_inject(
fn,
args,
kwargs,
patched.injections,
patched.closing,
)
return _patched
async def _patched(*args: Any, **raw_kwargs: Any) -> Any:
resolver = DependencyResolver(raw_kwargs, patched.injections, patched.closing)
async with resolver as kwargs:
return await fn(*args, **kwargs)
return cast(F, _patched)
def _get_async_gen_patched(fn: F, patched: PatchedCallable) -> F:
@functools.wraps(fn)
async def _patched(*args: Any, **raw_kwargs: Any) -> AsyncIterator[Any]:
resolver = DependencyResolver(raw_kwargs, patched.injections, patched.closing)
async with resolver as kwargs:
async for obj in fn(*args, **kwargs):
yield obj
return cast(F, _patched)
def _get_sync_patched(fn: F, patched: PatchedCallable) -> F:
@functools.wraps(fn)
def _patched(*args: Any, **raw_kwargs: Any) -> Any:
resolver = DependencyResolver(raw_kwargs, patched.injections, patched.closing)
with resolver as kwargs:
return fn(*args, **kwargs)
return cast(F, _patched)
if sys.version_info >= (3, 10):
def _get_annotations(obj: Any) -> Dict[str, Any]:
return inspect.get_annotations(obj)
else:
def _get_annotations(obj: Any) -> Dict[str, Any]:
return getattr(obj, "__annotations__", {})
def _get_members_and_annotated(obj: Any) -> Iterable[Tuple[str, Any]]:
members = inspect.getmembers(obj)
annotations = _get_annotations(obj)
for annotation_name, annotation in annotations.items():
if get_origin(annotation) is Annotated:
args = get_args(annotation)
if len(args) > 1:
member = args[1]
members.append((annotation_name, member))
return members
def clear_cache() -> None:
"""Clear all caches used by :func:`wire`."""
_fetch_reference_injections.cache_clear()

View File

@ -1,10 +0,0 @@
[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

@ -1,10 +0,0 @@
[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

@ -1,11 +0,0 @@
[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:Please import \`.*?\` from the \`scipy(.*?)\` namespace(.*):DeprecationWarning
ignore:\`scipy(.*?)\` is deprecated(.*):DeprecationWarning
ignore:ssl\.PROTOCOL_TLS is deprecated:DeprecationWarning:botocore.*

View File

@ -1,12 +1,10 @@
from dependency_injector import providers
class Animal:
...
class Animal: ...
class Cat(Animal):
...
class Cat(Animal): ...
# Test 1: to check Aggregate provider
@ -20,13 +18,19 @@ val1: str = provider1("a")
provider1_set_non_string_keys: providers.Aggregate[str] = providers.Aggregate()
provider1_set_non_string_keys.set_providers({Cat: providers.Object("str")})
provider_set_non_string_1: providers.Provider[str] = provider1_set_non_string_keys.providers[Cat]
provider_set_non_string_1: providers.Provider[str] = (
provider1_set_non_string_keys.providers[Cat]
)
provider1_new_non_string_keys: providers.Aggregate[str] = providers.Aggregate(
{Cat: providers.Object("str")},
)
factory_new_non_string_1: providers.Provider[str] = provider1_new_non_string_keys.providers[Cat]
factory_new_non_string_1: providers.Provider[str] = (
provider1_new_non_string_keys.providers[Cat]
)
provider1_no_explicit_typing = providers.Aggregate(a=providers.Object("str"))
provider1_no_explicit_typing_factory: providers.Provider[str] = provider1_no_explicit_typing.providers["a"]
provider1_no_explicit_typing_factory: providers.Provider[str] = (
provider1_no_explicit_typing.providers["a"]
)
provider1_no_explicit_typing_object: str = provider1_no_explicit_typing("a")

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