diff --git a/nanodi/nanodi.py b/nanodi/nanodi.py index 56ea61a..7589903 100644 --- a/nanodi/nanodi.py +++ b/nanodi/nanodi.py @@ -23,6 +23,8 @@ def Depends(dependency: Dependency, /, use_cache: bool = True) -> Any: # noqa: N802 + if dependency in _resources and not use_cache: + raise ValueError("use_cache=False is not supported for resources") return _Depends(dependency, use_cache) diff --git a/tests/test_sync_resource.py b/tests/test_sync_resource.py index 88e31ab..1101fd6 100644 --- a/tests/test_sync_resource.py +++ b/tests/test_sync_resource.py @@ -57,3 +57,13 @@ def my_service(redis: Redis = Depends(redis_dependency)): shutdown_resources() assert redis.closed is True + + +def test_resources_cant_be_used_if_specified_without_cache(redis_dependency): + with pytest.raises( + ValueError, match="use_cache=False is not supported for resources" + ): + + @inject + def my_service(redis1: Redis = Depends(redis_dependency, use_cache=False)): + return redis1