Skip to content

Commit a247d93

Browse files
authored
Better error checking and tests for xarray.set_options (pydata#1066)
1 parent 90c854f commit a247d93

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

xarray/core/options.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33

44
class set_options(object):
5-
"""Set global state within a controlled context
5+
"""Set options for xarray in a controlled context.
66
77
Currently, the only supported option is ``display_width``, which has a
88
default value of 80.
@@ -24,6 +24,10 @@ class set_options(object):
2424
>>> xr.set_options(display_width=80)
2525
"""
2626
def __init__(self, **kwargs):
27+
invalid_options = {k for k in kwargs if k not in OPTIONS}
28+
if invalid_options:
29+
raise ValueError('argument names %r are not in the set of valid '
30+
'options %r' % (invalid_options, set(OPTIONS)))
2731
self.old = OPTIONS.copy()
2832
OPTIONS.update(kwargs)
2933

xarray/test/test_options.py

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import xarray
2+
import pytest
3+
4+
from xarray.core.options import OPTIONS
5+
6+
7+
def test_invalid_option_raises():
8+
with pytest.raises(ValueError):
9+
xarray.set_options(not_a_valid_options=True)
10+
11+
12+
def test_nested_options():
13+
original = OPTIONS['display_width']
14+
with xarray.set_options(display_width=1):
15+
assert OPTIONS['display_width'] == 1
16+
with xarray.set_options(display_width=2):
17+
assert OPTIONS['display_width'] == 2
18+
assert OPTIONS['display_width'] == 1
19+
assert OPTIONS['display_width'] == original

0 commit comments

Comments
 (0)