python 类型提示应如何要求值具有给定属性?

How should a python type hint require that a value has a given attribute?

假设我有一个像这样的简单函数:

def foo(a: Any):
    return a.bar + a.baz

我想将类型提示从 Any 更改为需要(嗯,建议,因为它是一种类型 hinta 提供 barbaz 属性。应该改成什么?

您需要使用这些属性创建一个 class,以便您传递的对象中包含这些属性。即:

class Myclass():
    def __init__(self, bar, baz):
        self.bar = bar
        self.baz = baz


def foo(a: Myclass):
    return a.bar + a.baz

类型提示只能引用一个class,所以创建一个抽象class

import abc

class MyType(abc.ABC):

    @abc.abstractproperty
    def foo(self):
        pass

    @abc.abstractproperty
    def bar(self):
        pass

并声明f(a: MyType)

这正是 Protocols 的用途。简而言之,协议让您可以使用 structural 而不是 nominal 子类型。对于名义子类型,如果 A 显式继承或扩展 B,则类型 A 是 B 的子类型。对于结构子类型,如果类型 A 具有与 B 相同的方法和属性 "signatures"(有一些限制),则类型 A 是 B 的子类型.

例如:

# If you're using Python 3.8+
from typing import Protocol

# If you need to support older versions of Python,
# pip-install the 'typing_extensions' module and do:
from typing_extensions import Protocol

class SupportsBarBaz(Protocol):
    bar: int
    baz: int

class MyUnrelatedClass1:
    def __init__(self, bar: int, baz: int) -> None:
        self.bar = bar
        self.baz = baz

class MyUnrelatedClass2:
    def __init__(self, bar: int, baz: int, blah: str) -> None:
        self.bar = bar
        self.baz = baz
        self.blah = blah

class MyUnrelatedClass3:
    def __init__(self, bar: str, baz: str, blah: str) -> None:
        self.bar = bar
        self.baz = baz
        self.blah = blah

def foo(a: SupportsBarBaz) -> int:
    return a.bar + a.baz

# These both type-check, even though there's no explicit relationship
# between 'SupportsBarBaz' and these two classes
foo(MyUnrelatedClass1(1, 2))
foo(MyUnrelatedClass2(1, 2, "abc"))

# But this doesn't type-check, since 'bar' and 'baz' are both strs here
foo(MyUnrelatedClass3("a", "b", "c"))

您可以在 mypy docs 中找到有关使用协议的更多信息。该页面中的信息都符合 PEP,因此那里的信息应该都适用于其他类型检查器,假设他们已经完成了对协议的支持。

您还可以在 typeshed, the repository of type hints for the Python standard library 中找到使用协议的稍微复杂的示例。

不过,我认为只有当您真正打算在代码中使用静态分析时,这一切才有意义。如果没有,您可以做一些更简单的事情,只需为 Any 定义一个自定义类型别名,记录该别名 "supposed" 的含义,并使用该别名而不是完整的协议。就静态 analysis/autocompletion tools/etc 而言,该别名几乎完全没用,但人类通常可以毫无问题地阅读评论。