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 fastapi singleton usage with getattr #48

Merged
merged 1 commit into from
Jul 1, 2024
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
5 changes: 5 additions & 0 deletions tests/integrations/test_fastapi_di.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
3 changes: 3 additions & 0 deletions that_depends/providers/singleton.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Loading