Skip to content

Commit 9530851

Browse files
authored
TST: Clean contexts (#57571)
* Remove use_numexpr * remove return_filelike in ensure_clean * Start using temp_file * Use more unique name?' * Fix failure * remove from all
1 parent 421f47d commit 9530851

File tree

11 files changed

+512
-516
lines changed

11 files changed

+512
-516
lines changed

doc/source/development/contributing_codebase.rst

+6-5
Original file line numberDiff line numberDiff line change
@@ -596,14 +596,15 @@ with the specific exception subclass (i.e. never use :py:class:`Exception`) and
596596
Testing involving files
597597
^^^^^^^^^^^^^^^^^^^^^^^
598598

599-
The ``tm.ensure_clean`` context manager creates a temporary file for testing,
600-
with a generated filename (or your filename if provided), that is automatically
601-
deleted when the context block is exited.
599+
The ``temp_file`` pytest fixture creates a temporary file :py:class:`Pathlib` object for testing:
602600

603601
.. code-block:: python
604602
605-
with tm.ensure_clean('my_file_path') as path:
606-
# do something with the path
603+
def test_something(temp_file):
604+
pd.DataFrame([1]).to_csv(str(temp_file))
605+
606+
Please reference `pytest's documentation <https://docs.pytest.org/en/latest/how-to/tmp_path.html#the-default-base-temporary-directory>`_
607+
for the file retension policy.
607608

608609
Testing involving network connectivity
609610
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

pandas/_testing/__init__.py

-2
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@
7676
ensure_clean,
7777
raises_chained_assignment_error,
7878
set_timezone,
79-
use_numexpr,
8079
with_csv_dialect,
8180
)
8281
from pandas.core.arrays import (
@@ -628,7 +627,6 @@ def shares_memory(left, right) -> bool:
628627
"to_array",
629628
"UNSIGNED_INT_EA_DTYPES",
630629
"UNSIGNED_INT_NUMPY_DTYPES",
631-
"use_numexpr",
632630
"with_csv_dialect",
633631
"write_to_compressed",
634632
]

pandas/_testing/contexts.py

+2-38
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616
from pandas.compat import PYPY
1717
from pandas.errors import ChainedAssignmentError
1818

19-
from pandas import set_option
20-
2119
from pandas.io.common import get_handle
2220

2321
if TYPE_CHECKING:
@@ -95,9 +93,7 @@ def setTZ(tz) -> None:
9593

9694

9795
@contextmanager
98-
def ensure_clean(
99-
filename=None, return_filelike: bool = False, **kwargs: Any
100-
) -> Generator[Any, None, None]:
96+
def ensure_clean(filename=None) -> Generator[Any, None, None]:
10197
"""
10298
Gets a temporary path and agrees to remove on close.
10399
@@ -109,12 +105,6 @@ def ensure_clean(
109105
----------
110106
filename : str (optional)
111107
suffix of the created file.
112-
return_filelike : bool (default False)
113-
if True, returns a file-like which is *always* cleaned. Necessary for
114-
savefig and other functions which want to append extensions.
115-
**kwargs
116-
Additional keywords are passed to open().
117-
118108
"""
119109
folder = Path(tempfile.gettempdir())
120110

@@ -125,19 +115,11 @@ def ensure_clean(
125115

126116
path.touch()
127117

128-
handle_or_str: str | IO = str(path)
129-
encoding = kwargs.pop("encoding", None)
130-
if return_filelike:
131-
kwargs.setdefault("mode", "w+b")
132-
if encoding is None and "b" not in kwargs["mode"]:
133-
encoding = "utf-8"
134-
handle_or_str = open(path, encoding=encoding, **kwargs)
118+
handle_or_str = str(path)
135119

136120
try:
137121
yield handle_or_str
138122
finally:
139-
if not isinstance(handle_or_str, str):
140-
handle_or_str.close()
141123
if path.is_file():
142124
path.unlink()
143125

@@ -176,24 +158,6 @@ def with_csv_dialect(name: str, **kwargs) -> Generator[None, None, None]:
176158
csv.unregister_dialect(name)
177159

178160

179-
@contextmanager
180-
def use_numexpr(use, min_elements=None) -> Generator[None, None, None]:
181-
from pandas.core.computation import expressions as expr
182-
183-
if min_elements is None:
184-
min_elements = expr._MIN_ELEMENTS
185-
186-
olduse = expr.USE_NUMEXPR
187-
oldmin = expr._MIN_ELEMENTS
188-
set_option("compute.use_numexpr", use)
189-
expr._MIN_ELEMENTS = min_elements
190-
try:
191-
yield
192-
finally:
193-
expr._MIN_ELEMENTS = oldmin
194-
set_option("compute.use_numexpr", olduse)
195-
196-
197161
def raises_chained_assignment_error(warn=True, extra_warnings=(), extra_match=()):
198162
from pandas._testing import assert_produces_warning
199163

pandas/conftest.py

+12
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
TYPE_CHECKING,
3636
Callable,
3737
)
38+
import uuid
3839

3940
from dateutil.tz import (
4041
tzlocal,
@@ -2020,3 +2021,14 @@ def arrow_string_storage():
20202021
Fixture that lists possible PyArrow values for StringDtype storage field.
20212022
"""
20222023
return ("pyarrow", "pyarrow_numpy")
2024+
2025+
2026+
@pytest.fixture
2027+
def temp_file(tmp_path):
2028+
"""
2029+
Generate a unique file for testing use. See link for removal policy.
2030+
https://docs.pytest.org/en/7.1.x/how-to/tmp_path.html#the-default-base-temporary-directory
2031+
"""
2032+
file_path = tmp_path / str(uuid.uuid4())
2033+
file_path.touch()
2034+
return file_path

0 commit comments

Comments
 (0)