Skip to content

Commit 4f15905

Browse files
committed
refactor(config): remove option_context
BREAKING CHANGE: `option_context` is removed. Use `contextlib.contextmanager` to create your own version of this functionality if necessary.
1 parent 9f57999 commit 4f15905

File tree

5 files changed

+124
-123
lines changed

5 files changed

+124
-123
lines changed

ibis/backends/clickhouse/tests/test_client.py

Lines changed: 45 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
import ibis.common.exceptions as exc
1414
import ibis.expr.datatypes as dt
1515
import ibis.expr.types as ir
16-
from ibis import config, udf
16+
from ibis import udf
1717
from ibis.backends.clickhouse.tests.conftest import (
1818
CLICKHOUSE_HOST,
1919
CLICKHOUSE_PASS,
@@ -72,21 +72,19 @@ def test_limit_overrides_expr(con, alltypes):
7272
assert len(result) == 5
7373

7474

75-
def test_limit_equals_none_no_limit(alltypes):
76-
with config.option_context("sql.default_limit", 10):
77-
result = alltypes.execute(limit=None)
78-
assert len(result) > 10
75+
def test_limit_equals_none_no_limit(alltypes, monkeypatch):
76+
monkeypatch.setattr(ibis.options.sql, "default_limit", 10)
77+
result = alltypes.execute(limit=None)
78+
assert len(result) > 10
7979

8080

81-
def test_verbose_log_queries(con):
81+
def test_verbose_log_queries(con, monkeypatch):
8282
queries = []
8383

84-
def logger(x):
85-
queries.append(x)
84+
monkeypatch.setattr(ibis.options, "verbose", True)
85+
monkeypatch.setattr(ibis.options, "verbose_log", queries.append)
8686

87-
with config.option_context("verbose", True):
88-
with config.option_context("verbose_log", logger):
89-
con.table("functional_alltypes")
87+
con.table("functional_alltypes")
9088

9189
expected = "DESCRIBE functional_alltypes"
9290

@@ -95,36 +93,43 @@ def logger(x):
9593
assert expected in queries
9694

9795

98-
def test_sql_query_limits(alltypes):
99-
table = alltypes
100-
with config.option_context("sql.default_limit", 100000):
101-
# table has 25 rows
102-
assert len(table.execute()) == 7300
103-
# comply with limit arg for Table
104-
assert len(table.execute(limit=10)) == 10
105-
# state hasn't changed
106-
assert len(table.execute()) == 7300
107-
# non-Table ignores default_limit
108-
assert table.count().execute() == 7300
109-
# non-Table doesn't observe limit arg
110-
assert table.count().execute(limit=10) == 7300
111-
with config.option_context("sql.default_limit", 20):
112-
# Table observes default limit setting
113-
assert len(table.execute()) == 20
114-
# explicit limit= overrides default
115-
assert len(table.execute(limit=15)) == 15
116-
assert len(table.execute(limit=23)) == 23
117-
# non-Table ignores default_limit
118-
assert table.count().execute() == 7300
119-
# non-Table doesn't observe limit arg
120-
assert table.count().execute(limit=10) == 7300
96+
def test_sql_query_limits_big(alltypes, monkeypatch):
97+
monkeypatch.setattr(ibis.options.sql, "default_limit", 100_000)
98+
99+
# alltypes has 7300 rows
100+
assert len(alltypes.execute()) == 7300 # comply with limit arg for alltypes
101+
assert len(alltypes.execute(limit=10)) == 10
102+
# state hasn't changed
103+
assert len(alltypes.execute()) == 7300
104+
# non-alltypes ignores default_limit
105+
assert alltypes.count().execute() == 7300
106+
# non-alltypes doesn't observe limit arg
107+
assert alltypes.count().execute(limit=10) == 7300
108+
109+
110+
def test_sql_query_limits_small(alltypes, monkeypatch):
111+
monkeypatch.setattr(ibis.options.sql, "default_limit", 20)
112+
113+
# alltypes observes default limit setting
114+
assert len(alltypes.execute()) == 20
115+
# explicit limit= overrides default
116+
assert len(alltypes.execute(limit=15)) == 15
117+
assert len(alltypes.execute(limit=23)) == 23
118+
# non-alltypes ignores default_limit
119+
assert alltypes.count().execute() == 7300
120+
# non-alltypes doesn't observe limit arg
121+
assert alltypes.count().execute(limit=10) == 7300
122+
123+
124+
def test_sql_query_limits_none(alltypes, monkeypatch):
125+
monkeypatch.setattr(ibis.options.sql, "default_limit", None)
126+
121127
# eliminating default_limit doesn't break anything
122-
with config.option_context("sql.default_limit", None):
123-
assert len(table.execute()) == 7300
124-
assert len(table.execute(limit=15)) == 15
125-
assert len(table.execute(limit=10000)) == 7300
126-
assert table.count().execute() == 7300
127-
assert table.count().execute(limit=10) == 7300
128+
assert len(alltypes.execute()) == 7300
129+
assert len(alltypes.execute(limit=15)) == 15
130+
assert len(alltypes.execute(limit=10000)) == 7300
131+
assert alltypes.count().execute() == 7300
132+
assert alltypes.count().execute(limit=10) == 7300
128133

129134

130135
def test_embedded_identifier_quoting(alltypes):

ibis/backends/impala/tests/test_client.py

Lines changed: 58 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
import ibis
1111
import ibis.expr.datatypes as dt
1212
import ibis.expr.types as ir
13-
from ibis import config
1413
from ibis.tests.util import assert_equal
1514

1615
pytest.importorskip("impala")
@@ -66,16 +65,17 @@ def test_result_as_dataframe(con, alltypes):
6665
assert len(result) == 10
6766

6867

69-
def test_adapt_scalar_array_results(con, alltypes):
68+
def test_adapt_scalar_array_results(con, alltypes, monkeypatch):
7069
table = alltypes
7170

7271
expr = table.double_col.sum()
7372
result = con.execute(expr)
7473
assert isinstance(result, float)
7574

76-
with config.option_context("interactive", True):
77-
result2 = expr.execute()
78-
assert isinstance(result2, float)
75+
monkeypatch.setattr(ibis.options, "interactive", True)
76+
77+
result2 = expr.execute()
78+
assert isinstance(result2, float)
7979

8080
expr = (
8181
table.group_by("string_col").aggregate([table.count().name("count")]).string_col
@@ -85,7 +85,7 @@ def test_adapt_scalar_array_results(con, alltypes):
8585
assert isinstance(result, pd.Series)
8686

8787

88-
def test_interactive_repr_call_failure(con):
88+
def test_interactive_repr_call_failure(con, monkeypatch):
8989
t = con.table("lineitem").limit(100000)
9090

9191
t = t.select(t, t.l_receiptdate.cast("timestamp").name("date"))
@@ -100,8 +100,9 @@ def test_interactive_repr_call_failure(con):
100100

101101
metric = expr["avg_px"].mean().over(w2)
102102
enriched = expr.select(expr, metric)
103-
with config.option_context("interactive", True):
104-
repr(enriched)
103+
104+
monkeypatch.setattr(ibis.options, "interactive", True)
105+
repr(enriched)
105106

106107

107108
def test_array_default_limit(con, alltypes):
@@ -118,20 +119,22 @@ def test_limit_overrides_expr(con, alltypes):
118119
assert len(result) == 5
119120

120121

121-
def test_limit_equals_none_no_limit(alltypes):
122+
def test_limit_equals_none_no_limit(alltypes, monkeypatch):
122123
t = alltypes
123124

124-
with config.option_context("sql.default_limit", 10):
125-
result = t.execute(limit=None)
126-
assert len(result) > 10
125+
monkeypatch.setattr(ibis.options.sql, "default_limit", 10)
127126

127+
result = t.execute(limit=None)
128+
assert len(result) > 10
128129

129-
def test_verbose_log_queries(con, test_data_db):
130+
131+
def test_verbose_log_queries(con, test_data_db, monkeypatch):
130132
queries = []
131133

132-
with config.option_context("verbose", True):
133-
with config.option_context("verbose_log", queries.append):
134-
con.table("orders", database=test_data_db)
134+
monkeypatch.setattr(ibis.options, "verbose", True)
135+
monkeypatch.setattr(ibis.options, "verbose_log", queries.append)
136+
137+
con.table("orders", database=test_data_db)
135138

136139
# we can't make assertions about the length of queries, since the Python GC
137140
# could've collected a temporary pandas table any time between construction
@@ -140,36 +143,47 @@ def test_verbose_log_queries(con, test_data_db):
140143
assert expected in queries
141144

142145

143-
def test_sql_query_limits(con, test_data_db):
146+
def test_sql_query_limits_big(con, test_data_db, monkeypatch):
144147
table = con.table("nation", database=test_data_db)
145-
with config.option_context("sql.default_limit", 100000):
146-
# table has 25 rows
147-
assert len(table.execute()) == 25
148-
# comply with limit arg for Table
149-
assert len(table.execute(limit=10)) == 10
150-
# state hasn't changed
151-
assert len(table.execute()) == 25
152-
# non-Table ignores default_limit
153-
assert table.count().execute() == 25
154-
# non-Table doesn't observe limit arg
155-
assert table.count().execute(limit=10) == 25
156-
with config.option_context("sql.default_limit", 20):
157-
# Table observes default limit setting
158-
assert len(table.execute()) == 20
159-
# explicit limit= overrides default
160-
assert len(table.execute(limit=15)) == 15
161-
assert len(table.execute(limit=23)) == 23
162-
# non-Table ignores default_limit
163-
assert table.count().execute() == 25
164-
# non-Table doesn't observe limit arg
165-
assert table.count().execute(limit=10) == 25
148+
monkeypatch.setattr(ibis.options.sql, "default_limit", 100_000)
149+
150+
# table has 25 rows
151+
assert len(table.execute()) == 25
152+
# comply with limit arg for Table
153+
assert len(table.execute(limit=10)) == 10
154+
# state hasn't changed
155+
assert len(table.execute()) == 25
156+
# non-Table ignores default_limit
157+
assert table.count().execute() == 25
158+
# non-Table doesn't observe limit arg
159+
assert table.count().execute(limit=10) == 25
160+
161+
162+
def test_sql_query_limits_small(con, test_data_db, monkeypatch):
163+
table = con.table("nation", database=test_data_db)
164+
monkeypatch.setattr(ibis.options.sql, "default_limit", 20)
165+
166+
# Table observes default limit setting
167+
assert len(table.execute()) == 20
168+
# explicit limit= overrides default
169+
assert len(table.execute(limit=15)) == 15
170+
assert len(table.execute(limit=23)) == 23
171+
# non-Table ignores default_limit
172+
assert table.count().execute() == 25
173+
# non-Table doesn't observe limit arg
174+
assert table.count().execute(limit=10) == 25
175+
176+
177+
def test_sql_query_limits_none(con, test_data_db, monkeypatch):
178+
table = con.table("nation", database=test_data_db)
179+
monkeypatch.setattr(ibis.options.sql, "default_limit", None)
180+
166181
# eliminating default_limit doesn't break anything
167-
with config.option_context("sql.default_limit", None):
168-
assert len(table.execute()) == 25
169-
assert len(table.execute(limit=15)) == 15
170-
assert len(table.execute(limit=10000)) == 25
171-
assert table.count().execute() == 25
172-
assert table.count().execute(limit=10) == 25
182+
assert len(table.execute()) == 25
183+
assert len(table.execute(limit=15)) == 15
184+
assert len(table.execute(limit=10000)) == 25
185+
assert table.count().execute() == 25
186+
assert table.count().execute(limit=10) == 25
173187

174188

175189
def test_set_compression_codec(con):

ibis/backends/postgres/tests/test_functions.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
import ibis
1515
import ibis.expr.datatypes as dt
1616
import ibis.expr.types as ir
17-
from ibis import config
1817
from ibis import literal as L
1918

2019
pytest.importorskip("psycopg2")
@@ -656,11 +655,12 @@ def test_not_exists(alltypes, df):
656655
tm.assert_frame_equal(result, expected, check_index_type=False, check_dtype=False)
657656

658657

659-
def test_interactive_repr_shows_error(alltypes):
658+
def test_interactive_repr_shows_error(alltypes, monkeypatch):
660659
expr = alltypes.int_col.convert_base(10, 2)
661660

662-
with config.option_context("interactive", True):
663-
result = repr(expr)
661+
monkeypatch.setattr(ibis.options, "interactive", True)
662+
663+
result = repr(expr)
664664

665665
assert "OperationNotDefinedError" in result
666666
assert "BaseConvert" in result

ibis/backends/tests/test_interactive.py

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,12 @@
1313
# limitations under the License.
1414
from __future__ import annotations
1515

16+
import shutil
17+
1618
import pytest
1719

1820
import ibis
1921
import ibis.common.exceptions as exc
20-
from ibis import config
2122

2223

2324
@pytest.fixture
@@ -40,19 +41,19 @@ def test_interactive_execute_on_repr(table, queries):
4041
assert len(queries) >= 1
4142

4243

43-
def test_repr_png_is_none_in_interactive(table):
44-
with config.option_context("interactive", True):
45-
assert table._repr_png_() is None
44+
def test_repr_png_is_none_in_interactive(table, monkeypatch):
45+
monkeypatch.setattr(ibis.options, "interactive", True)
46+
assert table._repr_png_() is None
4647

4748

48-
def test_repr_png_is_not_none_in_not_interactive(table):
49+
def test_repr_png_is_not_none_in_not_interactive(table, monkeypatch):
4950
pytest.importorskip("ibis.expr.visualize")
5051

51-
with (
52-
config.option_context("interactive", False),
53-
config.option_context("graphviz_repr", True),
54-
):
55-
assert table._repr_png_() is not None
52+
monkeypatch.setattr(ibis.options, "interactive", False)
53+
monkeypatch.setattr(ibis.options, "graphviz_repr", True)
54+
55+
assert shutil.which("dot") is not None
56+
assert table._repr_png_() is not None
5657

5758

5859
@pytest.mark.notimpl(["polars"])
@@ -70,12 +71,14 @@ def test_respect_set_limit(table, queries):
7071

7172

7273
@pytest.mark.notimpl(["polars"])
73-
def test_disable_query_limit(table, queries):
74+
def test_disable_query_limit(table, queries, monkeypatch):
7475
assert ibis.options.sql.default_limit is None
7576

76-
with config.option_context("sql.default_limit", 10):
77-
assert ibis.options.sql.default_limit == 10
78-
repr(table.select("id", "bool_col"))
77+
monkeypatch.setattr(ibis.options.sql, "default_limit", 10)
78+
79+
assert ibis.options.sql.default_limit == 10
80+
81+
repr(table.select("id", "bool_col"))
7982

8083
assert len(queries) >= 1
8184

ibis/config.py

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
from __future__ import annotations
22

3-
import contextlib
43
from collections.abc import Callable # noqa: TCH003
54
from typing import Annotated, Any, Optional
65

@@ -27,21 +26,6 @@ def set(self, key: str, value: Any) -> None:
2726
conf = getattr(conf, field)
2827
setattr(conf, key, value)
2928

30-
@contextlib.contextmanager
31-
def _with_temporary(self, options):
32-
try:
33-
old = {}
34-
for key, value in options.items():
35-
old[key] = self.get(key)
36-
self.set(key, value)
37-
yield
38-
finally:
39-
for key, value in old.items():
40-
self.set(key, value)
41-
42-
def __call__(self, options):
43-
return self._with_temporary(options)
44-
4529

4630
class SQL(Config):
4731
"""SQL-related options.
@@ -207,9 +191,4 @@ def _default_backend() -> Any:
207191
options = Options()
208192

209193

210-
@public
211-
def option_context(key, new_value):
212-
return options({key: new_value})
213-
214-
215194
public(options=options)

0 commit comments

Comments
 (0)