fix: recognise PEP 695 type aliases as Depends() parameters - #261
Closed
cezar-hapiko wants to merge 1 commit into
Closed
cezar-hapiko wants to merge 1 commit into
cezar-hapiko wants to merge 1 commit into
Conversation
`_has_depends` checks `get_origin(param.annotation) is Annotated`, but for a PEP 695 alias such as `type SettingsDep = Annotated[Settings, Depends(get_settings)]` the annotation is a `TypeAliasType` and `get_origin` returns None. Since 1.5.0 `_build_dependency_only_callable` therefore strips every such parameter before FastAPI sees it, and the wrapped callable fails with "missing 1 required positional argument". FastAPI's own resolver unwraps these aliases, so the same providers work in routes and only `@injectable` breaks. Unwrap `TypeAliasType` (both `typing` and `typing_extensions`, which are distinct classes on 3.12/3.13) before inspecting the annotation.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #261 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 11 11
Lines 595 603 +8
Branches 72 73 +1
=========================================
+ Hits 595 603 +8
🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Since 1.5.0,
_build_dependency_only_callablepre-filters parameters with_has_depends, which checksget_origin(param.annotation) is Annotated. For a PEP 695 alias:param.annotationis aTypeAliasType,get_originreturnsNone, the parameter is stripped before FastAPI sees it, and the call fails with:FastAPI's own resolver unwraps these aliases, so the same providers work in routes; only
@injectablebreaks. 1.4.6 passed the function straight toget_dependantand worked. This bit us on upgrade from 1.4.6 to 1.6.1: every@injectableworker factory in a codebase that usestype XDep = Annotated[...]aliases throughout failed at startup.Fix
Unwrap
TypeAliasTypebefore inspecting the annotation. Bothtyping.TypeAliasTypeandtyping_extensions.TypeAliasTypeare handled — they are distinct classes on 3.12 and 3.13, so checking only one misses the other.Test
test_injectable_resolves_pep695_type_alias_dependencyis parametrized over both alias classes (thetypingcase skips below 3.12). It fails onmainwith theTypeErrorabove and passes with this change. Full suite: 231 passed on 3.10, 232 passed on 3.12;mypy srcandruffclean.Also verified downstream: a ~3000-test application suite that failed at worker startup on 1.6.1 passes fully with this patch applied.