-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #35 from modern-python/24-feature-add-object-provider
add object provider
- Loading branch information
Showing
5 changed files
with
74 additions
and
0 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
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,15 @@ | ||
# Object | ||
|
||
Object provider returns an object “as is”. | ||
|
||
```python | ||
from modern_di import BaseGraph, Container, Scope, providers | ||
|
||
|
||
class Dependencies(BaseGraph): | ||
object_provider = providers.Object(Scope.APP, 1) | ||
|
||
|
||
with Container(scope=Scope.APP) as container: | ||
assert Dependencies.object_provider.sync_resolve(container) == 1 | ||
``` |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import enum | ||
import typing | ||
|
||
from modern_di import Container | ||
from modern_di.providers.abstract import AbstractOverrideProvider | ||
|
||
|
||
T_co = typing.TypeVar("T_co", covariant=True) | ||
P = typing.ParamSpec("P") | ||
|
||
|
||
class Object(AbstractOverrideProvider[T_co]): | ||
__slots__ = [*AbstractOverrideProvider.BASE_SLOTS, "_obj"] | ||
|
||
def __init__(self, scope: enum.IntEnum, obj: T_co) -> None: | ||
super().__init__(scope) | ||
self._obj: typing.Final = obj | ||
|
||
async def async_resolve(self, container: Container) -> T_co: | ||
return self.sync_resolve(container) | ||
|
||
def sync_resolve(self, container: Container) -> T_co: | ||
container = container.find_container(self.scope) | ||
if (override := container.fetch_override(self.provider_id)) is not None: | ||
return typing.cast(T_co, override) | ||
|
||
return self._obj |
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,29 @@ | ||
from modern_di import Container, Scope, providers | ||
|
||
|
||
instance = ["some item"] | ||
|
||
|
||
object_provider = providers.Object(Scope.APP, instance) | ||
|
||
|
||
async def test_object_provider() -> None: | ||
async with Container(scope=Scope.APP) as app_container: | ||
instance1 = await object_provider.async_resolve(app_container) | ||
instance2 = object_provider.sync_resolve(app_container) | ||
|
||
assert instance1 is instance2 is instance | ||
|
||
|
||
async def test_object_provider_overridden() -> None: | ||
async with Container(scope=Scope.APP) as app_container: | ||
instance1 = await object_provider.async_resolve(app_container) | ||
|
||
object_provider.override(["override"], container=app_container) | ||
|
||
instance2 = await object_provider.async_resolve(app_container) | ||
instance3 = object_provider.sync_resolve(app_container) | ||
|
||
assert instance1 is instance | ||
assert instance2 is not instance | ||
assert instance2 is instance3 |