Skip to content

fix(event_handler): resolve Depends() and Request params with a None default on Python 3.10 - #8649

Merged
leandrodamascena merged 1 commit into
developfrom
fix/depends-optional-py310
Oct 20, 2026
Merged

leandrodamascena merged 1 commit into
developfrom
fix/depends-optional-py310

Conversation

@leandrodamascena

Copy link
Copy Markdown
Contributor

Issue number: closes #8648

Summary

Changes

On Python 3.10, typing.get_type_hints wraps the annotation of a parameter whose default is None in Optional[...]. That hid the Annotated metadata, so Depends() was silently skipped and the parameter received None, and a request: Request = None parameter was not injected either. Python 3.11+ stopped wrapping, so only 3.10 was affected.

  • New _unwrap_optional helper in depends.py returns the single non-None member of a Union/Optional, otherwise the annotation itself.
  • Applied in _get_depends_from_annotation, in the Request injection inside solve_dependencies, and in _find_request_param_name.
  • Three tests: Depends() with a None default, explicit Optional[Annotated[...]] and Annotated[...] | None, and request: Request = None. All three fail on 3.10 without the change and pass with it. tests/functional/event_handler and tests/unit/event_handler pass on 3.10 and 3.14.

User experience

Before, on Python 3.10:

@app.get("/my/path")
def handler(region: Annotated[str, Depends(get_region)] = None):
    return {"region": region}   # {"region": null}

After: {"region": "eu-west-1"}, the same as on 3.11+. No change for 3.11+ and no public API change.

Found while running the pre-release smoke test of develop in real Lambda functions on python3.10.


By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

Disclaimer: We value your time and bandwidth. As such, any pull requests created on non-triaged issues might not be successful.

…default on Python 3.10

On Python 3.10, typing.get_type_hints wraps the annotation of a parameter whose
default is None in Optional[...], hiding the Annotated metadata. Depends() was
silently skipped and the parameter received None; a Request parameter with a
None default was not injected either. Python 3.11+ stopped wrapping, so only
3.10 was affected. Unwrap Optional/Union before inspecting the annotation.
@leandrodamascena
leandrodamascena requested a review from a team as a code owner October 20, 2026 10:39
@powertools-for-aws-oss-automation powertools-for-aws-oss-automation Bot added the size/M Denotes a PR that changes 30-99 lines, ignoring generated files. label Oct 20, 2026
@sonarqubecloud

Copy link
Copy Markdown

@codecov

codecov Bot commented Oct 20, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 96.93%. Comparing base (4fe0d6d) to head (cb2b574).

Additional details and impacted files
@@           Coverage Diff            @@
##           develop    #8649   +/-   ##
========================================
  Coverage    96.93%   96.93%           
========================================
  Files          318      318           
  Lines        16036    16045    +9     
  Branches      1423     1425    +2     
========================================
+ Hits         15545    15554    +9     
  Misses         349      349           
  Partials       142      142           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@leandrodamascena
leandrodamascena merged commit 7a5b1ac into develop Oct 20, 2026
18 checks passed
@leandrodamascena
leandrodamascena deleted the fix/depends-optional-py310 branch October 20, 2026 10:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

event_handlers size/M Denotes a PR that changes 30-99 lines, ignoring generated files. tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: Depends() and Request parameters with a None default are not resolved on Python 3.10

1 participant