Coverage for /run/media/veganeco/water/status600.com/pypi/reptilian_climates/modules_series_2/ships/fields/gardens_pip/rich/protocol.py: 80%

1 from typing import Any, cast, Set, TYPE_CHECKING

2 from inspect import isclass

3

4 if TYPE_CHECKING:

5 from rich.console import RenderableType

6

7 _GIBBERISH = """aihwerij235234ljsdnp34ksodfipwoe234234jlskjdf"""

8

9

10 def is_renderable(check_object: Any) -> bool:

11 """Check if an object may be rendered by Rich."""

12 return (

13 isinstance(check_object, str)

14 or hasattr(check_object, "__rich__")

15 or hasattr(check_object, "__rich_console__")

16 )

17

18

19 def rich_cast(renderable: object) -> "RenderableType":

20 """Cast an object to a renderable by calling __rich__ if present.

21

22 Args:

23 renderable (object): A potentially renderable object

24

25 Returns:

26 object: The result of recursively calling __rich__.

27 """

28 from rich.console import RenderableType

29

30 rich_visited_set: Set[type] = set() # Prevent potential infinite loop

31 while hasattr(renderable, "__rich__") and not isclass(renderable):

32 # Detect object which claim to have all the attributes

33 if hasattr(renderable, _GIBBERISH):

34 return repr(renderable)

35 cast_method = getattr(renderable, "__rich__")

36 renderable = cast_method()

37 renderable_type = type(renderable)

38 if renderable_type in rich_visited_set:

39 break

40 rich_visited_set.add(renderable_type)

41

42 return cast(RenderableType, renderable)