繁体   English   中英

如何输入提示Exception的子类的返回值?

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM