roo-oliv / injectable

Python Dependency Injection for Humans™
https://injectable.readthedocs.io
MIT License
112 stars 9 forks source link

First-class support for testing #106

Open roo-oliv opened 3 years ago

roo-oliv commented 3 years ago

Supplying fake/mocked/stubbed injectables should be really easy.

An example feature would be to introduce a helper fixture for pytest:

@pytest.fixture()
def given_injectable():
    originals: Dict[Union[type, str], Tuple[Set[Injectable], bool]] = {}

    def register(constructor: callable, klass=None, qualifier=None, propagate=False):
        if not klass and not qualifier:
            raise ValueError("params 'klass' and 'qualifier' cannot be both None")
        if propagate and not klass:
            raise ValueError("param 'klass' must be set when 'propagate' is True")
        for dependency in klass, qualifier:
            if not dependency or dependency in originals:
                continue
            originals[dependency] = clear_injectables(dependency), propagate
        register_injectables(
            {Injectable(constructor)}, klass, qualifier, propagate=propagate
        )

    yield register

    for dependency, (injectables, propagate) in originals.items():
        clear_injectables(dependency)
        if isinstance(dependency, str):
            register_injectables(injectables, qualifier=dependency, propagate=propagate)
        else:
            register_injectables(injectables, klass=dependency, propagate=propagate)
javiermrzfever commented 6 months ago

Hi! Any updates on this? I've been searching for a nice python DI package and this is by bar the most friendly and easy to use. I see a lot of intention on adding new features and maintaining it, but I don't see any new release since 2021. What is the state of the library?

Thanks!

roo-oliv commented 5 months ago

Hi @javiermrzfever, thanks for reaching out! :heart:

I've been working to make injectable compatible with newer Python versions since 2023 but I've found it to be quite more work than I expected. So this is why it has been a while since the latest release. Good news is that I plan to release a new version by May'24 :tada: