Skip to content

Commit

Permalink
fix for provider overriding in litestar (#120)
Browse files Browse the repository at this point in the history
* add failing test

* forbid provider deepcopy

* remove not needed comments
  • Loading branch information
lesnik512 authored Nov 19, 2024
1 parent 1703b65 commit d9dfff9
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
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:
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

0 comments on commit d9dfff9

Please sign in to comment.