Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged

Conversation

nightblure
Copy link
Contributor

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):
    ...
  1. overriding multiple providers with a context manager. Now you can use it like this:
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
Copy link
Member

@nightblure looks great, thanks!

pyproject.toml Outdated Show resolved Hide resolved
tests/providers/test_providers_overriding.py Show resolved Hide resolved
that_depends/container.py Outdated Show resolved Hide resolved
that_depends/providers/base.py Outdated Show resolved Hide resolved
@nightblure
Copy link
Contributor Author

nightblure commented Jul 15, 2024

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

image

@lesnik512
Copy link
Member

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
Copy link
Contributor Author

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

@nightblure
Copy link
Contributor Author

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
Copy link
Member

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

@nightblure
Copy link
Contributor Author

@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
Copy link
Member

@nightblure better in separate, I'll merge this

@lesnik512 lesnik512 merged commit 548b5e7 into modern-python:main Jul 15, 2024
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants