-
-
Save StefanoFrazzetto/d15340dacd261cdd0a9391a2d4e7b03e to your computer and use it in GitHub Desktop.
dependency-injector-4.10.0_bug-config-provider
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
import sys | |
from dependency_injector import providers, containers | |
class MyService(object): | |
def __init__(self, config: dict): | |
self.key = config.pop('key') | |
def trigger(self): | |
print('> Service: success') | |
class MyDevice(object): | |
def __init__(self, service: MyService): | |
# doesn't raise an error because it's an instance of | |
# dependency_injector.providers.Singleton | |
self.service = service | |
def do_something(self): | |
# raises "AttributeError: 'NoneType' object has no attribute 'get'" | |
print('> Device: triggering service') | |
self.service().trigger() | |
class ServiceContainer(containers.DeclarativeContainer): | |
config = providers.Configuration(strict=True) | |
myservice = providers.Singleton(MyService, config=config.myservice.required()) | |
class Container(containers.DeclarativeContainer): | |
config = providers.Configuration() | |
services = providers.Container(ServiceContainer, config=config.services) | |
mydevice = providers.Factory(MyDevice) | |
def print_version(): | |
import dependency_injector | |
print(f'dependency-injector v{dependency_injector.__version__}') | |
def execute(config: dict): | |
container = Container() | |
container.config.from_dict(config) | |
container.init_resources() | |
container.wire(modules=[sys.modules[__name__]]) | |
mydevice = container.mydevice(service=container.services.myservice) | |
mydevice.do_something() | |
if __name__ == '__main__': | |
print_version() | |
print("\n### WORKING") | |
working = {'services': {'myservice': {'key': 'foobar'}}} | |
execute(working) | |
print("\n### BUG") | |
broken = {'key': 'foobar'} | |
execute(broken) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment