Skip to content

Commit b1ba308

Browse files
authored
CLN: Reset global config state in tests (#51506)
* CLN: Reset global config state in tests * Add fixture to keep current configs
1 parent 8d2a4e1 commit b1ba308

File tree

1 file changed

+55
-46
lines changed

1 file changed

+55
-46
lines changed

pandas/tests/io/formats/test_format.py

+55-46
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
import pytest
2323
import pytz
2424

25+
from pandas._config import config
26+
2527
from pandas.compat import (
2628
IS64,
2729
is_platform_windows,
@@ -40,7 +42,6 @@
4042
option_context,
4143
read_csv,
4244
reset_option,
43-
set_option,
4445
)
4546
import pandas._testing as tm
4647

@@ -57,6 +58,17 @@ def get_local_am_pm():
5758
return am_local, pm_local
5859

5960

61+
@pytest.fixture(autouse=True)
62+
def clean_config():
63+
curr_deprecated_options = config._deprecated_options.copy()
64+
curr_registered_options = config._registered_options.copy()
65+
curr_global_config = config._global_config.copy()
66+
yield
67+
config._deprecated_options = curr_deprecated_options
68+
config._registered_options = curr_registered_options
69+
config._global_config = curr_global_config
70+
71+
6072
@pytest.fixture(params=["string", "pathlike", "buffer"])
6173
def filepath_or_buffer_id(request):
6274
"""
@@ -234,12 +246,11 @@ def test_repr_truncation(self):
234246
def test_max_colwidth_negative_int_raises(self):
235247
# Deprecation enforced from:
236248
# https://github.com/pandas-dev/pandas/issues/31532
237-
width = get_option("display.max_colwidth")
238249
with pytest.raises(
239250
ValueError, match="Value must be a nonnegative integer or None"
240251
):
241-
set_option("display.max_colwidth", -1)
242-
set_option("display.max_colwidth", width)
252+
with option_context("display.max_colwidth", -1):
253+
pass
243254

244255
def test_repr_chop_threshold(self):
245256
df = DataFrame([[0.1, 0.5], [0.5, -0.1]])
@@ -1113,11 +1124,10 @@ def test_repr_corner(self):
11131124

11141125
def test_frame_info_encoding(self):
11151126
index = ["'Til There Was You (1997)", "ldum klaka (Cold Fever) (1994)"]
1116-
fmt.set_option("display.max_rows", 1)
1117-
df = DataFrame(columns=["a", "b", "c"], index=index)
1118-
repr(df)
1119-
repr(df.T)
1120-
fmt.set_option("display.max_rows", 200)
1127+
with option_context("display.max_rows", 1):
1128+
df = DataFrame(columns=["a", "b", "c"], index=index)
1129+
repr(df)
1130+
repr(df.T)
11211131

11221132
def test_wide_repr(self):
11231133
with option_context(
@@ -1421,40 +1431,39 @@ def test_to_string_line_width_no_index(self):
14211431

14221432
def test_to_string_float_formatting(self):
14231433
tm.reset_display_options()
1424-
fmt.set_option(
1434+
with option_context(
14251435
"display.precision",
14261436
5,
14271437
"display.notebook_repr_html",
14281438
False,
1429-
)
1430-
1431-
df = DataFrame(
1432-
{"x": [0, 0.25, 3456.000, 12e45, 1.64e6, 1.7e8, 1.253456, np.pi, -1e6]}
1433-
)
1439+
):
1440+
df = DataFrame(
1441+
{"x": [0, 0.25, 3456.000, 12e45, 1.64e6, 1.7e8, 1.253456, np.pi, -1e6]}
1442+
)
14341443

1435-
df_s = df.to_string()
1444+
df_s = df.to_string()
14361445

1437-
if _three_digit_exp():
1438-
expected = (
1439-
" x\n0 0.00000e+000\n1 2.50000e-001\n"
1440-
"2 3.45600e+003\n3 1.20000e+046\n4 1.64000e+006\n"
1441-
"5 1.70000e+008\n6 1.25346e+000\n7 3.14159e+000\n"
1442-
"8 -1.00000e+006"
1443-
)
1444-
else:
1445-
expected = (
1446-
" x\n0 0.00000e+00\n1 2.50000e-01\n"
1447-
"2 3.45600e+03\n3 1.20000e+46\n4 1.64000e+06\n"
1448-
"5 1.70000e+08\n6 1.25346e+00\n7 3.14159e+00\n"
1449-
"8 -1.00000e+06"
1450-
)
1451-
assert df_s == expected
1446+
if _three_digit_exp():
1447+
expected = (
1448+
" x\n0 0.00000e+000\n1 2.50000e-001\n"
1449+
"2 3.45600e+003\n3 1.20000e+046\n4 1.64000e+006\n"
1450+
"5 1.70000e+008\n6 1.25346e+000\n7 3.14159e+000\n"
1451+
"8 -1.00000e+006"
1452+
)
1453+
else:
1454+
expected = (
1455+
" x\n0 0.00000e+00\n1 2.50000e-01\n"
1456+
"2 3.45600e+03\n3 1.20000e+46\n4 1.64000e+06\n"
1457+
"5 1.70000e+08\n6 1.25346e+00\n7 3.14159e+00\n"
1458+
"8 -1.00000e+06"
1459+
)
1460+
assert df_s == expected
14521461

1453-
df = DataFrame({"x": [3234, 0.253]})
1454-
df_s = df.to_string()
1462+
df = DataFrame({"x": [3234, 0.253]})
1463+
df_s = df.to_string()
14551464

1456-
expected = " x\n0 3234.000\n1 0.253"
1457-
assert df_s == expected
1465+
expected = " x\n0 3234.000\n1 0.253"
1466+
assert df_s == expected
14581467

14591468
tm.reset_display_options()
14601469
assert get_option("display.precision") == 6
@@ -1746,19 +1755,19 @@ def test_repr_html(self, float_frame):
17461755
df = float_frame
17471756
df._repr_html_()
17481757

1749-
fmt.set_option("display.max_rows", 1, "display.max_columns", 1)
1750-
df._repr_html_()
1758+
with option_context("display.max_rows", 1, "display.max_columns", 1):
1759+
df._repr_html_()
17511760

1752-
fmt.set_option("display.notebook_repr_html", False)
1753-
df._repr_html_()
1761+
with option_context("display.notebook_repr_html", False):
1762+
df._repr_html_()
17541763

17551764
tm.reset_display_options()
17561765

17571766
df = DataFrame([[1, 2], [3, 4]])
1758-
fmt.set_option("display.show_dimensions", True)
1759-
assert "2 rows" in df._repr_html_()
1760-
fmt.set_option("display.show_dimensions", False)
1761-
assert "2 rows" not in df._repr_html_()
1767+
with option_context("display.show_dimensions", True):
1768+
assert "2 rows" in df._repr_html_()
1769+
with option_context("display.show_dimensions", False):
1770+
assert "2 rows" not in df._repr_html_()
17621771

17631772
tm.reset_display_options()
17641773

@@ -1922,7 +1931,7 @@ def test_info_repr_max_cols(self):
19221931
assert not has_non_verbose_info_repr(df)
19231932

19241933
# test verbose overrides
1925-
# fmt.set_option('display.max_info_columns', 4) # exceeded
1934+
# set_option('display.max_info_columns', 4) # exceeded
19261935

19271936
def test_info_repr_html(self):
19281937
max_rows = 60
@@ -1952,8 +1961,8 @@ def get_ipython():
19521961
repstr = df._repr_html_()
19531962
assert repstr is not None
19541963

1955-
fmt.set_option("display.max_rows", 5, "display.max_columns", 2)
1956-
repstr = df._repr_html_()
1964+
with option_context("display.max_rows", 5, "display.max_columns", 2):
1965+
repstr = df._repr_html_()
19571966

19581967
assert "class" in repstr # info fallback
19591968
tm.reset_display_options()

0 commit comments

Comments
 (0)