python / python/typing

TypeVarTuple with Callable and default arguments

未关闭
#1,231 8 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

topic: feature
主要语言
Python
星标
1.8k
派生
302
平均合并
23 小时
30 天内合并 PR
8

描述

Previous discussion: https://github.com/microsoft/pyright/issues/3775

I've been playing around with TypeVarTuples recently, in particular together with Callable and args.
https://peps.python.org/pep-0646/#type-variable-tuples-with-callable

One issue became obvious early on which isn't defined in the PEP itself.

How should default arguments be handled?

A common pattern, especially in async code, is to pass a callable and it's args to a function

from typing import Callable
from typing_extensions import TypeVarTuple

Ts = TypeVarTuple("Ts")

def call_later(cb: Callable[[*Ts], None], *args: *Ts) -> None: ...

That works well if all arguments are required

def func1(x: int, y: int, z: int) -> None: ...
call_later(func1, 0, 0, 0)  # ok

However, what should happen if y and z have default arguments?

def func2(x: int, y: int = 0, z: int = 0) -> None: ...
call_later(func2, 0)

Instinctively, I would think that it should work, too. As @erictraut did point out though, there are at least a few cases where the behavior would need to be further specified / or explicitly forbidden.

贡献指南

这个仓库没有索引到贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

首先阅读 issue 3775 中之前的讨论,以及 PEP 646 中关于使用 Callable 的 TypeVarTuples 部分。比较必需参数和默认参数的示例,以及讨论中提出的边界情况,并为默认参数确定一种已规定或明确禁止的行为。

由索引模型根据 Issue 内容生成。

评估

技术栈
python
领域
tooling
Issue 类型
功能
难度
5/5
预计耗时
一周以上
活跃度
停滞
描述清晰度
需要澄清
新手友好度
25/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。