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

Feature: 优先使用 Annotated 的最后一个子依赖 #2360

Merged
merged 4 commits into from Sep 13, 2023

Conversation

ProgramRipper
Copy link
Member

当前的 Annotated 子依赖支持优先使用 metadata 中的第一个子依赖。

此 PR 将行为更改成优先使用 metadata 中的最后一个子依赖。
借助嵌套 Annotated 会被展平,metadata 顺序由最内层向外的特性(assert Annotated[Annotated[int, ValueRange(3, 10)], ctype("char")] == Annotated[int, ValueRange(3, 10), ctype("char")]),可以实现 Annotated 子依赖的复用:

假设上游提供一个 User 子依赖

User = Annotated[_User, _get_user]

# Usage:
@handler.handle()
async def _(user: User):
    ...

在下游,可以重写 User 的依赖注入逻辑,而复用 User 的类型标注:

CustomUser = Annotated[User, _custom_get_user()]

# Usage:
@handler.handle()
async def _(user: CustomUser):
    ...

参见:

@codecov
Copy link

codecov bot commented Sep 12, 2023

Codecov Report

Merging #2360 (8b2e4cf) into master (005968a) will increase coverage by 0.03%.
Report is 2 commits behind head on master.
The diff coverage is 100.00%.

@@            Coverage Diff             @@
##           master    #2360      +/-   ##
==========================================
+ Coverage   90.77%   90.81%   +0.03%     
==========================================
  Files          46       46              
  Lines        3424     3428       +4     
==========================================
+ Hits         3108     3113       +5     
+ Misses        316      315       -1     
Flag Coverage Δ
unittests 90.81% <100.00%> (+0.03%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files Changed Coverage Δ
nonebot/internal/params.py 98.92% <100.00%> (ø)

... and 3 files with indirect coverage changes

@github-actions
Copy link
Contributor

github-actions bot commented Sep 12, 2023

🚀 Deployed on https://deploy-preview-2360--nonebot2.netlify.app

@github-actions github-actions bot temporarily deployed to pull request September 12, 2023 17:52 Inactive
mnixry
mnixry previously approved these changes Sep 12, 2023
Copy link
Member

@mnixry mnixry left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@yanyongyu yanyongyu added the enhancement New feature or request label Sep 13, 2023
tests/test_param.py Outdated Show resolved Hide resolved
yanyongyu
yanyongyu previously approved these changes Sep 13, 2023
@github-actions github-actions bot temporarily deployed to pull request September 13, 2023 04:09 Inactive
@ProgramRipper ProgramRipper marked this pull request as draft September 13, 2023 05:02
@ProgramRipper ProgramRipper marked this pull request as ready for review September 13, 2023 14:45
@github-actions github-actions bot temporarily deployed to pull request September 13, 2023 14:49 Inactive
@github-actions github-actions bot temporarily deployed to pull request September 13, 2023 15:21 Inactive
@yanyongyu yanyongyu merged commit 7aaa66c into master Sep 13, 2023
24 checks passed
@yanyongyu yanyongyu deleted the feature/last-annotated-dependency branch September 13, 2023 16:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Development

Successfully merging this pull request may close these issues.

None yet

3 participants