From a1aa3dd348a6487d1a3f6b87bcbdb975da99151a Mon Sep 17 00:00:00 2001 From: Artur Shiriev Date: Mon, 1 Jul 2024 09:12:09 +0300 Subject: [PATCH] fix fastapi singleton usage with getattr --- tests/integrations/test_fastapi_di.py | 5 +++++ that_depends/providers/singleton.py | 3 +++ 2 files changed, 8 insertions(+) diff --git a/tests/integrations/test_fastapi_di.py b/tests/integrations/test_fastapi_di.py index 675cc35..cefe117 100644 --- a/tests/integrations/test_fastapi_di.py +++ b/tests/integrations/test_fastapi_di.py @@ -23,9 +23,14 @@ async def read_root( container.FreeFactory, fastapi.Depends(container.DIContainer.resolver(container.FreeFactory)), ], + singleton: typing.Annotated[ + container.SingletonFactory, + fastapi.Depends(container.DIContainer.singleton), + ], ) -> datetime.datetime: assert dependency.sync_resource == free_dependency.dependent_factory.sync_resource assert dependency.async_resource == free_dependency.dependent_factory.async_resource + assert singleton.dep1 return dependency.async_resource diff --git a/that_depends/providers/singleton.py b/that_depends/providers/singleton.py index 5d4b44a..0be50af 100644 --- a/that_depends/providers/singleton.py +++ b/that_depends/providers/singleton.py @@ -21,6 +21,9 @@ def __init__(self, factory: type[T] | typing.Callable[P, T], *args: P.args, **kw self._resolving_lock = asyncio.Lock() def __getattr__(self, attr_name: str) -> typing.Any: # noqa: ANN401 + if attr_name.startswith("_"): + msg = f"'{type(self)}' object has no attribute '{attr_name}'" + raise AttributeError(msg) return AttrGetter(provider=self, attr_name=attr_name) async def async_resolve(self) -> T: