Skip to content

Commit 63fc8af

Browse files
committed
CLN/BUG/ENH: Raise AttributeError with pd.options
It's called OptionError now and it subclasses both AttributeError (for hasattr) and KeyError (for backwards compatibility). Hurray!
1 parent 10ca4f0 commit 63fc8af

File tree

1 file changed

+19
-13
lines changed

1 file changed

+19
-13
lines changed

pandas/core/config.py

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,11 @@
6565
_reserved_keys = ['all'] # keys which have a special meaning
6666

6767

68+
class OptionError(AttributeError, KeyError):
69+
"""Exception for pandas.options, backwards compatible with KeyError
70+
checks"""
71+
72+
6873
##########################################
6974
# User API
7075

@@ -73,9 +78,9 @@ def _get_single_key(pat, silent):
7378
if len(keys) == 0:
7479
if not silent:
7580
_warn_if_deprecated(pat)
76-
raise KeyError('No such keys(s): %r' % pat)
81+
raise OptionError('No such keys(s): %r' % pat)
7782
if len(keys) > 1:
78-
raise KeyError('Pattern matched multiple keys')
83+
raise OptionError('Pattern matched multiple keys')
7984
key = keys[0]
8085

8186
if not silent:
@@ -147,7 +152,7 @@ def _describe_option(pat='', _print_desc=True):
147152

148153
keys = _select_options(pat)
149154
if len(keys) == 0:
150-
raise KeyError('No such keys(s)')
155+
raise OptionError('No such keys(s)')
151156

152157
s = u('')
153158
for k in keys: # filter by pat
@@ -164,7 +169,7 @@ def _reset_option(pat):
164169
keys = _select_options(pat)
165170

166171
if len(keys) == 0:
167-
raise KeyError('No such keys(s)')
172+
raise OptionError('No such keys(s)')
168173

169174
if len(keys) > 1 and len(pat) < 4 and pat != 'all':
170175
raise ValueError('You must specify at least 4 characters when '
@@ -195,7 +200,7 @@ def __setattr__(self, key, val):
195200
if key in self.d and not isinstance(self.d[key], dict):
196201
_set_option(prefix, val)
197202
else:
198-
raise KeyError("You can only set the value of existing options")
203+
raise OptionError("You can only set the value of existing options")
199204

200205
def __getattr__(self, key):
201206
prefix = object.__getattribute__(self, "prefix")
@@ -211,6 +216,7 @@ def __getattr__(self, key):
211216
def __dir__(self):
212217
return list(self.d.keys())
213218

219+
214220
# For user convenience, we'd like to have the available options described
215221
# in the docstring. For dev convenience we'd like to generate the docstrings
216222
# dynamically instead of maintaining them by hand. To this, we use the
@@ -255,7 +261,7 @@ def __doc__(self):
255261
256262
Raises
257263
------
258-
KeyError if no such option exists
264+
OptionError if no such option exists
259265
260266
{opts_desc}
261267
"""
@@ -281,7 +287,7 @@ def __doc__(self):
281287
282288
Raises
283289
------
284-
KeyError if no such option exists
290+
OptionError if no such option exists
285291
286292
{opts_desc}
287293
"""
@@ -398,9 +404,9 @@ def register_option(key, defval, doc='', validator=None, cb=None):
398404
key = key.lower()
399405

400406
if key in _registered_options:
401-
raise KeyError("Option '%s' has already been registered" % key)
407+
raise OptionError("Option '%s' has already been registered" % key)
402408
if key in _reserved_keys:
403-
raise KeyError("Option '%s' is a reserved key" % key)
409+
raise OptionError("Option '%s' is a reserved key" % key)
404410

405411
# the default value should be legal
406412
if validator:
@@ -418,14 +424,14 @@ def register_option(key, defval, doc='', validator=None, cb=None):
418424
cursor = _global_config
419425
for i, p in enumerate(path[:-1]):
420426
if not isinstance(cursor, dict):
421-
raise KeyError("Path prefix to option '%s' is already an option"
427+
raise OptionError("Path prefix to option '%s' is already an option"
422428
% '.'.join(path[:i]))
423429
if p not in cursor:
424430
cursor[p] = {}
425431
cursor = cursor[p]
426432

427433
if not isinstance(cursor, dict):
428-
raise KeyError("Path prefix to option '%s' is already an option"
434+
raise OptionError("Path prefix to option '%s' is already an option"
429435
% '.'.join(path[:-1]))
430436

431437
cursor[path[-1]] = defval # initialize
@@ -470,14 +476,14 @@ def deprecate_option(key, msg=None, rkey=None, removal_ver=None):
470476
471477
Raises
472478
------
473-
KeyError - if key has already been deprecated.
479+
OptionError - if key has already been deprecated.
474480
475481
"""
476482

477483
key = key.lower()
478484

479485
if key in _deprecated_options:
480-
raise KeyError("Option '%s' has already been defined as deprecated."
486+
raise OptionError("Option '%s' has already been defined as deprecated."
481487
% key)
482488

483489
_deprecated_options[key] = DeprecatedOption(key, msg, rkey, removal_ver)

0 commit comments

Comments
 (0)