From 72c7fbfa3477759e65e97938105531a1a568063d Mon Sep 17 00:00:00 2001 From: Kevin Sheppard Date: Thu, 18 Aug 2022 16:39:03 +0100 Subject: [PATCH] CLN: Use ensure_clean from pandas Use ensure clean Switch to only 1.5 compatible typing --- pandas-stubs/io/stata.pyi | 18 ++--------- tests/test_io.py | 65 +-------------------------------------- 2 files changed, 3 insertions(+), 80 deletions(-) diff --git a/pandas-stubs/io/stata.pyi b/pandas-stubs/io/stata.pyi index 5bb90b47d..59cdcd897 100644 --- a/pandas-stubs/io/stata.pyi +++ b/pandas-stubs/io/stata.pyi @@ -27,6 +27,7 @@ from pandas._typing import ( @overload def read_stata( path: FilePath | ReadBuffer[bytes], + *, convert_dates: bool = ..., convert_categoricals: bool = ..., index_col: str | None = ..., @@ -35,22 +36,6 @@ def read_stata( columns: list[HashableT] | None = ..., order_categoricals: bool = ..., chunksize: int | None = ..., - *, - iterator: Literal[True], - compression: CompressionOptions = ..., - storage_options: StorageOptions = ..., -) -> StataReader: ... -@overload -def read_stata( - path: FilePath | ReadBuffer[bytes], - convert_dates: bool, - convert_categoricals: bool, - index_col: str | None, - convert_missing: bool, - preserve_dtypes: bool, - columns: list[HashableT] | None, - order_categoricals: bool, - chunksize: int | None, iterator: Literal[True], compression: CompressionOptions = ..., storage_options: StorageOptions = ..., @@ -58,6 +43,7 @@ def read_stata( @overload def read_stata( path: FilePath | ReadBuffer[bytes], + *, convert_dates: bool = ..., convert_categoricals: bool = ..., index_col: str | None = ..., diff --git a/tests/test_io.py b/tests/test_io.py index 1837c96dd..e169e4821 100644 --- a/tests/test_io.py +++ b/tests/test_io.py @@ -1,16 +1,8 @@ from __future__ import annotations -from contextlib import contextmanager -from pathlib import Path -import tempfile -from typing import ( - IO, - Any, -) -import uuid - import pandas as pd from pandas import DataFrame +from pandas._testing import ensure_clean from typing_extensions import assert_type from tests import check @@ -23,67 +15,12 @@ DF = DataFrame({"a": [1, 2, 3], "b": [0.0, 0.0, 0.0]}) -@contextmanager -def ensure_clean(filename=None, return_filelike: bool = False, **kwargs: Any): - """ - Gets a temporary path and agrees to remove on close. - This implementation does not use tempfile.mkstemp to avoid having a file handle. - If the code using the returned path wants to delete the file itself, windows - requires that no program has a file handle to it. - Parameters - ---------- - 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()) - - if filename is None: - filename = "" - filename = str(uuid.uuid4()) + filename - path = folder / filename - - path.touch() - - handle_or_str: str | IO = str(path) - if return_filelike: - kwargs.setdefault("mode", "w+b") - handle_or_str = open(path, **kwargs) - - try: - yield handle_or_str - finally: - if not isinstance(handle_or_str, str): - handle_or_str.close() - if path.is_file(): - path.unlink() - - def test_read_stata_df(): with ensure_clean() as path: DF.to_stata(path) check(assert_type(read_stata(path), pd.DataFrame), pd.DataFrame) -def test_read_stata_iterator_positional(): - with ensure_clean() as path: - str_path = str(path) - DF.to_stata(str_path) - check( - assert_type( - read_stata( - str_path, False, False, None, False, False, None, False, 2, True - ), - StataReader, - ), - StataReader, - ) - - def test_read_stata_iterator(): with ensure_clean() as path: str_path = str(path)