Reference
pybooster
pybooster.required
A sentinel object used to indicate that a dependency is required.
pybooster.solved
Resolve the dependency graph defined by the given providers during the context.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
providers |
Provider[[], Any] | Sequence[Provider[[], Any]]
|
The providers that define the dependency graph to be resolved given as positional arguments or as sequences of providers. |
()
|
Source code in src/pybooster/core/solution.py
pybooster.core
pybooster.core.injector
pybooster.core.injector.required
A sentinel object used to indicate that a dependency is required.
pybooster.core.injector.CurrentValues
pybooster.core.injector.asynccontextmanager
Inject dependencies into the given async context manager function.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
func |
AsyncIteratorCallable[P, R]
|
The function to inject dependencies into. |
required |
requires |
HintMap | HintSeq | None
|
The parameters and dependencies to inject. Otherwise infered from signature. |
None
|
shared |
bool
|
Whether injected values should be shared for the duration of the context. |
False
|
Source code in src/pybooster/core/injector.py
pybooster.core.injector.asyncfunction
Inject dependencies into the given coroutine.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
func |
Callable[P, Coroutine[Any, Any, R]]
|
The function to inject dependencies into. |
required |
requires |
HintMap | HintSeq | None
|
The parameters and dependencies to inject. Otherwise infered from signature. |
None
|
shared |
bool
|
Whether injected values should be shared for the duration of any calls. |
False
|
Source code in src/pybooster/core/injector.py
pybooster.core.injector.asynciterator
Inject dependencies into the given async iterator.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
func |
AsyncIteratorCallable[P, R]
|
The function to inject dependencies into. |
required |
requires |
HintMap | HintSeq | None
|
The parameters and dependencies to inject. Otherwise infered from signature. |
None
|
shared |
bool
|
Whether injected values should be shared for the duration of any calls. |
False
|
Source code in src/pybooster/core/injector.py
pybooster.core.injector.contextmanager
Inject dependencies into the given context manager function.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
func |
IteratorCallable[P, R]
|
The function to inject dependencies into. |
required |
requires |
HintMap | HintSeq | None
|
The parameters and dependencies to inject. Otherwise infered from signature. |
None
|
shared |
bool
|
Whether injected values should be shared for the duration of the context. |
False
|
Source code in src/pybooster/core/injector.py
pybooster.core.injector.current_values
pybooster.core.injector.function
Inject dependencies into the given function.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
func |
Callable[P, R]
|
The function to inject dependencies into. |
required |
requires |
HintMap | HintSeq | None
|
The parameters and dependencies to inject. Otherwise infered from signature. |
None
|
shared |
bool
|
Whether injected values should be shared for the duration of any calls. |
False
|
Source code in src/pybooster/core/injector.py
pybooster.core.injector.iterator
Inject dependencies into the given iterator.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
func |
IteratorCallable[P, R]
|
The function to inject dependencies into. |
required |
requires |
HintMap | HintSeq | None
|
The parameters and dependencies to inject. Otherwise infered from signature. |
None
|
shared |
bool
|
Whether injected values should be shared for the duration of any calls. |
False
|
Source code in src/pybooster/core/injector.py
pybooster.core.injector.shared
Share the values for a set of dependencies for the duration of a context.
Source code in src/pybooster/core/injector.py
pybooster.core.provider
pybooster.core.provider.Provider
A provider for a dependency.
pybooster.core.provider.AsyncProvider
Bases: Generic[P, R]
, _BaseProvider[R]
A provider for a dependency.
Source code in src/pybooster/core/provider.py
pybooster.core.provider.AsyncProvider.bind
pybooster.core.provider.SyncProvider
Bases: Generic[P, R]
, _BaseProvider[R]
A provider for a dependency.
Source code in src/pybooster/core/provider.py
pybooster.core.provider.SyncProvider.bind
pybooster.core.provider.asyncfunction
Create a provider from the given coroutine.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
func |
Callable[P, Awaitable[R]]
|
The function to create a provider from. |
required |
requires |
HintMap | HintSeq | None
|
The dependencies of the function (infered if not provided). |
None
|
provides |
Hint | InferHint | None
|
The type that the function provides (infered if not provided). |
None
|
Source code in src/pybooster/core/provider.py
pybooster.core.provider.asynciterator
Create a provider from the given async iterator function.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
func |
AsyncIteratorCallable[P, R]
|
The function to create a provider from. |
required |
requires |
HintMap | HintSeq | None
|
The dependencies of the function (infered if not provided). |
None
|
provides |
Hint | InferHint | None
|
The type that the function provides (infered if not provided). |
None
|
Source code in src/pybooster/core/provider.py
pybooster.core.provider.function
Create a provider from the given function.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
func |
Callable[P, R]
|
The function to create a provider from. |
required |
requires |
HintMap | HintSeq | None
|
The dependencies of the function (infered if not provided). |
None
|
provides |
Hint | InferHint | None
|
The type that the function provides (infered if not provided). |
None
|
Source code in src/pybooster/core/provider.py
pybooster.core.provider.iterator
Create a provider from the given iterator function.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
func |
IteratorCallable[P, R]
|
The function to create a provider from. |
required |
requires |
HintMap | HintSeq | None
|
The dependencies of the function (infered if not provided). |
None
|
provides |
Hint | InferHint | None
|
The type that the function provides (infered if not provided). |
None
|
Source code in src/pybooster/core/provider.py
pybooster.core.solution
pybooster.core.solution.solved
Resolve the dependency graph defined by the given providers during the context.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
providers |
Provider[[], Any] | Sequence[Provider[[], Any]]
|
The providers that define the dependency graph to be resolved given as positional arguments or as sequences of providers. |
()
|
Source code in src/pybooster/core/solution.py
pybooster.core.state
pybooster.core.state.copy_state
Copy PyBooster's current state and return a callback that will set it in another context.
Example
If you need to run a function in a different context, you can use this function to copy the state established there and set it in the new context. This might happen if you create a thread (which has its own context) to run a function that creates some PyBooster state which you'd then like to use in the main thread.
from concurrent.futures import Future
from threading import Thread
from typing import NewType
from pybooster import injector
from pybooster.core.state import copy_state
Greeting = NewType("Greeting", str)
def from_thread(future):
with injector.shared((Greeting, "Hello")):
set_state = copy_state()
future.set_result(set_state)
set_state_future = Future()
thread = Thread(target=from_thread, args=(set_state_future,))
thread.start()
set_state = set_state_future.result()
reset_state = set_state()
assert injector.current_values().get(Greeting) == "Hello"
reset_state()
assert injector.current_values().get(Greeting) is None
Source code in src/pybooster/core/state.py
pybooster.extra
pybooster.extra.asgi
pybooster.extra.sqlalchemy
pybooster.extra.sqlalchemy.async_session_provider
async_session_provider = asynciterator(
_async_session_provider,
requires={"bind": AsyncEngine},
provides=_infer_async_session_type,
)
Provide an async SQLAlchemy session.
pybooster.extra.sqlalchemy.session_provider
session_provider = iterator(
_session_provider,
requires={"bind": Engine},
provides=_infer_session_type,
)
Provide a SQLAlchemy session.
pybooster.extra.sqlalchemy.AsyncSessionMaker
Bases: Protocol[P, A_co]
A protocol for creating an async SQLAlchemy session.
pybooster.extra.sqlalchemy.AsyncSessionMaker.__call__
pybooster.extra.sqlalchemy.SessionMaker
Bases: Protocol[P, S_co]
A protocol for creating a SQLAlchemy session.
pybooster.extra.sqlalchemy.async_engine_provider
Provide an async SQLAlchemy engine.
Source code in src/pybooster/extra/sqlalchemy.py
pybooster.extra.sqlalchemy.engine_provider
Provide a SQLAlchemy engine.
Source code in src/pybooster/extra/sqlalchemy.py
pybooster.injector
pybooster.injector.required
A sentinel object used to indicate that a dependency is required.
pybooster.injector.CurrentValues
pybooster.injector.asynccontextmanager
Inject dependencies into the given async context manager function.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
func |
AsyncIteratorCallable[P, R]
|
The function to inject dependencies into. |
required |
requires |
HintMap | HintSeq | None
|
The parameters and dependencies to inject. Otherwise infered from signature. |
None
|
shared |
bool
|
Whether injected values should be shared for the duration of the context. |
False
|
Source code in src/pybooster/core/injector.py
pybooster.injector.asyncfunction
Inject dependencies into the given coroutine.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
func |
Callable[P, Coroutine[Any, Any, R]]
|
The function to inject dependencies into. |
required |
requires |
HintMap | HintSeq | None
|
The parameters and dependencies to inject. Otherwise infered from signature. |
None
|
shared |
bool
|
Whether injected values should be shared for the duration of any calls. |
False
|
Source code in src/pybooster/core/injector.py
pybooster.injector.asynciterator
Inject dependencies into the given async iterator.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
func |
AsyncIteratorCallable[P, R]
|
The function to inject dependencies into. |
required |
requires |
HintMap | HintSeq | None
|
The parameters and dependencies to inject. Otherwise infered from signature. |
None
|
shared |
bool
|
Whether injected values should be shared for the duration of any calls. |
False
|
Source code in src/pybooster/core/injector.py
pybooster.injector.contextmanager
Inject dependencies into the given context manager function.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
func |
IteratorCallable[P, R]
|
The function to inject dependencies into. |
required |
requires |
HintMap | HintSeq | None
|
The parameters and dependencies to inject. Otherwise infered from signature. |
None
|
shared |
bool
|
Whether injected values should be shared for the duration of the context. |
False
|
Source code in src/pybooster/core/injector.py
pybooster.injector.current_values
pybooster.injector.function
Inject dependencies into the given function.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
func |
Callable[P, R]
|
The function to inject dependencies into. |
required |
requires |
HintMap | HintSeq | None
|
The parameters and dependencies to inject. Otherwise infered from signature. |
None
|
shared |
bool
|
Whether injected values should be shared for the duration of any calls. |
False
|
Source code in src/pybooster/core/injector.py
pybooster.injector.iterator
Inject dependencies into the given iterator.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
func |
IteratorCallable[P, R]
|
The function to inject dependencies into. |
required |
requires |
HintMap | HintSeq | None
|
The parameters and dependencies to inject. Otherwise infered from signature. |
None
|
shared |
bool
|
Whether injected values should be shared for the duration of any calls. |
False
|
Source code in src/pybooster/core/injector.py
pybooster.injector.shared
Share the values for a set of dependencies for the duration of a context.
Source code in src/pybooster/core/injector.py
pybooster.provider
pybooster.provider.Provider
A provider for a dependency.
pybooster.provider.AsyncProvider
Bases: Generic[P, R]
, _BaseProvider[R]
A provider for a dependency.
Source code in src/pybooster/core/provider.py
pybooster.provider.AsyncProvider.bind
pybooster.provider.SyncProvider
Bases: Generic[P, R]
, _BaseProvider[R]
A provider for a dependency.
Source code in src/pybooster/core/provider.py
pybooster.provider.SyncProvider.bind
pybooster.provider.asyncfunction
Create a provider from the given coroutine.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
func |
Callable[P, Awaitable[R]]
|
The function to create a provider from. |
required |
requires |
HintMap | HintSeq | None
|
The dependencies of the function (infered if not provided). |
None
|
provides |
Hint | InferHint | None
|
The type that the function provides (infered if not provided). |
None
|
Source code in src/pybooster/core/provider.py
pybooster.provider.asynciterator
Create a provider from the given async iterator function.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
func |
AsyncIteratorCallable[P, R]
|
The function to create a provider from. |
required |
requires |
HintMap | HintSeq | None
|
The dependencies of the function (infered if not provided). |
None
|
provides |
Hint | InferHint | None
|
The type that the function provides (infered if not provided). |
None
|
Source code in src/pybooster/core/provider.py
pybooster.provider.function
Create a provider from the given function.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
func |
Callable[P, R]
|
The function to create a provider from. |
required |
requires |
HintMap | HintSeq | None
|
The dependencies of the function (infered if not provided). |
None
|
provides |
Hint | InferHint | None
|
The type that the function provides (infered if not provided). |
None
|
Source code in src/pybooster/core/provider.py
pybooster.provider.iterator
Create a provider from the given iterator function.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
func |
IteratorCallable[P, R]
|
The function to create a provider from. |
required |
requires |
HintMap | HintSeq | None
|
The dependencies of the function (infered if not provided). |
None
|
provides |
Hint | InferHint | None
|
The type that the function provides (infered if not provided). |
None
|
Source code in src/pybooster/core/provider.py
pybooster.types
pybooster.types.AnyContextManagerCallable
AnyContextManagerCallable = Callable[
P,
AbstractContextManager[R]
| AbstractAsyncContextManager[R],
]
A callable that returns any kind of context manager.
pybooster.types.AsyncContextManagerCallable
A callable that returns an async context manager.
pybooster.types.AsyncIteratorCallable
A callable that returns an async iterator.
pybooster.types.ContextManagerCallable
A callable that returns a context manager.
pybooster.types.HintDict
HintDict = dict[str, Hint]
A dictionary of parameter or attribute names to their type.
pybooster.types.HintMap
HintMap = Mapping[str, Hint]
A mapping of parameter or attribute names to their type.
pybooster.types.IteratorCallable
A callable that returns an iterator.
pybooster.types.InjectionError
Bases: RuntimeError
An error raised when an injection fails.
pybooster.types.SolutionError
Bases: RuntimeError
An error raised when a solution fails.