-
Notifications
You must be signed in to change notification settings - Fork 14
Closed
Labels
Description
I found a new test case. This is more of a Litestar problem, but still
Non-primitive objects cannot be overridden with any other type until we mark it with the Any annotation. Below is the code that will help reproduce this
class and container:
class SomeService:
def do_smth(self) -> str:
return "something"
class DIContainer(BaseContainer):
bool_fn = providers.Factory(bool_fn, value=False)
str_fn = providers.Factory(str_fn)
list_fn = providers.Factory(list_fn)
int_fn = providers.Factory(int_fn)
some_service = providers.Factory(SomeService)controller handler:
@get(path="mock_overriding", dependencies={"some_service": Provide(DIContainer.some_service)})
# does not works with some_service: SomeService type, but works with some_service: SomeService | Any
async def mock_overriding_endpoint_handler(self, some_service: SomeService) -> dict[str, typing.Any]:
return {"object": some_service.do_smth()}test:
def test_litestar_endpoint_with_mock_overriding() -> None:
some_service_mock = Mock(do_smth=lambda: "mock func")
with DIContainer.some_service.override_context(some_service_mock), TestClient(app=app) as client:
response = client.get("/router/controller/mock_overriding")
assert response.json()["object"] == "mock func"error:
TypeError: Unsupported type: <class 'unittest.mock.Mock'>
msgspec.ValidationError: Unsupported type: <class 'unittest.mock.Mock'> - at `$.some_service`