Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Infinite loop with Closing ConfigurationOption #869

Copy link
Copy link
Open
@AndreyFrolov44

Description

@AndreyFrolov44
Issue body actions

After updating to 4.46.0, there is an endless iteration through the list when using Configuration.

For example:

from dependency_injector.containers import DeclarativeContainer
from dependency_injector.providers import Configuration, Resource, Factory
from dependency_injector.wiring import Provide, Closing


def test_resource(string: str):
    print("init resource")
    yield f"resource {string}"
    print("shutdown resource")


class Service:
    def __init__(self, resource: str):
        self.resource = resource

    def execute(self):
        print(self.resource)


class DIContainer(DeclarativeContainer):
    config = Configuration()
    resource = Resource(test_resource, config.foo)
    service = Factory(Service, resource=resource)


container = DIContainer()
container.config.from_dict({"foo": "test"})


def foo(
    service: Service = Closing[Provide["service"]],
):
    print("foo")
    service.execute()


if __name__ == "__main__":
    container.wire(modules=[__name__])

    foo()

At the same time, nothing is output to the console and no error occurs.

I suggest making the following fixes, but I'm not sure they won't break anything.

def _locate_dependent_closing_args(
    provider: providers.Provider, closing_deps: Dict[str, providers.Provider]
) -> Dict[str, providers.Provider]:
    for arg in [
        *getattr(provider, "args", []),
        *getattr(provider, "kwargs", {}).values(),
    ]:
        if not isinstance(arg, providers.Provider):
            continue
        if isinstance(arg, providers.ConfigurationOption):
            continue
        if isinstance(arg, providers.Resource):
            closing_deps[str(id(arg))] = arg

        _locate_dependent_closing_args(arg, closing_deps)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      Morty Proxy This is a proxified and sanitized view of the page, visit original site.