Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Added documentation section explaining how to use Callable provider i… #708

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
Loading
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions 9 docs/providers/callable.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,15 @@ Callable provider
:language: python
:lines: 3-


If you would like to inject :py:class:`Callable` instance to a service using :py:class:`Provide` and the wiring
mechanism, then you should use the ``.provider`` field. This way :py:class:`Callable` instance will not be called
when being provided, and the service can deliver their own additional arguments.

.. literalinclude:: ../../examples/providers/callable_reusable.py
:language: python
:lines: 3-

``Callable`` provider handles an injection of the dependencies the same way like a
:ref:`factory-provider`.

Expand Down
38 changes: 38 additions & 0 deletions 38 examples/providers/callable_reusable.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
"""`Callable` provider example with configurable parameters"""

import passlib.hash

from dependency_injector import containers, providers
from dependency_injector.wiring import Provide


class Service:
def __init__(self, hasher):
self.hasher = hasher

def hash(self, value):
return self.hasher(value)


class Container(containers.DeclarativeContainer):

password_hasher = providers.Callable(
passlib.hash.sha256_crypt.hash,
salt_size=16,
rounds=10000,
)
password_verifier = providers.Callable(passlib.hash.sha256_crypt.verify)

service = providers.Factory(
Service,
hasher=password_hasher.provider
)


if __name__ == "__main__":
service: Service = Provide["service"]
container = Container()
container.wire(modules=[__name__])

hashed_password = service.hash("super secret")
assert container.password_verifier("super secret", hashed_password)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.