abc.py 890 B

123456789101112131415161718192021222324252627282930313233
  1. from abc import ABC
  2. class RichRenderable(ABC):
  3. """An abstract base class for Rich renderables.
  4. Note that there is no need to extend this class, the intended use is to check if an
  5. object supports the Rich renderable protocol. For example::
  6. if isinstance(my_object, RichRenderable):
  7. console.print(my_object)
  8. """
  9. @classmethod
  10. def __subclasshook__(cls, other: type) -> bool:
  11. """Check if this class supports the rich render protocol."""
  12. return hasattr(other, "__rich_console__") or hasattr(other, "__rich__")
  13. if __name__ == "__main__": # pragma: no cover
  14. from pip._vendor.rich.text import Text
  15. t = Text()
  16. print(isinstance(Text, RichRenderable))
  17. print(isinstance(t, RichRenderable))
  18. class Foo:
  19. pass
  20. f = Foo()
  21. print(isinstance(f, RichRenderable))
  22. print(isinstance("", RichRenderable))