-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improved documentation for singleton provider and minor fixes (#92)
* Fixed AttrGetter bug with fastapi. * Changed AttrGetter to raise an exception. * Rework Attrgetter exception. * Added __init__ call for parent classes and improved some docstrings. * Improve singleton documentation. * Fixed typos. * Reformatted code. * Added pre-commit. * Changed pre-commit to use local config. --------- Co-authored-by: Alexander <e1634240@student.tuwien.ac.at>
- Loading branch information
1 parent
18e16fb
commit 4704255
Showing
10 changed files
with
104 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
repos: | ||
- repo: local | ||
hooks: | ||
- id: lint | ||
name: lint | ||
entry: just | ||
args: [lint] | ||
language: system | ||
types: [python] | ||
pass_filenames: false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,77 @@ | ||
# Singleton | ||
- Initialized only once, but without teardown logic. | ||
- Class or simple function is allowed. | ||
# Singleton Provider | ||
|
||
Singleton providers resolve the dependency only once and cache the resolved instance for future injections. | ||
|
||
## How it works | ||
|
||
```python | ||
import dataclasses | ||
|
||
import random | ||
from that_depends import providers | ||
|
||
def some_function(): | ||
"""Generate number between 0.0 and 1.0""" | ||
return random.random() | ||
|
||
# create a Singleton provider | ||
prov = providers.Singleton(some_function) | ||
|
||
# provider with call `some_func` and cache the return value | ||
prov.sync_resolve() # 0.3 | ||
# provider with return the cached value | ||
prov.sync_resolve() # 0.3 | ||
``` | ||
|
||
## Example with `pydantic-settings` | ||
|
||
Lets say we are storing our application configuration using [pydantic-settings](https://docs.pydantic.dev/latest/concepts/pydantic_settings/): | ||
|
||
```python | ||
from pydantic_settings import BaseSettings | ||
from that_depends import BaseContainer, providers | ||
|
||
class DatabaseConfig(BaseModel): | ||
address: str = "127.0.0.1" | ||
port: int = 5432 | ||
db_name: str = "postgres" | ||
|
||
@dataclasses.dataclass(kw_only=True, slots=True) | ||
class SingletonFactory: | ||
dep1: bool | ||
class Settings(BaseSettings): | ||
auth_key: str = "my_auth_key" | ||
db: DatabaseConfig = DatabaseConfig() | ||
``` | ||
|
||
Because we do not want to resolve the configuration each time it is used in our application, we provide it using the `Singleton` provider. | ||
|
||
class DIContainer(BaseContainer): | ||
singleton = providers.Singleton(SingletonFactory, dep1=True) | ||
```python | ||
async def get_db_connection(address: str, port:int, db_name: str) -> Connection: | ||
... | ||
|
||
class MyContainer(BaseContainer): | ||
config = providers.Singleton(Settings) | ||
# provide connection arguments and create a connection provider | ||
db_connection = providers.AsyncFactory( | ||
get_db_connection, config.db.address, config.db.port, config.db_name: | ||
) | ||
``` | ||
|
||
Now we can inject our database connection where it required using `@inject`: | ||
|
||
```python | ||
from that_depends import inject, Provide | ||
|
||
@inject | ||
async def with_db_connection(conn: Connection = Provide[MyContainer.db_connection]): | ||
... | ||
``` | ||
|
||
Of course we can also resolve the whole configuration without accessing attributes by running: | ||
|
||
```python | ||
# sync resolution | ||
config: Settings = MyContainer.config.sync_resolve() | ||
# async resolution | ||
config: Settings = await MyContainer.config.async_resolve() | ||
# inject the configuration into a function | ||
async def with_config(config: Settings = Provide[MyContainer.config]): | ||
assert config.auth_key == "my_auth_key" | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,6 +35,7 @@ dev-dependencies = [ | |
"ruff", | ||
"mypy==1.10.1", | ||
"typing-extensions", | ||
"pre-commit" | ||
] | ||
|
||
[build-system] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters