Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get config item if variable is None #6862

Merged
merged 6 commits into from Dec 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 12 additions & 1 deletion dask/config.py
Expand Up @@ -423,10 +423,13 @@ def refresh(config=config, defaults=defaults, **kwargs):
update(config, collect(**kwargs))


def get(key, default=no_default, config=config):
def get(key, default=no_default, config=config, override_with=None):
"""
Get elements from global config

If ``override_with`` is not None this value will be passed straight back.
Useful for getting kwarg defaults from Dask config.

Use '.' for nested access

Examples
Expand All @@ -441,10 +444,18 @@ def get(key, default=no_default, config=config):
>>> config.get('foo.x.y', default=123) # doctest: +SKIP
123

>>> config.get('foo.y', override_with=None) # doctest: +SKIP
2

>>> config.get('foo.y', override_with=3) # doctest: +SKIP
3

See Also
--------
dask.config.set
"""
if override_with is not None:
return override_with
keys = key.split(".")
result = config
for k in keys:
Expand Down
17 changes: 17 additions & 0 deletions dask/tests/test_config.py
Expand Up @@ -472,3 +472,20 @@ def test_deprecations():
assert dask.config.get("optimization.fuse.ave-width") == 123

assert "optimization.fuse.ave-width" in str(info[0].message)


def test_get_override_with():
with dask.config.set({"foo": "bar"}):
# If override_with is None get the config key
assert dask.config.get("foo") == "bar"
assert dask.config.get("foo", override_with=None) == "bar"

# Otherwise pass the default straight through
assert dask.config.get("foo", override_with="baz") == "baz"
assert dask.config.get("foo", override_with=False) is False
assert dask.config.get("foo", override_with=True) is True
assert dask.config.get("foo", override_with=123) == 123
assert dask.config.get("foo", override_with={"hello": "world"}) == {
"hello": "world"
}
assert dask.config.get("foo", override_with=["one"]) == ["one"]