如何解决使用用户定义的类键入提示?

的,如果arg接受以下 :

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

如果你想要 ,那么你应该写:

from typing import Type  # you have to import Type

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

就像它写在关于 的文档中一样:

**class typing.Type(Generic[CT_co])**

带有注释的变量C可以接受 type 的值C。相反,带有 ——具体来说,它将接受 的。

该文档包含一个带有int该类的示例:

a = 3         # Has type 'int'
b = int       # Has type 'Type[int]'
c = type(a)   # Also has type 'Type[int]'

解决方法

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

然后让我们说在某个函数中,调用它FuncA(arg),我有一个名为 的参数arg。输入提示的正确方法FuncA是:

def FuncA(arg: CustomClass):

或者会是:

from typing import Type

def FuncA(Arg:Type[CustomClass]):