This commit is contained in:
Will McGugan 2020-11-19 18:23:20 +00:00
parent 084860050b
commit ecc42312fa
2 changed files with 37 additions and 0 deletions

View File

@ -0,0 +1,5 @@
rich.protocol
=============
.. automodule:: rich.protocol
:members:

32
rich/abc.py Normal file
View File

@ -0,0 +1,32 @@
from abc import ABC
from .protocol import is_rich_object
class RichRenderable(ABC):
"""An abstract base class for Rich renderables.
Use this to check if an object supports the Rich renderable protocol. For example::
if isinstance(my_object, RichRenderable):
console.print(my_object)
"""
@classmethod
def __subclasscheck__(cls, other) -> bool:
return is_rich_object(other)
if __name__ == "__main__":
from rich.text import Text
t = Text()
print(isinstance(t, RichRenderable))
class Foo:
pass
f = Foo()
print(isinstance(f, RichRenderable))
print(isinstance("", RichRenderable))