Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/rich/protocol.py: 26%
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1from typing import Any, cast, Set, TYPE_CHECKING
3if TYPE_CHECKING:
4 from rich.console import RenderableType
6_GIBBERISH = """aihwerij235234ljsdnp34ksodfipwoe234234jlskjdf"""
9def is_renderable(check_object: Any) -> bool:
10 """Check if an object may be rendered by Rich."""
11 return (
12 isinstance(check_object, str)
13 or hasattr(check_object, "__rich__")
14 or hasattr(check_object, "__rich_console__")
15 )
18def rich_cast(renderable: object) -> "RenderableType":
19 """Cast an object to a renderable by calling __rich__ if present.
21 Args:
22 renderable (object): A potentially renderable object
24 Returns:
25 object: The result of recursively calling __rich__.
26 """
27 from rich.console import RenderableType
29 rich_visited_set: Set[type] = set() # Prevent potential infinite loop
30 while hasattr(renderable, "__rich__") and not isinstance(renderable, type):
31 # Detect object which claim to have all the attributes
32 if hasattr(renderable, _GIBBERISH):
33 return repr(renderable)
34 cast_method = getattr(renderable, "__rich__")
35 renderable = cast_method()
36 renderable_type = type(renderable)
37 if renderable_type in rich_visited_set:
38 break
39 rich_visited_set.add(renderable_type)
41 return cast(RenderableType, renderable)