Skip to content

TYP: handle mypy ignore in pandas/_testing #38236

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 6 commits into from
Dec 19, 2020
Merged
Changes from 3 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
48 changes: 27 additions & 21 deletions pandas/_testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -299,39 +309,35 @@ 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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think you could leave this inline and just type compress_method no?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess so.
I just thought that some small function extraction will be helpful here.
But if you suggest against the extraction, I can definitely do that way as well.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer for this to stay inline as well

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I inlined the function and also slid the conditionals. Can revert if this is not what you expect, but in my opinion, it simplifies the understanding that zip has a different signature as compared to other compression methods.


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:
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`
Expand Down