removed is_rich_object, so as not to have 2 ways of doing it.

This commit is contained in:
Will McGugan 2020-11-19 18:47:37 +00:00
parent ecc42312fa
commit c3b2c40c5b
4 changed files with 14 additions and 21 deletions

View file

@ -11,7 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Added get_datetime parameter to Console, to allow for repeatable tests
- Added get_time parameter to Console
- Added rich.protocol.is_rich_object and rich.abc.RichRenderable
- Added rich.abc.RichRenderable
### Fixed

View file

@ -3,8 +3,7 @@ Reference
.. toctree::
:maxdepth: 3
reference/init.rst
reference/align.rst
reference/bar.rst
reference/color.rst
@ -12,6 +11,7 @@ Reference
reference/console.rst
reference/emoji.rst
reference/highlighter.rst
reference/init.rst
reference/logging.rst
reference/markdown.rst
reference/markup.rst
@ -19,8 +19,8 @@ Reference
reference/padding.rst
reference/panel.rst
reference/pretty.rst
reference/progress.rst
reference/progress_bar.rst
reference/progress.rst
reference/prompt.rst
reference/protocol.rst
reference/rule.rst
@ -32,3 +32,4 @@ Reference
reference/text.rst
reference/theme.rst
reference/traceback.rst
references/abc.rst

View file

@ -1,12 +1,11 @@
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::
Note that there is no need to extend this class, the intended use is to check if an
object supports the Rich renderable protocol. For example::
if isinstance(my_object, RichRenderable):
console.print(my_object)
@ -14,14 +13,16 @@ class RichRenderable(ABC):
"""
@classmethod
def __subclasscheck__(cls, other) -> bool:
return is_rich_object(other)
def __subclasshook__(cls, other: type) -> bool:
"""Check if this class supports the rich render protocol."""
return hasattr(other, "__rich_console__") or hasattr(other, "__rich__")
if __name__ == "__main__":
if __name__ == "__main__": # pragma: no cover
from rich.text import Text
t = Text()
print(isinstance(Text, RichRenderable))
print(isinstance(t, RichRenderable))
class Foo:

View file

@ -1,17 +1,8 @@
from typing import Any
def is_rich_object(check_object: Any) -> bool:
"""Check if an object is a Rich renderable."""
return hasattr(check_object, "__rich_console__") or hasattr(
check_object, "__rich__"
)
from .abc import RichRenderable
def is_renderable(check_object: Any) -> bool:
"""Check if an object may be rendered by Rich."""
return (
isinstance(check_object, str)
or hasattr(check_object, "__rich_console__")
or hasattr(check_object, "__rich__")
)
return isinstance(check_object, str) or isinstance(check_object, RichRenderable)