@@ -24,14 +24,33 @@ def test_doc(a):
24
24
assert "Encode the string using the codec" in a .encode .__doc__
25
25
26
26
27
+ class _A :
28
+ pass
29
+
30
+
31
+ def test_missing_doc ():
32
+ with pytest .raises (AttributeError ):
33
+ skrub .X ().__doc__
34
+
35
+ with pytest .raises (AttributeError ):
36
+ skrub .X (_A ()).__doc__
37
+
38
+
27
39
@pytest .mark .parametrize ("a" , example_strings ())
28
40
def test_signature (a ):
29
41
assert "encoding" in inspect .signature (a .encode ).parameters
30
42
31
43
44
+ def test_missing_signature ():
45
+ with pytest .raises (AttributeError ):
46
+ skrub .X (0 ).__signature__
47
+
48
+
32
49
def test_key_completions ():
33
50
a = skrub .var ("a" , {"one" : 1 }) | skrub .var ("b" , {"two" : 2 })
34
51
assert a ._ipython_key_completions_ () == ["one" , "two" ]
52
+ assert skrub .X ()._ipython_key_completions_ () == []
53
+ assert skrub .X (0 )._ipython_key_completions_ () == []
35
54
36
55
37
56
def test_repr_html ():
@@ -41,6 +60,9 @@ def test_repr_html():
41
60
a = skrub .var ("thename" , skrub .toy_orders ().orders )
42
61
r = a ._repr_html_ ()
43
62
assert "thename" in r and "table-report" in r
63
+ assert "thename" in skrub .var ("thename" )._repr_html_ ()
64
+ # example without a name
65
+ assert "add" in (skrub .var ("thename" , 0 ) + 2 )._repr_html_ ()
44
66
45
67
46
68
def test_repr ():
@@ -60,4 +82,118 @@ def test_repr():
60
82
Result:
61
83
―――――――
62
84
'one two'
85
+ >>> skrub.as_expr({'a': 0})
86
+ <Value dict>
87
+ Result:
88
+ ―――――――
89
+ {'a': 0}
90
+ >>> skrub.var('a', 1).skb.match({1: 10, 2: 20})
91
+ <Match <Var 'a'>>
92
+ >>> from sklearn.preprocessing import StandardScaler, RobustScaler
93
+ >>> skrub.X().skb.apply(StandardScaler())
94
+ <Apply StandardScaler>
95
+ >>> skrub.X().skb.apply('passthrough')
96
+ <Apply 'passthrough'>
97
+ >>> skrub.X().skb.apply(None)
98
+ <Apply passthrough>
99
+ >>> skrub.X().skb.apply(skrub.optional(StandardScaler(), name='scale'))
100
+ <Apply StandardScaler>
101
+ >>> skrub.X().skb.apply(
102
+ ... skrub.choose_from([RobustScaler(), StandardScaler()], name='scale'))
103
+ <Apply RobustScaler>
104
+ >>> skrub.as_expr({'a': 0})['a']
105
+ <GetItem 'a'>
106
+ Result:
107
+ ―――――――
108
+ 0
109
+ >>> skrub.as_expr({'a': 0, 'b': 1})[skrub.choose_from(['a', 'b'], name='c')]
110
+ <GetItem choose_from(['a', 'b'], name='c')>
111
+ Result:
112
+ ―――――――
113
+ 0
114
+ >>> skrub.as_expr({'a': 0, 'b': 1})[skrub.var('key', 'b')]
115
+ <GetItem <Var 'key'>>
116
+ Result:
117
+ ―――――――
118
+ 1
119
+ >>> skrub.as_expr('hello').upper()
120
+ <CallMethod 'upper'>
121
+ Result:
122
+ ―――――――
123
+ 'HELLO'
124
+ >>> a = skrub.var('a', 'hello')
125
+ >>> b = skrub.var('b', 1)
126
+ >>> skrub.as_expr({0: a.upper, 1: a.title})[b]()
127
+ <Call "{ ... }[<Var 'b'>]">
128
+ Result:
129
+ ―――――――
130
+ 'Hello'
131
+ >>> skrub.var('f', str.upper)('abc')
132
+ <Call 'f'>
133
+ Result:
134
+ ―――――――
135
+ 'ABC'
136
+
137
+ Weird (unnecessary) use of deferred to trigger a case where calling a
138
+ method has not been translated to a CallMethod
139
+
140
+ >>> skrub.deferred(skrub.var('a', 'hello').upper)()
141
+ <Call 'upper'>
142
+ Result:
143
+ ―――――――
144
+ 'HELLO'
145
+
146
+ In cases that are hard to figure out we fall back on a less informative
147
+ default
148
+
149
+ >>> skrub.choose_from([str.upper, str.title], name='f').as_expr()('abc')
150
+ <Call 'Value'>
151
+ Result:
152
+ ―――――――
153
+ 'ABC'
154
+ >>> skrub.as_expr(str.upper)('abc')
155
+ <Call 'Value'>
156
+ Result:
157
+ ―――――――
158
+ 'ABC'
159
+
160
+ >>> a = skrub.var('a')
161
+ >>> b = skrub.var('b')
162
+ >>> c = skrub.var('c', 0)
163
+ >>> a + b
164
+ <BinOp: add>
165
+ >>> - a
166
+ <UnaryOp: neg>
167
+ >>> 2 + a
168
+ <BinOp: add>
169
+ >>> c + c
170
+ <BinOp: add>
171
+ Result:
172
+ ―――――――
173
+ 0
174
+ >>> - c
175
+ <UnaryOp: neg>
176
+ Result:
177
+ ―――――――
178
+ 0
179
+ >>> 2 - c
180
+ <BinOp: sub>
181
+ Result:
182
+ ―――――――
183
+ 2
184
+
185
+ >>> X = skrub.X()
186
+ >>> X.skb.concat_horizontal([X, X])
187
+ <ConcatHorizontal: 3 dataframes>
188
+
189
+ When we do not know the length of the list of dataframes to concatenate
190
+
191
+ >>> X.skb.concat_horizontal(skrub.as_expr([X, X]))
192
+ <ConcatHorizontal>
63
193
"""
194
+
195
+
196
+ def test_format ():
197
+ assert f"{ skrub .X ()} " == "<Var 'X'>"
198
+ with pytest .raises (ValueError , match = "Invalid format specifier" ):
199
+ f"{ skrub .X (0.2 ):.2f} "
0 commit comments