Issue
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.
Solution
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
Answered By - gmds
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.