Skip to content

Commit 5f579bc

Browse files
feat: add Dependency injection feature
1 parent 15f7ea1 commit 5f579bc

1 file changed

Lines changed: 60 additions & 0 deletions

File tree

tests/functional/event_handler/required_dependencies/test_depends.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,3 +354,63 @@ def handler(config: Annotated[Config, Depends(Config)]):
354354
result = app(API_GW_V2_EVENT, {})
355355
assert result["statusCode"] == 200
356356
assert json.loads(result["body"]) == {"region": "us-east-1"}
357+
358+
359+
def test_depends_with_unresolvable_annotations_is_ignored():
360+
"""A handler whose annotations cannot be resolved by get_type_hints is treated as having no deps."""
361+
app = APIGatewayHttpResolver()
362+
363+
# Build a function with broken annotations that get_type_hints cannot resolve.
364+
# The param has a default so the handler can still be called without it.
365+
def make_handler():
366+
def handler(x: "CompletelyBogusType" = None): # noqa: F821
367+
return {"ok": True}
368+
369+
return handler
370+
371+
app.post("/my/path")(make_handler())
372+
373+
result = app(API_GW_V2_EVENT, {})
374+
assert result["statusCode"] == 200
375+
assert json.loads(result["body"]) == {"ok": True}
376+
377+
378+
def test_depends_without_request_does_not_inject():
379+
"""A dependency that does NOT declare Request still works when request is available."""
380+
app = APIGatewayHttpResolver()
381+
382+
def get_static() -> str:
383+
return "no-request-needed"
384+
385+
@app.post("/my/path")
386+
def handler(val: Annotated[str, Depends(get_static)]):
387+
return {"val": val}
388+
389+
result = app(API_GW_V2_EVENT, {})
390+
assert result["statusCode"] == 200
391+
assert json.loads(result["body"]) == {"val": "no-request-needed"}
392+
393+
394+
def test_depends_with_broken_type_hints_on_dependency():
395+
"""A dependency callable with broken annotations still resolves (get_type_hints fails gracefully)."""
396+
app = APIGatewayHttpResolver()
397+
398+
# Create a callable whose annotations reference a nonexistent type
399+
# so get_type_hints() will raise inside solve_dependencies
400+
broken_dep = type(
401+
"BrokenDep",
402+
(),
403+
{
404+
"__call__": lambda self: "it-works",
405+
"__annotations__": {"x": "NonExistentType"},
406+
"__module__": __name__,
407+
},
408+
)()
409+
410+
@app.post("/my/path")
411+
def handler(val: Annotated[str, Depends(broken_dep)]):
412+
return {"val": val}
413+
414+
result = app(API_GW_V2_EVENT, {})
415+
assert result["statusCode"] == 200
416+
assert json.loads(result["body"]) == {"val": "it-works"}

0 commit comments

Comments
 (0)