Skip to content

Commit

Permalink
Fix interference with depends when not being used in a wireup context;
Browse files Browse the repository at this point in the history
…Fixes #14
  • Loading branch information
maldoinc committed Dec 28, 2023
1 parent ac1fa52 commit 381f94e
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 6 deletions.
13 changes: 9 additions & 4 deletions test/integration/test_fastapi_integration.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import unittest

from fastapi import FastAPI
from fastapi import FastAPI, Depends
from fastapi.testclient import TestClient
from typing_extensions import Annotated

Expand All @@ -18,14 +18,19 @@ def setUp(self):
def test_injects_service(self):
self.container.register(RandomService)

def get_lucky_number() -> int:
return 42

@self.app.get("/")
@self.container.autowire
async def target(random_service: Annotated[RandomService, Wire()]):
return {"number": random_service.get_random()}
async def target(
random_service: Annotated[RandomService, Wire()], lucky_number: Annotated[int, Depends(get_lucky_number)]
):
return {"number": random_service.get_random(), "lucky_number": lucky_number}

response = self.client.get("/")
self.assertEqual(response.status_code, 200)
self.assertEqual(response.json(), {"number": 4})
self.assertEqual(response.json(), {"number": 4, "lucky_number": 42})

def test_raises_on_unknown_service(self):
@self.app.get("/")
Expand Down
2 changes: 1 addition & 1 deletion wireup/annotation/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def wire(
try:
# Allow fastapi users to do .get() without any params
# It is meant to be used as a default value in where Depends() is expected
return importlib.import_module("fastapi").Depends(lambda: None)
return importlib.import_module("fastapi").Depends(EmptyContainerInjectionRequest)
except ModuleNotFoundError:
return EmptyContainerInjectionRequest()

Expand Down
5 changes: 4 additions & 1 deletion wireup/ioc/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ def map_to_injectable_type(metadata: Any) -> InjectableType | None:
if isinstance(metadata, InjectableType):
return metadata

if str(metadata.__class__) == "<class 'fastapi.params.Depends'>":
if (
str(metadata.__class__) == "<class 'fastapi.params.Depends'>"
and metadata.dependency == EmptyContainerInjectionRequest
):
return EmptyContainerInjectionRequest()

return None
Expand Down

0 comments on commit 381f94e

Please sign in to comment.