Skip to content

Commit 9dbeb6b

Browse files
pkopparlamax-sixtydcherian
authored
Added get_options method (#5716)
* Added get_options method and squashed commits * get_options returns FrozenDict and added retention test * Updated docstring * Update doc/whats-new.rst Co-authored-by: Maximilian Roos <5635139+max-sixty@users.noreply.github.com> * Update doc/whats-new.rst Co-authored-by: Deepak Cherian <dcherian@users.noreply.github.com> * Changed docstring to refer to set_options * Added additional test parameter Co-authored-by: Maximilian Roos <5635139+max-sixty@users.noreply.github.com> Co-authored-by: Deepak Cherian <dcherian@users.noreply.github.com>
1 parent 96c5683 commit 9dbeb6b

File tree

5 files changed

+30
-1
lines changed

5 files changed

+30
-1
lines changed

doc/api.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ Top-level functions
3535
map_blocks
3636
show_versions
3737
set_options
38+
get_options
3839
unify_chunks
3940

4041
Dataset

doc/whats-new.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ v0.19.1 (unreleased)
2222

2323
New Features
2424
~~~~~~~~~~~~
25+
- Added a :py:func:`get_options` method to xarray's root namespace (:issue:`5698`, :pull:`5716`)
26+
By `Pushkar Kopparla <https://github.com/pkopparla>`_.
2527
- Xarray now does a better job rendering variable names that are long LaTeX sequences when plotting (:issue:`5681`, :pull:`5682`).
2628
By `Tomas Chor <https://github.com/tomchor>`_.
2729
- Add a option to disable the use of ``bottleneck`` (:pull:`5560`)

xarray/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
from .core.dataset import Dataset
2525
from .core.extensions import register_dataarray_accessor, register_dataset_accessor
2626
from .core.merge import Context, MergeError, merge
27-
from .core.options import set_options
27+
from .core.options import get_options, set_options
2828
from .core.parallel import map_blocks
2929
from .core.variable import Coordinate, IndexVariable, Variable, as_variable
3030
from .util.print_versions import show_versions
@@ -57,6 +57,7 @@
5757
"cov",
5858
"corr",
5959
"full_like",
60+
"get_options",
6061
"infer_freq",
6162
"load_dataarray",
6263
"load_dataset",

xarray/core/options.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import sys
22
import warnings
33

4+
from .utils import FrozenDict
5+
46
# TODO: Remove this check once python 3.7 is not supported:
57
if sys.version_info >= (3, 8):
68
from typing import TYPE_CHECKING, Literal, TypedDict, Union
@@ -269,3 +271,15 @@ def __enter__(self):
269271

270272
def __exit__(self, type, value, traceback):
271273
self._apply_update(self.old)
274+
275+
276+
def get_options():
277+
"""
278+
Get options for xarray.
279+
280+
See Also
281+
----------
282+
set_options
283+
284+
"""
285+
return FrozenDict(OPTIONS)

xarray/tests/test_options.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,3 +203,14 @@ def test_display_dataarray_style_html(self) -> None:
203203
html = da._repr_html_()
204204
assert html.startswith("<div>")
205205
assert "#x27;nested&#x27;" in html
206+
207+
208+
@pytest.mark.parametrize(
209+
"set_value",
210+
[("left"), ("exact")],
211+
)
212+
def test_get_options_retention(set_value):
213+
"""Test to check if get_options will return changes made by set_options"""
214+
with xarray.set_options(arithmetic_join=set_value):
215+
get_options = xarray.get_options()
216+
assert get_options["arithmetic_join"] == set_value

0 commit comments

Comments
 (0)