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

fix for provider overriding in litestar #120

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions tests/integrations/test_litestar_di.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,34 @@ def test_litestar_di() -> None:
"local_dependency": 1,
"router_dependency": "",
}


def test_litestar_di_override_fail_on_provider_override() -> None:
mock = 12345364758999
with TestClient(app=app) as client, DIContainer.int_fn.override_context(mock):
response = client.get("/router/controller/handler")

assert response.status_code == HTTP_200_OK, response.text
assert response.json() == {
"app_dependency": False,
"controller_dependency": ["some"],
"local_dependency": mock,
"router_dependency": "",
}


def test_litestar_di_override_fail_on_override_providers() -> None:
mock = 12345364758999
overrides = {
"int_fn": mock,
}
with TestClient(app=app) as client, DIContainer.override_providers(overrides):
response = client.get("/router/controller/handler")

assert response.status_code == HTTP_200_OK, response.text
assert response.json() == {
"app_dependency": False,
"controller_dependency": ["some"],
"local_dependency": mock,
"router_dependency": "",
}
5 changes: 5 additions & 0 deletions that_depends/providers/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
from contextlib import contextmanager
from operator import attrgetter

import typing_extensions

from that_depends.entities.resource_context import ResourceContext


Expand All @@ -22,6 +24,9 @@ def __init__(self) -> None:
super().__init__()
self._override: typing.Any = None

def __deepcopy__(self, *_: object, **__: object) -> typing_extensions.Self:
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think very good decision will be to left here comment with reference to issue and reference to litestar behaviour. Because at some point it may be not so easy to understand why this decision is been made

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good idea, thank you

return self

def __getattr__(self, attr_name: str) -> typing.Any: # noqa: ANN401
if attr_name.startswith("_"):
msg = f"'{type(self)}' object has no attribute '{attr_name}'"
Expand Down