From 00335ae721b71e1b45e3e53bd119f4bb21d1e680 Mon Sep 17 00:00:00 2001 From: Maxim Ivanov Date: Wed, 2 Dec 2020 22:04:53 +0700 Subject: [PATCH 1/4] TYP: handle mypy errors in compress_method --- pandas/_testing.py | 30 ++++++++++++++---------------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/pandas/_testing.py b/pandas/_testing.py index bfff4301c2220..6438cbf8570e4 100644 --- a/pandas/_testing.py +++ b/pandas/_testing.py @@ -299,22 +299,7 @@ def write_to_compressed(compression, path, data, dest="test"): ------ ValueError : An invalid compression value was passed in. """ - if compression == "zip": - compress_method = zipfile.ZipFile - elif compression == "gzip": - # pandas\_testing.py:288: error: Incompatible types in assignment - # (expression has type "Type[GzipFile]", variable has type - # "Type[ZipFile]") - compress_method = gzip.GzipFile # type: ignore[assignment] - elif compression == "bz2": - # pandas\_testing.py:290: error: Incompatible types in assignment - # (expression has type "Type[BZ2File]", variable has type - # "Type[ZipFile]") - compress_method = bz2.BZ2File # type: ignore[assignment] - elif compression == "xz": - compress_method = get_lzma_file(lzma) - else: - raise ValueError(f"Unrecognized compression type: {compression}") + compress_method = _select_compress_method(compression) if compression == "zip": mode = "w" @@ -332,6 +317,19 @@ def write_to_compressed(compression, path, data, dest="test"): getattr(f, method)(*args) +def _select_compress_method(compression: str) -> Callable: + if compression == "zip": + return zipfile.ZipFile + elif compression == "gzip": + return gzip.GzipFile + elif compression == "bz2": + return bz2.BZ2File + elif compression == "xz": + return get_lzma_file(lzma) + else: + raise ValueError(f"Unrecognized compression type: {compression}") + + def _get_tol_from_less_precise(check_less_precise: Union[bool, int]) -> float: """ Return the tolerance equivalent to the deprecated `check_less_precise` From 81924735e18f233c74335ab20366686481c4171a Mon Sep 17 00:00:00 2001 From: Maxim Ivanov Date: Wed, 2 Dec 2020 22:09:35 +0700 Subject: [PATCH 2/4] TYP: handle mypy error in args --- pandas/_testing.py | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/pandas/_testing.py b/pandas/_testing.py index 6438cbf8570e4..2e8eb20921734 100644 --- a/pandas/_testing.py +++ b/pandas/_testing.py @@ -10,7 +10,17 @@ from shutil import rmtree import string import tempfile -from typing import Any, Callable, ContextManager, List, Optional, Type, Union, cast +from typing import ( + Any, + Callable, + ContextManager, + List, + Optional, + Tuple, + Type, + Union, + cast, +) import warnings import zipfile @@ -301,16 +311,14 @@ def write_to_compressed(compression, path, data, dest="test"): """ compress_method = _select_compress_method(compression) + args: Tuple[Any, ...] if compression == "zip": mode = "w" args = (dest, data) method = "writestr" else: mode = "wb" - # pandas\_testing.py:302: error: Incompatible types in assignment - # (expression has type "Tuple[Any]", variable has type "Tuple[Any, - # Any]") - args = (data,) # type: ignore[assignment] + args = (data,) method = "write" with compress_method(path, mode=mode) as f: From 4641ea738980fb99cc21eb01e27137843ef62d45 Mon Sep 17 00:00:00 2001 From: Maxim Ivanov Date: Fri, 18 Dec 2020 15:33:11 +0700 Subject: [PATCH 3/4] REF: inline _select_compress_method --- pandas/_testing.py | 25 +++++++++++-------------- 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/pandas/_testing.py b/pandas/_testing.py index 03a8d10f7f3e6..22c0468bc22da 100644 --- a/pandas/_testing.py +++ b/pandas/_testing.py @@ -311,7 +311,17 @@ def write_to_compressed(compression, path, data, dest="test"): ------ ValueError : An invalid compression value was passed in. """ - compress_method = _select_compress_method(compression) + compress_method: Callable + if compression == "zip": + compress_method = zipfile.ZipFile + elif compression == "gzip": + compress_method = gzip.GzipFile + elif compression == "bz2": + compress_method = bz2.BZ2File + elif compression == "xz": + compress_method = get_lzma_file(lzma) + else: + raise ValueError(f"Unrecognized compression type: {compression}") args: Tuple[Any, ...] if compression == "zip": @@ -327,19 +337,6 @@ def write_to_compressed(compression, path, data, dest="test"): getattr(f, method)(*args) -def _select_compress_method(compression: str) -> Callable: - if compression == "zip": - return zipfile.ZipFile - elif compression == "gzip": - return gzip.GzipFile - elif compression == "bz2": - return bz2.BZ2File - elif compression == "xz": - return get_lzma_file(lzma) - else: - raise ValueError(f"Unrecognized compression type: {compression}") - - def _get_tol_from_less_precise(check_less_precise: Union[bool, int]) -> float: """ Return the tolerance equivalent to the deprecated `check_less_precise` From 3002576312e2731cbaffe60bae0d0f499a7f8764 Mon Sep 17 00:00:00 2001 From: Maxim Ivanov Date: Fri, 18 Dec 2020 15:38:58 +0700 Subject: [PATCH 4/4] REF: slide conditionals --- pandas/_testing.py | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/pandas/_testing.py b/pandas/_testing.py index 22c0468bc22da..2d74cc89b8063 100644 --- a/pandas/_testing.py +++ b/pandas/_testing.py @@ -311,9 +311,16 @@ def write_to_compressed(compression, path, data, dest="test"): ------ ValueError : An invalid compression value was passed in. """ + args: Tuple[Any, ...] = (data,) + mode = "wb" + method = "write" compress_method: Callable + if compression == "zip": compress_method = zipfile.ZipFile + mode = "w" + args = (dest, data) + method = "writestr" elif compression == "gzip": compress_method = gzip.GzipFile elif compression == "bz2": @@ -323,16 +330,6 @@ def write_to_compressed(compression, path, data, dest="test"): else: raise ValueError(f"Unrecognized compression type: {compression}") - args: Tuple[Any, ...] - if compression == "zip": - mode = "w" - args = (dest, data) - method = "writestr" - else: - mode = "wb" - args = (data,) - method = "write" - with compress_method(path, mode=mode) as f: getattr(f, method)(*args)