codekingpro/portable-devtools
114k
1from abc import ABC2 3 4class RichRenderable(ABC):5 """An abstract base class for Rich renderables.6 7 Note that there is no need to extend this class, the intended use is to check if an8 object supports the Rich renderable protocol. For example::9 10 if isinstance(my_object, RichRenderable):11 console.print(my_object)12 13 """14 15 @classmethod16 def __subclasshook__(cls, other: type) -> bool:17 """Check if this class supports the rich render protocol."""18 return hasattr(other, "__rich_console__") or hasattr(other, "__rich__")19 20 21if __name__ == "__main__": # pragma: no cover22 from rich.text import Text23 24 t = Text()25 print(isinstance(Text, RichRenderable))26 print(isinstance(t, RichRenderable))27 28 class Foo:29 pass30 31 f = Foo()32 print(isinstance(f, RichRenderable))33 print(isinstance("", RichRenderable))34 