6
6
7
7
from .. import unpack_labeled_data
8
8
9
+
9
10
# these two get used in multiple tests, so define them here
10
11
@unpack_labeled_data (replace_names = ["x" ,"y" ])
11
12
def plot_func (ax , x , y , ls = "x" , label = None , w = "xyz" ):
12
13
return "x: %s, y: %s, ls: %s, w: %s, label: %s" % (list (x ),list (y ),ls , w , label )
13
14
14
15
15
- @unpack_labeled_data (replace_names = ["x" ,"y" ], positional_parameter_names = ["ax" , " x" , "y" , "ls" , "label" , "w" ])
16
+ @unpack_labeled_data (replace_names = ["x" ,"y" ], positional_parameter_names = ["x" , "y" , "ls" , "label" , "w" ])
16
17
def plot_func_varags (ax , * args , ** kwargs ):
17
18
all_args = [None , None , "x" , None , "xyz" ]
18
19
for i , v in enumerate (args ):
@@ -26,25 +27,6 @@ def plot_func_varags(ax, *args, **kwargs):
26
27
27
28
all_funcs = [plot_func , plot_func_varags ]
28
29
29
- d # these two get used in multiple tests, so define them here
30
- @unpack_labeled_data (replace_names = ["x" ,"y" ])
31
- def plot_func (ax , x , y , ls = "x" , label = None , w = "xyz" ):
32
- return "x: %s, y: %s, ls: %s, w: %s, label: %s" % (list (x ),list (y ),ls , w , label )
33
-
34
-
35
- @unpack_labeled_data (replace_names = ["x" ,"y" ], positional_parameter_names = ["ax" , "x" , "y" , "ls" , "label" , "w" ])
36
- def plot_func_varags (ax , * args , ** kwargs ):
37
- all_args = [None , None , "x" , None , "xyz" ]
38
- for i , v in enumerate (args ):
39
- all_args [i ] = v
40
- for i , k in enumerate (["x" , "y" , "ls" , "label" , "w" ]):
41
- if k in kwargs :
42
- all_args [i ] = kwargs [k ]
43
- x , y , ls , label , w = all_args
44
- return "x: %s, y: %s, ls: %s, w: %s, label: %s" % (list (x ),list (y ),ls , w , label )
45
-
46
-
47
- all_funcs = [plot_func , plot_func_varags ]
48
30
49
31
def test_compiletime_checks ():
50
32
"""test decorator invocations -> no replacements"""
@@ -112,6 +94,7 @@ def f():
112
94
# This sets a label although the function can't handle it.
113
95
assert_raises (TypeError , f )
114
96
97
+
115
98
def test_function_call_without_data ():
116
99
"""test without data -> no replacements"""
117
100
for func in all_funcs :
@@ -192,13 +175,18 @@ def func_varags_replace_all(ax, *args, **kwargs):
192
175
return "x: %s, y: %s, ls: %s, w: %s, label: %s" % (list (x ),list (y ),ls , w , label )
193
176
194
177
# in the first case, we can't get a "y" argument, as we don't know the names of the *args
195
- assert_equal (func_varags_replace_all (None , "a" ,"b" , w = "x" , data = data ), "x: [1, 2], y: [8, 9], ls: x, w: xyz, label: None" )
196
178
assert_equal (func_varags_replace_all (None , x = "a" ,y = "b" , w = "x" , data = data ) , "x: [1, 2], y: [8, 9], ls: x, w: xyz, label: b" )
197
179
assert_equal (func_varags_replace_all (None , "a" ,"b" , w = "x" , label = "" , data = data ) , "x: [1, 2], y: [8, 9], ls: x, w: xyz, label: " )
198
180
assert_equal (func_varags_replace_all (None , "a" ,"b" , w = "x" , label = "text" , data = data ) , "x: [1, 2], y: [8, 9], ls: x, w: xyz, label: text" )
199
181
assert_equal (func_varags_replace_all (None , x = "a" ,y = "b" , w = "x" , label = "" , data = data ) , "x: [1, 2], y: [8, 9], ls: x, w: xyz, label: " )
200
182
assert_equal (func_varags_replace_all (None , x = "a" ,y = "b" , w = "x" , label = "text" , data = data ) , "x: [1, 2], y: [8, 9], ls: x, w: xyz, label: text" )
201
183
184
+ try :
185
+ from pandas .util .testing import assert_produces_warning
186
+ assert_equal (func_varags_replace_all (None , "a" ,"b" , w = "x" , data = data ), "x: [1, 2], y: [8, 9], ls: x, w: xyz, label: None" )
187
+ except ImportError :
188
+ pass
189
+
202
190
203
191
def test_no_label_replacements ():
204
192
"""Test without "label_namer=None" -> no label replacement at all"""
@@ -213,6 +201,7 @@ def func_no_label(ax, x, y, ls="x", label=None, w="xyz"):
213
201
assert_equal (func_no_label (None , "a" ,"b" , label = "" , data = data ) , "x: [1, 2], y: [8, 9], ls: x, w: xyz, label: " )
214
202
assert_equal (func_no_label (None , "a" ,"b" , label = "text" , data = data ) , "x: [1, 2], y: [8, 9], ls: x, w: xyz, label: text" )
215
203
204
+
216
205
def test_more_args_than_pos_parameter ():
217
206
@unpack_labeled_data (replace_names = ["x" ,"y" ])
218
207
def func (ax , x , y , z = 1 ):
@@ -222,3 +211,31 @@ def func(ax, x, y, z=1):
222
211
def f ():
223
212
func (None , "a" ,"b" , "z" , "z" , data = data )
224
213
assert_raises (RuntimeError , f )
214
+
215
+
216
+ def test_function_call_with_replace_all_args ():
217
+ """Test with a "replace_all_args" argument, all *args should be replaced"""
218
+ data = {"a" :[1 ,2 ],"b" :[8 ,9 ], "x" :"xyz" }
219
+
220
+ def funcy (ax , * args , ** kwargs ):
221
+ all_args = [None , None , "x" , None , "NOT" ]
222
+ for i , v in enumerate (args ):
223
+ all_args [i ] = v
224
+ for i , k in enumerate (["x" , "y" , "ls" , "label" , "w" ]):
225
+ if k in kwargs :
226
+ all_args [i ] = kwargs [k ]
227
+ x , y , ls , label , w = all_args
228
+ return "x: %s, y: %s, ls: %s, w: %s, label: %s" % (list (x ),list (y ),ls , w , label )
229
+
230
+ func = unpack_labeled_data (replace_all_args = True , replace_names = ["w" ])(funcy )
231
+
232
+ #assert_equal(func(None, "a","b", w="x", data=data), "x: [1, 2], y: [8, 9], ls: x, w: xyz, label: None")
233
+ assert_equal (func (None , "a" ,"b" , w = "x" , label = "" , data = data ) , "x: [1, 2], y: [8, 9], ls: x, w: xyz, label: " )
234
+ assert_equal (func (None , "a" ,"b" , w = "x" , label = "text" , data = data ) , "x: [1, 2], y: [8, 9], ls: x, w: xyz, label: text" )
235
+
236
+ func2 = unpack_labeled_data (replace_all_args = True , replace_names = ["w" ],
237
+ positional_parameter_names = ["x" , "y" , "ls" , "label" , "w" ])(funcy )
238
+
239
+ assert_equal (func2 (None , "a" ,"b" , w = "x" , data = data ), "x: [1, 2], y: [8, 9], ls: x, w: xyz, label: b" )
240
+ assert_equal (func2 (None , "a" ,"b" , w = "x" , label = "" , data = data ) , "x: [1, 2], y: [8, 9], ls: x, w: xyz, label: " )
241
+ assert_equal (func2 (None , "a" ,"b" , w = "x" , label = "text" , data = data ) , "x: [1, 2], y: [8, 9], ls: x, w: xyz, label: text" )
0 commit comments