Skip to content

Commit f961ff5

Browse files
rmk135Roman Mogylatov
and
Roman Mogylatov
authored
Schemas (#429)
* Add single container prototype * Add multiple containers prototype * Add integration tests * Implement from_*() methods and add tests * Prototype inline injections * Add integration test for inline providers * Refactor integration tests * Add integration test for reordered schema * Remove unused imports from tests * Refactor schema module * Update tests to match latest schemas * Add mypy_boto3_s3 to the test requirements * Add boto3 to the test requirements * Add set_provides for Callable, Factory, and Singleton providers * Fix warnings in tests * Add typing stubs for Callable, Factory, and Singleton .set_provides() attributes * Fix singleton children to have optional provides * Implement provider to provider resolving * Fix pypy3 tests * Implement boto3 session use case and add tests * Implement lazy initialization and improve copying for Callable, Factory, Singleton, and Coroutine providers * Fix Python 2 tests * Add region name for boto3 integration example * Remove f-strings from set_provides() * Fix schema flake8 errors * Implement lazy initialization and improve copying for Delegate provider * Implement lazy initialization and improve copying for Object provider * Speed up wiring tests * Implement lazy initialization and improve copying for FactoryAggregate provider * Implement lazy initialization and improve copying for Selector provider * Implement lazy initialization and improve copying for Dependency provider * Implement lazy initialization and improve copying for Resource provider * Implement lazy initialization and improve copying for Configuration provider * Implement lazy initialization and improve copying for ProvidedInstance provider * Implement lazy initialization and improve copying for AttributeGetter provider * Implement lazy initialization and improve copying for ItemGetter provider * Implement lazy initialization and improve copying for MethodCaller provder * Update changelog * Fix typing in wiring module * Fix wiring module loader uninstallation issue * Fix provided instance providers error handing in asynchronous mode Co-authored-by: Roman Mogylatov <rmk@Romans-MacBook-Pro.local>
1 parent 8cad8c6 commit f961ff5

38 files changed

+24081
-17859
lines changed

docs/main/changelog.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@ that were made in every particular version.
77
From version 0.7.6 *Dependency Injector* framework strictly
88
follows `Semantic versioning`_
99

10+
Development version
11+
-------------------
12+
- Implement providers' lazy initialization.
13+
- Improve providers' copying.
14+
- Improve typing in wiring module.
15+
- Fix wiring module loader uninstallation issue.
16+
- Fix provided instance providers error handing in asynchronous mode.
17+
1018
4.30.0
1119
------
1220
- Remove restriction to wire a dynamic container.

requirements-dev.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,7 @@ fastapi
1212
pydantic
1313
numpy
1414
scipy
15+
boto3
16+
mypy_boto3_s3
1517

1618
-r requirements-ext.txt

src/dependency_injector/containers.c

Lines changed: 3742 additions & 2364 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/dependency_injector/containers.pyi

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from pathlib import Path
12
from typing import (
23
Generic,
34
Type,
@@ -34,6 +35,7 @@ class Container:
3435
def __init__(self) -> None: ...
3536
def __deepcopy__(self, memo: Optional[Dict[str, Any]]) -> Provider: ...
3637
def __setattr__(self, name: str, value: Union[Provider, Any]) -> None: ...
38+
def __getattr__(self, name: str) -> Provider: ...
3739
def __delattr__(self, name: str) -> None: ...
3840
def set_providers(self, **providers: Provider): ...
3941
def set_provider(self, name: str, provider: Provider) -> None: ...
@@ -48,6 +50,9 @@ class Container:
4850
def apply_container_providers_overridings(self) -> None: ...
4951
def reset_singletons(self) -> SingletonResetContext[C_Base]: ...
5052
def check_dependencies(self) -> None: ...
53+
def from_schema(self, schema: Dict[Any, Any]) -> None: ...
54+
def from_yaml_schema(self, filepath: Union[Path, str], loader: Optional[Any]=None) -> None: ...
55+
def from_json_schema(self, filepath: Union[Path, str]) -> None: ...
5156
@overload
5257
def resolve_provider_name(self, provider: Provider) -> str: ...
5358
@classmethod

src/dependency_injector/containers.pyx

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
11
"""Containers module."""
22

3+
import json
34
import sys
45

56
try:
67
import asyncio
78
except ImportError:
89
asyncio = None
910

11+
try:
12+
import yaml
13+
except ImportError:
14+
yaml = None
15+
1016
import six
1117

1218
from . import providers, errors
@@ -330,6 +336,39 @@ class DynamicContainer(Container):
330336
f'{", ".join(undefined_names)}',
331337
)
332338

339+
def from_schema(self, schema):
340+
"""Build container providers from schema."""
341+
from .schema import build_schema
342+
for name, provider in build_schema(schema).items():
343+
self.set_provider(name, provider)
344+
345+
def from_yaml_schema(self, filepath, loader=None):
346+
"""Build container providers from YAML schema.
347+
348+
You can specify type of loader as a second argument. By default, method
349+
uses ``SafeLoader``.
350+
"""
351+
if yaml is None:
352+
raise errors.Error(
353+
'Unable to load yaml schema - PyYAML is not installed. '
354+
'Install PyYAML or install Dependency Injector with yaml extras: '
355+
'"pip install dependency-injector[yaml]"'
356+
)
357+
358+
if loader is None:
359+
loader = yaml.SafeLoader
360+
361+
with open(filepath) as file:
362+
schema = yaml.load(file, loader)
363+
364+
self.from_schema(schema)
365+
366+
def from_json_schema(self, filepath):
367+
"""Build container providers from JSON schema."""
368+
with open(filepath) as file:
369+
schema = json.load(file)
370+
self.from_schema(schema)
371+
333372
def resolve_provider_name(self, provider):
334373
"""Try to resolve provider name."""
335374
for provider_name, container_provider in self.providers.items():

0 commit comments

Comments
 (0)