Currently, as far as I know, there’s no way to recover the registered subclasses of abc.ABC
(through cls.register(MyClass)
)
class A:
pass
class Array(abc.ABC):
pass
Array.register(A) # How to recover A from Array after this call ?
assert isinstance(A(), Array)
abc.get_registered(Array) # == [A] # <<< How to do this ?
It seems that Array
has a Array._abc_impl
but I wasn’t able to recover the list of registered subclasses from it.
Example of use-case:
IPython allow to register some custom rendering function for specific classes:
ipython = IPython.get_ipython()
formatter = ipython.display_formatter.formatters['text/html']
formatter.for_type(Array, _display_array) # Save `_cls_to_format_fn = {Array: _display_array}`
However because Array
is not in the A.mro()
, displaying A()
won’t call our custom display function.
Indeed IPython matching is done through lookup (cls in {Array: _display_array}
) and not isinstance
.
With abc.get_registered(Array)
, it would be possible to register all subclasses:
for cls in abc.get_registered(Array):
formatter.for_type(cls, _display_array)