diff --git a/doc/api.rst b/doc/api.rst index fb2296d1226..87bc3b7a84e 100644 --- a/doc/api.rst +++ b/doc/api.rst @@ -35,6 +35,7 @@ Top-level functions map_blocks show_versions set_options + get_options unify_chunks Dataset diff --git a/doc/whats-new.rst b/doc/whats-new.rst index 4f79a37eb4b..0a4209836ed 100644 --- a/doc/whats-new.rst +++ b/doc/whats-new.rst @@ -22,6 +22,8 @@ v0.19.1 (unreleased) New Features ~~~~~~~~~~~~ +- Added a :py:func:`get_options` method to xarray's root namespace (:issue:`5698`, :pull:`5716`) + By `Pushkar Kopparla `_. - Xarray now does a better job rendering variable names that are long LaTeX sequences when plotting (:issue:`5681`, :pull:`5682`). By `Tomas Chor `_. - Add a option to disable the use of ``bottleneck`` (:pull:`5560`) diff --git a/xarray/__init__.py b/xarray/__init__.py index 8321aba4b46..eb35bbb2d18 100644 --- a/xarray/__init__.py +++ b/xarray/__init__.py @@ -24,7 +24,7 @@ from .core.dataset import Dataset from .core.extensions import register_dataarray_accessor, register_dataset_accessor from .core.merge import Context, MergeError, merge -from .core.options import set_options +from .core.options import get_options, set_options from .core.parallel import map_blocks from .core.variable import Coordinate, IndexVariable, Variable, as_variable from .util.print_versions import show_versions @@ -57,6 +57,7 @@ "cov", "corr", "full_like", + "get_options", "infer_freq", "load_dataarray", "load_dataset", diff --git a/xarray/core/options.py b/xarray/core/options.py index e22d8ed99d8..14f77306316 100644 --- a/xarray/core/options.py +++ b/xarray/core/options.py @@ -1,6 +1,8 @@ import sys import warnings +from .utils import FrozenDict + # TODO: Remove this check once python 3.7 is not supported: if sys.version_info >= (3, 8): from typing import TYPE_CHECKING, Literal, TypedDict, Union @@ -269,3 +271,15 @@ def __enter__(self): def __exit__(self, type, value, traceback): self._apply_update(self.old) + + +def get_options(): + """ + Get options for xarray. + + See Also + ---------- + set_options + + """ + return FrozenDict(OPTIONS) diff --git a/xarray/tests/test_options.py b/xarray/tests/test_options.py index 19f74476ced..af16d867401 100644 --- a/xarray/tests/test_options.py +++ b/xarray/tests/test_options.py @@ -203,3 +203,14 @@ def test_display_dataarray_style_html(self): html = da._repr_html_() assert html.startswith("
") assert "#x27;nested'" in html + + +@pytest.mark.parametrize( + "set_value", + [("left"), ("exact")], +) +def test_get_options_retention(set_value): + """Test to check if get_options will return changes made by set_options""" + with xarray.set_options(arithmetic_join=set_value): + get_options = xarray.get_options() + assert get_options["arithmetic_join"] == set_value