3
3
import functools
4
4
import sys
5
5
from types import TracebackType
6
- from typing import Callable , Generic , Mapping , Optional , Type , TypeVar
6
+ from typing import Any , Callable , Generic , Mapping , Optional , Type , TypeVar
7
7
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
+ )
9
17
10
18
from .._typing_extensions import ParamSpec
19
+ from ..render .renderer import Renderer
11
20
12
21
P = ParamSpec ("P" )
13
22
R = TypeVar ("R" )
14
23
U = TypeVar ("U" )
15
24
16
25
26
+ def only_append_renderable (handler : Callable [[object ], None ]) -> Callable [[Any ], None ]:
27
+ def f (x : Any ):
28
+ if isinstance (x , (list , tuple )):
29
+ for item in x : # pyright: ignore[reportUnknownVariableType]
30
+ f (item )
31
+ return
32
+ elif isinstance (x , (str , float , int , bool )):
33
+ handler (x )
34
+ return
35
+ elif isinstance (x , (HTML , Tag , TagList , Tagifiable , ReprHtml , Renderer )):
36
+ handler (x ) # pyright: ignore[reportUnknownArgumentType]
37
+ return
38
+
39
+ trunc_to = 80
40
+ x_trunc = f"{ x !r} "
41
+ if len (x_trunc ) > trunc_to :
42
+ x_trunc = x_trunc [:trunc_to ] + "..."
43
+
44
+ print (
45
+ # TODO: Make this a more informative warning
46
+ f"Express has suppressed the object `{ x_trunc } ` because it is of type { type (x )} . "
47
+ "Coerce to HTML, a string, or an htmltools tag to display this object."
48
+ )
49
+
50
+ return f
51
+
52
+
17
53
class RecallContextManager (Generic [R ]):
18
54
def __init__ (
19
55
self ,
@@ -31,7 +67,9 @@ def __init__(
31
67
self .kwargs : dict [str , object ] = dict (kwargs )
32
68
# Let htmltools.wrap_displayhook_handler decide what to do with objects before
33
69
# we append them.
34
- self .wrapped_append = wrap_displayhook_handler (self .args .append )
70
+ self .wrapped_append = only_append_renderable (
71
+ wrap_displayhook_handler (self .args .append )
72
+ )
35
73
36
74
def __enter__ (self ) -> None :
37
75
self ._prev_displayhook = sys .displayhook
0 commit comments