Skip to content

Commit 97de0f3

Browse files
committed
Review _array_expr_enabled()
1 parent 8d2cc26 commit 97de0f3

2 files changed

Lines changed: 50 additions & 14 deletions

File tree

dask/array/__init__.py

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,29 @@ def _array_expr_enabled() -> builtins.bool:
1313

1414
global ARRAY_EXPR_ENABLED
1515

16-
use_array_expr = dask.config.get("array.query-planning")
16+
use_array_expr: builtins.bool | None = dask.config.get("array.query-planning")
17+
if use_array_expr is None:
18+
use_array_expr = False # Eventually, this default will flip to True
19+
if not isinstance(use_array_expr, builtins.bool):
20+
# Guard against "false" string in YAML config, which would evaluate to True
21+
raise TypeError( # pragma: no cover
22+
"The 'array.query-planning' config must be True, False, or None"
23+
)
1724

18-
if ARRAY_EXPR_ENABLED is not None:
19-
if (use_array_expr is True and ARRAY_EXPR_ENABLED is False) or (
20-
use_array_expr is False and ARRAY_EXPR_ENABLED is True
21-
):
22-
warnings.warn(
23-
"The 'array.query-planning' config is now set to "
24-
f"{use_array_expr}, but query planning is already "
25-
f"{'enabled' if ARRAY_EXPR_ENABLED else 'disabled'}. "
26-
"The query-planning config can only be changed before "
27-
"`dask.array` is first imported!"
28-
)
29-
return ARRAY_EXPR_ENABLED
25+
if ARRAY_EXPR_ENABLED is None:
26+
ARRAY_EXPR_ENABLED = use_array_expr
27+
return use_array_expr
3028

31-
return builtins.bool(use_array_expr if use_array_expr is not None else False)
29+
if use_array_expr != ARRAY_EXPR_ENABLED:
30+
warnings.warn(
31+
"The 'array.query-planning' config is now set to "
32+
f"{use_array_expr}, but query planning is already "
33+
f"{'enabled' if ARRAY_EXPR_ENABLED else 'disabled'}. "
34+
"The query-planning config can only be changed before "
35+
"`dask.array` is first imported!",
36+
RuntimeWarning,
37+
)
38+
return ARRAY_EXPR_ENABLED
3239

3340

3441
def array_expr_enabled() -> builtins.bool:
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import pytest
2+
3+
import dask.config
4+
from dask.array import _array_expr_enabled, array_expr_enabled
5+
6+
pytestmark = pytest.mark.normal_and_array_expr
7+
8+
9+
def test_array_expr_enabled():
10+
assert _array_expr_enabled() in (True, False)
11+
assert array_expr_enabled() is _array_expr_enabled()
12+
13+
14+
def test_flip_config():
15+
"""Config changes after the initial import are ignored with a warning"""
16+
prev = _array_expr_enabled()
17+
with dask.config.set({"array.query-planning": not prev}):
18+
with pytest.warns(RuntimeWarning, match="first imported"):
19+
actual = _array_expr_enabled()
20+
assert actual == prev
21+
22+
23+
def test_str_config():
24+
"""Prevent accidental use of 'false' string in YAML config,
25+
which would evaluate to True.
26+
"""
27+
with dask.config.set({"array.query-planning": "false"}):
28+
with pytest.raises(TypeError):
29+
_array_expr_enabled()

0 commit comments

Comments
 (0)