modern-python / that-depends

DI-framework, inspired by python-dependency-injector, but without wiring. Python 3.12 is supported
https://that-depends.readthedocs.io/
MIT License
133 stars 10 forks source link

Overriding with context manager (one and multiple providers) #53

Closed nightblure closed 2 months ago

nightblure commented 2 months ago

I implement two little features for ease of use during tests:

  1. overriding one provider with a context manager. Now you can use it like this:
    
    # old way
    some_provider.override(mock)
    some_provider.reset_override()

new way

with some_provider.override_context(mock): ...


2. overriding multiple providers with a context manager. Now you can use it like this:
```python3
mock_1 = ...
mock2 = ...

# old way
container.provider_1.override(mock_1)
container.provider_2.override(mock_2)
# do something
container.reset_override()

# new way
providers_for_overriding = {
    "provider_1": mock_1,
    "provider_2": mock2,
    # more providers...
}

with container.override_providers(providers_for_overriding):
    # do something
lesnik512 commented 2 months ago

@nightblure looks great, thanks!

nightblure commented 2 months ago

we lose coverage on this line after I removed omit. is it acceptable to add # pragma: no cover here?

image
lesnik512 commented 2 months ago

we lose coverage on this line after I removed omit. is it acceptable to add # pragma: no cover here?

image

yes, it's ok

nightblure commented 2 months ago

thanks for the review! corrected according to your comments, please take a look

nightblure commented 2 months ago

hmm, there is still a question about updating the documentation. I can try to add the relevant sections myself if you allow, or you will do this

lesnik512 commented 2 months ago

@nightblure would be great if you'll add docs as well, but it's up to you. Anyway, thank you for your contribution!

nightblure commented 2 months ago

@nightblure would be great if you'll add docs as well, but it's up to you. Anyway, thank you for your contribution!

ok I'll try to do it. within the current MR or separate?

lesnik512 commented 2 months ago

@nightblure better in separate, I'll merge this