Option to type default default arguments in Callable types - `WithDefault`
未关闭
还没有人认领这个 Issue。
topic: feature
- 主要语言
- Python
- 星标
- 1.8k
- 派生
- 302
- 平均合并
- 23 小时
- 30 天内合并 PR
- 8
描述
At the moment it's only possible to type callables with default arguments by using a Callback protocol . This adds a lot of additional code for just a few default arguments. Moreover it requires that users have at least a basic understanding of Protocols which is one of the more advanced typing concepts. Lastly, a Callback protocol can't be used to type a generic ParamSpec argument.
Proposal
Add a new WithDefault special type which can be used to annotate arguments in callable types.
Examples
from typing import Callable, TypeAlias, WithDefault
def func(a: str, b: int = 0) -> None: ...
def other(a: str, b: int) -> None: ...
def g(f: Callable[[str, WithDefault[int]], None]) -> None:
f("Hello") # ok
f("World", 2) # ok
g(func) # ok
g(other) # error
For generic ParamSpec types
class Job(Generic[_P]):
def __init__(self, target: Callable[_P, None]) -> None:
self.target = target
def g(job: Job[str, WithDefault[int]]) -> None:
job.target("Hello") # ok
job.target("Hello", 2) # ok
贡献指南
这个仓库没有索引到贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
先从提案中的 Callable 和泛型 ParamSpec 示例开始,然后审查当前如何通过回调协议表示默认参数。定义 WithDefault 的类型行为,包括接受省略的参数和拒绝必需参数,并在认为该功能完成之前验证这两个示例。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- python
- 领域
- developer-experience
- Issue 类型
- 功能
- 难度
- 5/5
- 预计耗时
- 一周以上
- 活跃度
- 停滞
- 描述清晰度
- 基本清楚
- 新手友好度
- 35/100