Reputation: 21942
I have an abstract method in my base class, and I want all the subclasses to return an iterable of their expected Exception
classes:
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()
How do I type hint this return value? At first I thought of -> Iterable[Exception]
, but that would mean they are instances of Exception
, as opposed to subclasses.
Upvotes: 8
Views: 3530
Reputation: 19885
You want typing.Type
, which specifies that you are returning a type, as opposed to an instance:
from typing import Type, Iterable
def expected_exceptions(self) -> Iterable[Type[Exception]]:
return ValueError, IndexError
Upvotes: 13