33import functools
44import sys
55from types import TracebackType
6- from typing import Callable , Generic , Mapping , Optional , Type , TypeVar
6+ from typing import Any , Callable , Generic , Mapping , Optional , Type , TypeVar
77
8- from htmltools import MetadataNode , Tag , TagList , wrap_displayhook_handler
8+ from htmltools import (
9+ HTML ,
10+ MetadataNode ,
11+ ReprHtml ,
12+ Tag ,
13+ Tagifiable ,
14+ TagList ,
15+ wrap_displayhook_handler ,
16+ )
917
1018from .._typing_extensions import ParamSpec
19+ from ..render .renderer import Renderer
1120
1221P = ParamSpec ("P" )
1322R = TypeVar ("R" )
1423U = TypeVar ("U" )
1524
1625
26+ def only_append_renderable (handler : Callable [[object ], None ]) -> Callable [[Any ], None ]:
27+ def f (x : Any ):
28+ if isinstance (x , str ):
29+ return handler (x )
30+ elif isinstance (x , (str , HTML , Tag , TagList , Tagifiable , ReprHtml , Renderer )):
31+ return handler (x ) # pyright: ignore
32+
33+ print (
34+ # TODO: Make this a more informative warning
35+ f"Express is suppressed the object of type { type (x )} . "
36+ "Coerce to HTML, a string, or an htmltools tag to display this object."
37+ )
38+
39+ return f
40+
41+
1742class RecallContextManager (Generic [R ]):
1843 def __init__ (
1944 self ,
@@ -31,7 +56,9 @@ def __init__(
3156 self .kwargs : dict [str , object ] = dict (kwargs )
3257 # Let htmltools.wrap_displayhook_handler decide what to do with objects before
3358 # we append them.
34- self .wrapped_append = wrap_displayhook_handler (self .args .append )
59+ self .wrapped_append = only_append_renderable (
60+ wrap_displayhook_handler (self .args .append )
61+ )
3562
3663 def __enter__ (self ) -> None :
3764 self ._prev_displayhook = sys .displayhook
0 commit comments