Skip to content

Commit

Permalink
CLN: Use ensure_clean from pandas
Browse files Browse the repository at this point in the history
Use ensure clean
Switch to only 1.5 compatible typing
  • Loading branch information
bashtage committed Aug 18, 2022
1 parent 620f297 commit 72c7fbf
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 80 deletions.
18 changes: 2 additions & 16 deletions pandas-stubs/io/stata.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ...,
Expand All @@ -35,29 +36,14 @@ 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 = ...,
) -> StataReader: ...
@overload
def read_stata(
path: FilePath | ReadBuffer[bytes],
*,
convert_dates: bool = ...,
convert_categoricals: bool = ...,
index_col: str | None = ...,
Expand Down
65 changes: 1 addition & 64 deletions tests/test_io.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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)
Expand Down

0 comments on commit 72c7fbf

Please sign in to comment.