You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何为接收*参数的字典zip函数正确重载类型注解?

解决方法

问题出在重载签名与实现的参数形式不兼容:你定义的重载使用了命名参数(a/b/c),类型检查器会认为调用时可以通过关键字传递这些参数,但实现的*dicts仅接受位置参数,导致签名不一致。

修正方案如下:

  1. 给重载的参数加上/标记,声明它们是仅限位置参数,避免类型检查器要求实现支持关键字参数;
  2. 给实现函数的*dicts添加合适的类型注解;
  3. 补充到5个参数的重载(按你的需求);
  4. 修复原代码中重复导入overload的问题,以及可选的字典键交集问题(避免键缺失引发KeyError)。

修正后的完整代码:

from typing import TypeVar, overload, Tuple

T = TypeVar("T")
V1 = TypeVar("V1")
V2 = TypeVar("V2")
V3 = TypeVar("V3")
V4 = TypeVar("V4")
V5 = TypeVar("V5")

@overload
def zip_dicts(a: dict[T, V1], /) -> dict[T, tuple[V1]]: ...

@overload
def zip_dicts(a: dict[T, V1], b: dict[T, V2], /) -> dict[T, tuple[V1, V2]]: ...

@overload
def zip_dicts(a: dict[T, V1], b: dict[T, V2], c: dict[T, V3], /) -> dict[T, tuple[V1, V2, V3]]: ...

@overload
def zip_dicts(a: dict[T, V1], b: dict[T, V2], c: dict[T, V3], d: dict[T, V4], /) -> dict[T, tuple[V1, V2, V3, V4]]: ...

@overload
def zip_dicts(a: dict[T, V1], b: dict[T, V2], c: dict[T, V3], d: dict[T, V4], e: dict[T, V5], /) -> dict[T, tuple[V1, V2, V3, V4, V5]]: ...

def zip_dicts(*dicts: dict[T, object]) -> dict[T, tuple[object, ...]]:
    if not dicts:
        raise ValueError("至少需要传入一个字典")
    # 取所有字典的共同键,避免KeyError
    common_keys = set.intersection(*(set(d.keys()) for d in dicts))
    return {k: tuple(d[k] for d in dicts) for k in common_keys}

# 测试代码
a: dict[str, int] = {"a": 1}
b: dict[str, str] = {"a": "hello"}
c: dict[str, float] = {"a": 3.14}
d: dict[str, tuple[int, str, float]] = zip_dicts(a, b, c)
print(d)  # 输出: {'a': (1, 'hello', 3.14)}

关键说明

  • /是Python 3.8+的语法,用于标记参数为仅限位置,这样类型检查器就不会要求实现支持a=...这类关键字传参,与*dicts的参数形式完全兼容;
  • 实现函数的*dicts: dict[T, object]注解确保类型检查器能识别传入的参数是同键类型的字典;
  • 添加了空参数的判断,避免dicts[0]引发索引错误;
  • 改用字典键的交集生成结果,避免原代码中因字典键不匹配导致的KeyError。

内容的提问来源于stack exchange,提问作者KamilCuk

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.15 22:47:31