使用用户定义的类进行类型提示

211

似乎找不到一个明确的答案。我想为一个函数进行类型提示,类型是我定义的自定义类,称之为CustomClass()

然后假设在某个函数中,称之为FuncA(arg),我有一个名为arg 的参数。那么正确的方式来为FuncA 进行类型提示是:

def FuncA(arg: CustomClass):

还是会是这样吗:

from typing import Type

def FuncA(Arg:Type[CustomClass]):
2个回答

265

如果arg接受一个CustomClass的实例,那么前者是正确的:

def FuncA(arg: CustomClass):
    #     ^ instance of CustomClass

如果您想要 CustomClass 类(或其子类型)本身,那么您应该编写:

from typing import Type  # you have to import Type

def FuncA(arg: Type[CustomClass]):
    #     ^ CustomClass (class object) itself

就像在有关Typing的文档中所写的那样:

<b>class typing.Type(Generic[CT_co])</b>

A variable annotated with C may accept a value of type C. In contrast, a variable annotated with Type[C] may accept values that are classes themselves - specifically, it will accept the class object of C.

文档中包含了一个与 int 类有关的示例:
a = 3         # Has type 'int'
b = int       # Has type 'Type[int]'
c = type(a)   # Also has type 'Type[int]'

33
如您所说,您也可以使用一个字符串。因此,def foo(bar: 'Qux')def foo(bar: Qux) 是等价的,只是前者不需要立即加载类型。 - Willem Van Onsem
3
@cs95 是的,所有类型提示都需要在Python 3.7或更高版本中使用。 - thiras
1
@WillemVanOnsem 很有用!VS Code 还支持使用 Microsoft 的 Python Linter 和 Debugger,使用 Pylance(而不是 Jedi)Python 语言服务器。 - DanielTuzes

28

Willem Van Onsem的回答当然是正确的,但我想提供一个小更新。在PEP 585中,标准集合引入了泛型类型提示。例如,以前我们必须这样说:

from typing import Dict

foo: Dict[str, str] = { "bar": "baz" }

现在我们可以放弃typing模块中的并行类型层次结构,直接说

foo: dict[str, str] = { "bar": "baz" }

此功能在Python 3.9+中可用,如果使用from __future__ import annotations,也可在3.7+中使用。

对于这个具体问题,这意味着我们现在可以使用内置的type来注释类,而不是使用from typing import Type

def FuncA(arg: type[CustomClass]):

4
这是Python 3.9的正确答案,因为Willem Van Onsem所给出的typing.Type已被弃用。 - bad_coder

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接