Skip to content

Feature unrequest: Remove dynamic container #151

@xelandernt

Description

@xelandernt

Don't really understand the point of dynamic container, it is very easy to break and I don't see a practical use case other than for a completely sync application:

1. Using dynamic container inside a function (async example):

import asyncio
import random

from that_depends import BaseContainer, providers, Provide, inject


class MyContainer(BaseContainer):

    p = providers.Factory(lambda: 10)


async def change_dynamic_container() -> int:
    await asyncio.sleep(random.randint(1, 2))
    MyContainer.p = providers.Factory(lambda: 0)
    return 0


async def some_foo():
    await asyncio.sleep(random.randint(1, 2))
    return await MyContainer.p.async_resolve()



async def app():

    results = await asyncio.gather(
        some_foo(),
        some_foo(),
        some_foo(),
        some_foo(),
        change_dynamic_container(),
        some_foo(),
        some_foo(),
        some_foo(),
        some_foo(),
    )
    print(sum(results))

if __name__ == '__main__':
    for _ in range(100):
        MyContainer.p = providers.Factory(lambda: 10)
        asyncio.run(app())

Outputs:

50, 60, 40, 60, 50, 70 ...

2. So then only useful at module level but then

#container.py
class Container(BaseContainer):
    p = providers.Factory(lambda: 10)

#another_module.py
from container import Container
Container.p = providers.Factory(lambda: 0)

Okay, now every other module, depending on when you import it (before or after another_module) you will get a different dependency provider.

Proposition

Yes I am aware the examples are quite targeted, but I just don't see a reason to actively support this feature (it's explicitly mentioned in the documentation) since anyone using it will likely have issues.

  1. Remove this feature?

  2. Am I missing some key use case?

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions