![](/img/trans.png)
[英]How to define a type hint to a argument (the argument's value is a class, all expected value is a subclass of a certain class)?
[英]How to type hint a return value of Exception's subclass?
我的基类中有一个抽象方法,我希望所有子类都返回它们预期的Exception
类的迭代:
class Foo(metaclass=ABCMeta):
@abstractmethod
def expected_exceptions(self):
raise NotImplementedError()
class Bar(Foo):
def expected_exceptions(self):
return ValueError, IndexError
class Baz(Foo):
def expected_exceptions(self):
yield from self.manager._get_exceptions()
如何输入提示此返回值? 起初我想到了-> Iterable[Exception]
,但这意味着它们是Exception
实例,而不是子类。
你想要typing.Type
,它指定你返回一个类型 ,而不是一个实例 :
from typing import Type, Iterable
def expected_exceptions(self) -> Iterable[Type[Exception]]:
return ValueError, IndexError
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.