protocol and abc

This commit is contained in:
Will McGugan 2020-11-19 18:22:10 +00:00
parent 48bb8b9c63
commit 084860050b
9 changed files with 50 additions and 12 deletions

View file

@ -1,12 +1,13 @@
import io
from rich.abc import RichRenderable
from rich.console import Console
from rich.panel import Panel
from rich.text import Text
class Foo:
def __rich__(self):
def __rich__(self) -> Text:
return Text("Foo")
@ -22,3 +23,13 @@ def test_rich_cast_container():
console = Console(file=io.StringIO(), legacy_windows=False)
console.print(Panel.fit(foo, padding=0))
assert console.file.getvalue() == "╭───╮\n│Foo│\n╰───╯\n"
def test_abc():
foo = Foo()
assert isinstance(foo, RichRenderable)
assert isinstance(Text("hello"), RichRenderable)
assert isinstance(Panel("hello"), RichRenderable)
assert not isinstance(foo, str)
assert not isinstance("foo", RichRenderable)
assert not isinstance([], RichRenderable)