Skip to content

TST: Clean contexts #57571

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

Merged
merged 7 commits into from
Feb 24, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions doc/source/development/contributing_codebase.rst
Original file line number Diff line number Diff line change
Expand Up @@ -596,14 +596,15 @@ with the specific exception subclass (i.e. never use :py:class:`Exception`) and
Testing involving files
^^^^^^^^^^^^^^^^^^^^^^^

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

.. code-block:: python

with tm.ensure_clean('my_file_path') as path:
# do something with the path
def test_something(temp_file):
pd.DataFrame([1]).to_csv(str(temp_file))

Please reference `pytest's documentation <https://docs.pytest.org/en/latest/how-to/tmp_path.html#the-default-base-temporary-directory>`_
for the file retension policy.

Testing involving network connectivity
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand Down
2 changes: 0 additions & 2 deletions pandas/_testing/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@
ensure_clean,
raises_chained_assignment_error,
set_timezone,
use_numexpr,
with_csv_dialect,
)
from pandas.core.arrays import (
Expand Down Expand Up @@ -628,7 +627,6 @@ def shares_memory(left, right) -> bool:
"to_array",
"UNSIGNED_INT_EA_DTYPES",
"UNSIGNED_INT_NUMPY_DTYPES",
"use_numexpr",
"with_csv_dialect",
"write_to_compressed",
]
40 changes: 2 additions & 38 deletions pandas/_testing/contexts.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
from pandas.compat import PYPY
from pandas.errors import ChainedAssignmentError

from pandas import set_option

from pandas.io.common import get_handle

if TYPE_CHECKING:
Expand Down Expand Up @@ -95,9 +93,7 @@ def setTZ(tz) -> None:


@contextmanager
def ensure_clean(
filename=None, return_filelike: bool = False, **kwargs: Any
) -> Generator[Any, None, None]:
def ensure_clean(filename=None) -> Generator[Any, None, None]:
"""
Gets a temporary path and agrees to remove on close.

Expand All @@ -109,12 +105,6 @@ def ensure_clean(
----------
filename : str (optional)
suffix of the created file.
return_filelike : bool (default False)
if True, returns a file-like which is *always* cleaned. Necessary for
savefig and other functions which want to append extensions.
**kwargs
Additional keywords are passed to open().

"""
folder = Path(tempfile.gettempdir())

Expand All @@ -125,19 +115,11 @@ def ensure_clean(

path.touch()

handle_or_str: str | IO = str(path)
encoding = kwargs.pop("encoding", None)
if return_filelike:
kwargs.setdefault("mode", "w+b")
if encoding is None and "b" not in kwargs["mode"]:
encoding = "utf-8"
handle_or_str = open(path, encoding=encoding, **kwargs)
handle_or_str = str(path)

try:
yield handle_or_str
finally:
if not isinstance(handle_or_str, str):
handle_or_str.close()
if path.is_file():
path.unlink()

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


@contextmanager
def use_numexpr(use, min_elements=None) -> Generator[None, None, None]:
from pandas.core.computation import expressions as expr

if min_elements is None:
min_elements = expr._MIN_ELEMENTS

olduse = expr.USE_NUMEXPR
oldmin = expr._MIN_ELEMENTS
set_option("compute.use_numexpr", use)
expr._MIN_ELEMENTS = min_elements
try:
yield
finally:
expr._MIN_ELEMENTS = oldmin
set_option("compute.use_numexpr", olduse)


def raises_chained_assignment_error(warn=True, extra_warnings=(), extra_match=()):
from pandas._testing import assert_produces_warning

Expand Down
12 changes: 12 additions & 0 deletions pandas/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
TYPE_CHECKING,
Callable,
)
import uuid

from dateutil.tz import (
tzlocal,
Expand Down Expand Up @@ -2010,3 +2011,14 @@ def arrow_string_storage():
Fixture that lists possible PyArrow values for StringDtype storage field.
"""
return ("pyarrow", "pyarrow_numpy")


@pytest.fixture
def temp_file(tmp_path):
"""
Generate a unique file for testing use. See link for removal policy.
https://docs.pytest.org/en/7.1.x/how-to/tmp_path.html#the-default-base-temporary-directory
"""
file_path = tmp_path / str(uuid.uuid4())
file_path.touch()
return file_path
Loading