Feature: 自定义默认依赖注入
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 7.7k
- Forks
- 662
- Avg merge
- 7d 15h
- Merged PRs (30d)
- 7
Description
希望能解决的问题
目前的 Depends 方法可以处理绝大多数的依赖注入例如:
def func(custom_arg: C): ...
其中 C 是一个 Annotated[_C, Depends(...)] 的别名。
但是这种处理方法有一个问题:Annotated 不支持运行时的含泛型别名,使用 TypeAlias 或 Python3.12+的 type 字段也无法做到这点,会造成依赖注入的失效(因为这两者本质都不是运行时,而是定义时就确定的)。
描述所需要的功能
考虑到创建一个含泛型的 Depends 方法的确比较繁琐,以下的一种注入方式是否可行:
对于实现了方法 _get_dependent 的自定义类,DependParam 或其他的构造器可以直接使用这个方法进行依赖信息的构造,而不需要再从默认参数等位置进行信息读取了,这样就能实现类似 Bot、Message 等的简洁的依赖注入了。
另外,目前 nonebot2 对于不同情景的 Param 注入管理比较混乱,是直接在多个地方定义 DEFAULT_PARAMS 等列表,其他插件的 Matcher 也需要通过重写 new 等手段进行修改(例如 Alconna 等)。如果能够创建统一的管理模块,也方便进行更深入的管理与自定义。
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reviewing the existing Depends and DependParam handling, the DEFAULT_PARAMS definitions, and Matcher.new overrides mentioned in the issue, then compare how Bot and Message injection is implemented. Define the scope of a unified parameter-management mechanism and verify that custom classes implementing _get_dependent can construct dependency information without relying on runtime generic aliases.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- backend-api-design
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100