我的基类中有一个抽象方法,我希望所有子类都返回它们预期的 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
关于python - 如何键入提示异常子类的返回值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56336702/