class 指针的正确类型提示是什么?
What is the correct type hint for a class pointer?
我不确定 class pointer 是否是 python 中的正确术语,但我正在寻找这个简约示例中的适当类型提示:
class AnotherClass:
some_var=5
class SomeClass:
def some_function(self, model: AnotherClass) -> AnotherClass:
return model()
当我用 mypy 对上面的代码片段进行类型检查时,抛出以下错误:
some_dir/some_file.py:6: error: "AnotherClass" not callable
那么: 在这种情况下,模型参数的适当类型提示是什么?
下面去掉mypy报错但是好像不对..
def some_function(self, model: Callable[..., AnotherClass]) -> AnotherClass:
return model()
我相信你想要 Type[AnotherClass]
因为你传递的是 class 本身,而不是 class.
的一个实例
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.
我不确定 class pointer 是否是 python 中的正确术语,但我正在寻找这个简约示例中的适当类型提示:
class AnotherClass:
some_var=5
class SomeClass:
def some_function(self, model: AnotherClass) -> AnotherClass:
return model()
当我用 mypy 对上面的代码片段进行类型检查时,抛出以下错误:
some_dir/some_file.py:6: error: "AnotherClass" not callable
那么: 在这种情况下,模型参数的适当类型提示是什么?
下面去掉mypy报错但是好像不对..
def some_function(self, model: Callable[..., AnotherClass]) -> AnotherClass:
return model()
我相信你想要 Type[AnotherClass]
因为你传递的是 class 本身,而不是 class.
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.