From f8df7e75ded8202f5cf8ca3e3b770c4c48352a22 Mon Sep 17 00:00:00 2001 From: Stephan Hoyer Date: Fri, 28 Oct 2016 10:58:48 -0700 Subject: [PATCH] Better error checking and tests for xarray.set_options --- xarray/core/options.py | 6 +++++- xarray/test/test_options.py | 19 +++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) create mode 100644 xarray/test/test_options.py diff --git a/xarray/core/options.py b/xarray/core/options.py index 0594a1ce36d..0a3493d63ed 100644 --- a/xarray/core/options.py +++ b/xarray/core/options.py @@ -2,7 +2,7 @@ class set_options(object): - """Set global state within a controlled context + """Set options for xarray in a controlled context. Currently, the only supported option is ``display_width``, which has a default value of 80. @@ -24,6 +24,10 @@ class set_options(object): >>> xr.set_options(display_width=80) """ def __init__(self, **kwargs): + invalid_options = {k for k in kwargs if k not in OPTIONS} + if invalid_options: + raise ValueError('argument names %r are not in the set of valid ' + 'options %r' % (invalid_options, set(OPTIONS))) self.old = OPTIONS.copy() OPTIONS.update(kwargs) diff --git a/xarray/test/test_options.py b/xarray/test/test_options.py new file mode 100644 index 00000000000..8eb9f541db7 --- /dev/null +++ b/xarray/test/test_options.py @@ -0,0 +1,19 @@ +import xarray +import pytest + +from xarray.core.options import OPTIONS + + +def test_invalid_option_raises(): + with pytest.raises(ValueError): + xarray.set_options(not_a_valid_options=True) + + +def test_nested_options(): + original = OPTIONS['display_width'] + with xarray.set_options(display_width=1): + assert OPTIONS['display_width'] == 1 + with xarray.set_options(display_width=2): + assert OPTIONS['display_width'] == 2 + assert OPTIONS['display_width'] == 1 + assert OPTIONS['display_width'] == original