重用数据类类型提示

Reusing Dataclass Type Hints

我正在尝试在我的函数签名中重用来自数据类的类型提示 - 也就是说,无需再次键入签名。

解决这个问题的最佳方法是什么?

from dataclasses import dataclass
from typing import Set, Tuple, Type

@dataclass
class MyDataClass:
    force: Set[Tuple[str, float, bool]]

# I've had to write the same type annotation in the dataclass and the
# function signature - yuck
def do_something(force: Set[Tuple[str, float, bool]]):
    print(force)

# I want to do something like this, where I reference the type annotation from
# the dataclass. But, doing it this way, pycharm thinks `force` is type `Any`
def do_something_2(force: Type["MyDataClass.force"]):
    print(force)

What would be the best way of going about this?

PEP 484 为这种情况给出了一个明确的选择

Type aliases

Type aliases are defined by simple variable assignments: (...) Type aliases may be as complex as type hints in annotations -- anything that is acceptable as a type hint is acceptable in a type alias:

应用于您的示例,这相当于(Mypy 确认这是正确的)

from dataclasses import dataclass

Your_Type = set[tuple[str, float, bool]]


@dataclass
class MyDataClass:
    force: Your_Type


def do_something(force: Your_Type):
    print(force)

以上是使用 Python 3.9 以后编写的 Generic Alias Type. The syntax is more concise and modern since typing.Set and typing.Tuple 已被弃用。



现在,根据 Python Data Model 完全理解这一点比看起来要复杂得多:

3.1. Objects, values and types

Every object has an identity, a type and a value.

你第一次尝试使用 Type 会得到一个惊人的结果

>>> type(MyDataClass.force)

AttributeError: type object 'MyDataClass' has no attribute 'force'

这是因为内置函数 type returns 是一个类型(它本身就是一个对象)但是 MyDataClass 是“一个 Class”(一个声明)并且“Class 属性”force 在 Class 上,而不是在 type() 查找它的 class 的类型对象上。仔细注意数据模型的区别:

  • Classes

    These objects normally act as factories for new instances of themselves

  • Class Instances

    Instances of arbitrary classes

如果您改为检查实例的类型,您将得到以下结果

>>> init_values: set = {(True, "the_str", 1.2)}

>>> a_var = MyDataClass(init_values)

>>> type(a_var)
<class '__main__.MyDataClass'>

>>> type(a_var.force)
<class 'set'>

现在让我们通过将 type() 应用于前面提到的 __anotations__ on the Class declaration object (here we see the Generic Alias type 来恢复 force 上的类型对象(不是类型提示)。 (这里我们确实检查了 class 属性 force 上的类型对象)。

>>> type(MyDataClass.__annotations__['force'])
<class 'typing._GenericAlias'>

或者我们可以检查 Class 实例上的注释,并恢复我们习惯看到的类型提示。

>>> init_values: set = {(True, "the_str", 1.2)}
>>> a_var = MyDataClass(init_values)
>>> a_var.__annotations__

{'force': set[tuple[str, float, bool]]}

I've had to write the same type annotation in the dataclass and the function signature -

对于元组注释往往会变成长文字,这证明为简洁起见创建目的变量是合理的。但一般来说,显式签名更具描述性,这是大多数 API 的目标。

The typing Module

Fundamental building blocks:

Tuple, used by listing the element types, for example Tuple[int, int, str]. The empty tuple can be typed as Tuple[()]. Arbitrary-length homogeneous tuples can be expressed using one type and ellipsis, for example Tuple[int, ...]. (The ... here are part of the syntax, a literal ellipsis.)