[Python-ideas] abc.optionalabstractmethod
Masklinn
masklinn at masklinn.net
Thu Aug 2 17:42:59 CEST 2012
On 2012-08-02, at 17:26 , Eric Snow wrote:
> Sometimes you want to specific an optional method in an abstract base
> class. Currently we don't have a consistent way of doing so, instead
> having to mix in the old way of defining "abstract" methods:
>
> class MyABC(metaclass=ABCMeta):
> ...
>
> def do_something_optional(self):
> """An optional method for doing something."""
> raise NotImplementedError
>
> This came up in issue 15502[1], where we are splitting
> importlib.abc.Finder into MetaPathFinder and PathEntryFinder. These
> have a method, invalidate_caches(), which is optional. It would be
> nice to have a new decorator akin to abstractmethod that would allow
> defining an optional interface in a consistent way. Something like
> optionalabstractmethod (or some better name). Then the above example
> would be like this:
>
> class MyABC(metaclass=ABCMeta):
> ...
>
> @optionalabstractmethod
> def do_something_optional(self):
> """An optional method for doing something."""
>
> Thoughts?
Wouldn't it be better to have a generic @optional applying to all @abstract*? e.g.
class MyABC*metaclass=ABCMeta):
@optional
@abstractmethod
def do_something_optional(self):
""" an optional method for doing something """
(yeah I know all of them apart from @abstractmethod are deprecated, still it
feels ugly and unreadable to have @optionalabstractmethod next to @abstractmethod)
More information about the Python-ideas
mailing list