3
3
4
4
import pytest
5
5
6
+ from pandas .compat import PY2
7
+
6
8
import pandas as pd
9
+ from pandas .core .config import OptionError
7
10
8
11
9
12
class TestConfig (object ):
@@ -48,26 +51,35 @@ def test_is_one_of_factory(self):
48
51
49
52
v (12 )
50
53
v (None )
51
- pytest .raises (ValueError , v , 1.1 )
54
+ msg = r"Value must be one of None\|12"
55
+ with pytest .raises (ValueError , match = msg ):
56
+ v (1.1 )
52
57
53
58
def test_register_option (self ):
54
59
self .cf .register_option ('a' , 1 , 'doc' )
55
60
56
61
# can't register an already registered option
57
- pytest .raises (KeyError , self .cf .register_option , 'a' , 1 , 'doc' )
62
+ msg = "Option 'a' has already been registered"
63
+ with pytest .raises (OptionError , match = msg ):
64
+ self .cf .register_option ('a' , 1 , 'doc' )
58
65
59
66
# can't register an already registered option
60
- pytest .raises (KeyError , self .cf .register_option , 'a.b.c.d1' , 1 ,
61
- 'doc' )
62
- pytest .raises (KeyError , self .cf .register_option , 'a.b.c.d2' , 1 ,
63
- 'doc' )
67
+ msg = "Path prefix to option 'a' is already an option"
68
+ with pytest .raises (OptionError , match = msg ):
69
+ self .cf .register_option ('a.b.c.d1' , 1 , 'doc' )
70
+ with pytest .raises (OptionError , match = msg ):
71
+ self .cf .register_option ('a.b.c.d2' , 1 , 'doc' )
64
72
65
73
# no python keywords
66
- pytest .raises (ValueError , self .cf .register_option , 'for' , 0 )
67
- pytest .raises (ValueError , self .cf .register_option , 'a.for.b' , 0 )
74
+ msg = "for is a python keyword"
75
+ with pytest .raises (ValueError , match = msg ):
76
+ self .cf .register_option ('for' , 0 )
77
+ with pytest .raises (ValueError , match = msg ):
78
+ self .cf .register_option ('a.for.b' , 0 )
68
79
# must be valid identifier (ensure attribute access works)
69
- pytest .raises (ValueError , self .cf .register_option ,
70
- 'Oh my Goddess!' , 0 )
80
+ msg = "oh my goddess! is not a valid identifier"
81
+ with pytest .raises (ValueError , match = msg ):
82
+ self .cf .register_option ('Oh my Goddess!' , 0 )
71
83
72
84
# we can register options several levels deep
73
85
# without predefining the intermediate steps
@@ -90,7 +102,9 @@ def test_describe_option(self):
90
102
self .cf .register_option ('l' , "foo" )
91
103
92
104
# non-existent keys raise KeyError
93
- pytest .raises (KeyError , self .cf .describe_option , 'no.such.key' )
105
+ msg = r"No such keys\(s\)"
106
+ with pytest .raises (OptionError , match = msg ):
107
+ self .cf .describe_option ('no.such.key' )
94
108
95
109
# we can get the description for any key we registered
96
110
assert 'doc' in self .cf .describe_option ('a' , _print_desc = False )
@@ -122,7 +136,9 @@ def test_case_insensitive(self):
122
136
assert self .cf .get_option ('kAnBaN' ) == 2
123
137
124
138
# gets of non-existent keys fail
125
- pytest .raises (KeyError , self .cf .get_option , 'no_such_option' )
139
+ msg = r"No such keys\(s\): 'no_such_option'"
140
+ with pytest .raises (OptionError , match = msg ):
141
+ self .cf .get_option ('no_such_option' )
126
142
self .cf .deprecate_option ('KanBan' )
127
143
128
144
assert self .cf ._is_deprecated ('kAnBaN' )
@@ -138,7 +154,9 @@ def test_get_option(self):
138
154
assert self .cf .get_option ('b.b' ) is None
139
155
140
156
# gets of non-existent keys fail
141
- pytest .raises (KeyError , self .cf .get_option , 'no_such_option' )
157
+ msg = r"No such keys\(s\): 'no_such_option'"
158
+ with pytest .raises (OptionError , match = msg ):
159
+ self .cf .get_option ('no_such_option' )
142
160
143
161
def test_set_option (self ):
144
162
self .cf .register_option ('a' , 1 , 'doc' )
@@ -157,16 +175,24 @@ def test_set_option(self):
157
175
assert self .cf .get_option ('b.c' ) == 'wurld'
158
176
assert self .cf .get_option ('b.b' ) == 1.1
159
177
160
- pytest .raises (KeyError , self .cf .set_option , 'no.such.key' , None )
178
+ msg = r"No such keys\(s\): 'no.such.key'"
179
+ with pytest .raises (OptionError , match = msg ):
180
+ self .cf .set_option ('no.such.key' , None )
161
181
162
182
def test_set_option_empty_args (self ):
163
- pytest .raises (ValueError , self .cf .set_option )
183
+ msg = "Must provide an even number of non-keyword arguments"
184
+ with pytest .raises (ValueError , match = msg ):
185
+ self .cf .set_option ()
164
186
165
187
def test_set_option_uneven_args (self ):
166
- pytest .raises (ValueError , self .cf .set_option , 'a.b' , 2 , 'b.c' )
188
+ msg = "Must provide an even number of non-keyword arguments"
189
+ with pytest .raises (ValueError , match = msg ):
190
+ self .cf .set_option ('a.b' , 2 , 'b.c' )
167
191
168
192
def test_set_option_invalid_single_argument_type (self ):
169
- pytest .raises (ValueError , self .cf .set_option , 2 )
193
+ msg = "Must provide an even number of non-keyword arguments"
194
+ with pytest .raises (ValueError , match = msg ):
195
+ self .cf .set_option (2 )
170
196
171
197
def test_set_option_multiple (self ):
172
198
self .cf .register_option ('a' , 1 , 'doc' )
@@ -183,27 +209,36 @@ def test_set_option_multiple(self):
183
209
assert self .cf .get_option ('b.c' ) is None
184
210
assert self .cf .get_option ('b.b' ) == 10.0
185
211
212
+ @pytest .mark .skipif (PY2 , reason = "pytest.raises match regex fails" )
186
213
def test_validation (self ):
187
214
self .cf .register_option ('a' , 1 , 'doc' , validator = self .cf .is_int )
188
215
self .cf .register_option ('b.c' , 'hullo' , 'doc2' ,
189
216
validator = self .cf .is_text )
190
- pytest .raises (ValueError , self .cf .register_option , 'a.b.c.d2' ,
191
- 'NO' , 'doc' , validator = self .cf .is_int )
217
+ msg = "Value must have type '<class 'int'>'"
218
+ with pytest .raises (ValueError , match = msg ):
219
+ self .cf .register_option (
220
+ 'a.b.c.d2' , 'NO' , 'doc' , validator = self .cf .is_int )
192
221
193
222
self .cf .set_option ('a' , 2 ) # int is_int
194
223
self .cf .set_option ('b.c' , 'wurld' ) # str is_str
195
224
196
- pytest .raises (
197
- ValueError , self .cf .set_option , 'a' , None ) # None not is_int
198
- pytest .raises (ValueError , self .cf .set_option , 'a' , 'ab' )
199
- pytest .raises (ValueError , self .cf .set_option , 'b.c' , 1 )
225
+ # None not is_int
226
+ with pytest .raises (ValueError , match = msg ):
227
+ self .cf .set_option ('a' , None )
228
+ with pytest .raises (ValueError , match = msg ):
229
+ self .cf .set_option ('a' , 'ab' )
230
+
231
+ msg = r"Value must be an instance of <class 'str'>\|<class 'bytes'>"
232
+ with pytest .raises (ValueError , match = msg ):
233
+ self .cf .set_option ('b.c' , 1 )
200
234
201
235
validator = self .cf .is_one_of_factory ([None , self .cf .is_callable ])
202
236
self .cf .register_option ('b' , lambda : None , 'doc' ,
203
237
validator = validator )
204
238
self .cf .set_option ('b' , '%.1f' .format ) # Formatter is callable
205
239
self .cf .set_option ('b' , None ) # Formatter is none (default)
206
- pytest .raises (ValueError , self .cf .set_option , 'b' , '%.1f' )
240
+ with pytest .raises (ValueError , match = "Value must be a callable" ):
241
+ self .cf .set_option ('b' , '%.1f' )
207
242
208
243
def test_reset_option (self ):
209
244
self .cf .register_option ('a' , 1 , 'doc' , validator = self .cf .is_int )
@@ -267,8 +302,9 @@ def test_deprecate_option(self):
267
302
assert 'eprecated' in str (w [- 1 ]) # we get the default message
268
303
assert 'nifty_ver' in str (w [- 1 ]) # with the removal_ver quoted
269
304
270
- pytest .raises (
271
- KeyError , self .cf .deprecate_option , 'a' ) # can't depr. twice
305
+ msg = "Option 'a' has already been defined as deprecated"
306
+ with pytest .raises (OptionError , match = msg ):
307
+ self .cf .deprecate_option ('a' )
272
308
273
309
self .cf .deprecate_option ('b.c' , 'zounds!' )
274
310
with warnings .catch_warnings (record = True ) as w :
@@ -374,12 +410,6 @@ def eq(val):
374
410
def test_attribute_access (self ):
375
411
holder = []
376
412
377
- def f ():
378
- options .b = 1
379
-
380
- def f2 ():
381
- options .display = 1
382
-
383
413
def f3 (key ):
384
414
holder .append (True )
385
415
@@ -397,8 +427,11 @@ def f3(key):
397
427
self .cf .reset_option ("a" )
398
428
assert options .a == self .cf .get_option ("a" , 0 )
399
429
400
- pytest .raises (KeyError , f )
401
- pytest .raises (KeyError , f2 )
430
+ msg = "You can only set the value of existing options"
431
+ with pytest .raises (OptionError , match = msg ):
432
+ options .b = 1
433
+ with pytest .raises (OptionError , match = msg ):
434
+ options .display = 1
402
435
403
436
# make sure callback kicks when using this form of setting
404
437
options .c = 1
@@ -429,5 +462,6 @@ def test_option_context_scope(self):
429
462
def test_dictwrapper_getattr (self ):
430
463
options = self .cf .options
431
464
# GH 19789
432
- pytest .raises (self .cf .OptionError , getattr , options , 'bananas' )
465
+ with pytest .raises (OptionError , match = "No such option" ):
466
+ options .bananas
433
467
assert not hasattr (options , 'bananas' )
0 commit comments