From ecc42312fa4665d130edd88741fc0fc37ee8ba09 Mon Sep 17 00:00:00 2001 From: Will McGugan Date: Thu, 19 Nov 2020 18:23:20 +0000 Subject: [PATCH] rich abc --- docs/source/reference/protocol.rst | 5 +++++ rich/abc.py | 32 ++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) create mode 100644 docs/source/reference/protocol.rst create mode 100644 rich/abc.py diff --git a/docs/source/reference/protocol.rst b/docs/source/reference/protocol.rst new file mode 100644 index 00000000..4febcee8 --- /dev/null +++ b/docs/source/reference/protocol.rst @@ -0,0 +1,5 @@ +rich.protocol +============= + +.. automodule:: rich.protocol + :members: diff --git a/rich/abc.py b/rich/abc.py new file mode 100644 index 00000000..acb870f5 --- /dev/null +++ b/rich/abc.py @@ -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))