Skip to content

Commit

Permalink
Raise OptionError instead of KeyError in __getattr__. Fixes pandas-de…
Browse files Browse the repository at this point in the history
  • Loading branch information
jayfoad authored and harisbal committed Feb 28, 2018
1 parent f001b70 commit 5d0f3d5
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 1 deletion.
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.23.0.txt
Expand Up @@ -909,3 +909,4 @@ Other
^^^^^

- Improved error message when attempting to use a Python keyword as an identifier in a ``numexpr`` backed query (:issue:`18221`)
- Bug in accessing a :func:`pandas.get_option`, which raised ``KeyError`` rather than ``OptionError`` when looking up a non-existant option key in some cases (:issue:`19789`)
5 changes: 4 additions & 1 deletion pandas/core/config.py
Expand Up @@ -196,7 +196,10 @@ def __getattr__(self, key):
if prefix:
prefix += "."
prefix += key
v = object.__getattribute__(self, "d")[key]
try:
v = object.__getattribute__(self, "d")[key]
except KeyError:
raise OptionError("No such option")
if isinstance(v, dict):
return DictWrapper(v, prefix)
else:
Expand Down
6 changes: 6 additions & 0 deletions pandas/tests/test_config.py
Expand Up @@ -428,3 +428,9 @@ def test_option_context_scope(self):

# Ensure the current context is reset
assert self.cf.get_option(option_name) == original_value

def test_dictwrapper_getattr(self):
options = self.cf.options
# GH 19789
pytest.raises(self.cf.OptionError, getattr, options, 'bananas')
assert not hasattr(options, 'bananas')

0 comments on commit 5d0f3d5

Please sign in to comment.