代码之家  ›  专栏  ›  技术社区  ›  rmorshea

MyPy-将Kwargs传递给复杂函数

  •  0
  • rmorshea  · 技术社区  · 7 年前

    请考虑以下尝试向函数添加类型提示 parent child

    def parent(*, a: Type1, b: Type2):
        ...
    
    def child(*, c: Type3, d: Type4, **kwargs):
        parent(**kwargs)
        ...
    

    MyPy抱怨说 kwargs 具有类型 Dict[str, Any] 但这就是论点 a b 要求 Type1 Type2 分别。

    我知道解决这个错误的办法是重写 按以下方式:

    def child(*, a: Type1, b: Type2, c: Type3, d: Type4, **kwargs):
        parent(a=a, b=b)
        ...
    

    但是如果 起源 许多的 时间长了,还是有功能 grandchild 它有自己的参数列表并且必须调用 小孩 **kwargs ?

    0 回复  |  直到 7 年前
    推荐文章