Skip to content

TYP: remove # type: ignore for unpacking compression_args #35344

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 1 commit into from
Jul 29, 2020
Merged
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
29 changes: 11 additions & 18 deletions pandas/io/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,8 +267,8 @@ def file_path_to_url(path: str) -> str:


def get_compression_method(
compression: Optional[Union[str, Mapping[str, str]]]
) -> Tuple[Optional[str], Dict[str, str]]:
compression: Optional[Union[str, Mapping[str, Any]]]
) -> Tuple[Optional[str], Dict[str, Any]]:
"""
Simplifies a compression argument to a compression method string and
a mapping containing additional arguments.
Expand All @@ -282,21 +282,23 @@ def get_compression_method(
Returns
-------
tuple of ({compression method}, Optional[str]
{compression arguments}, Dict[str, str])
{compression arguments}, Dict[str, Any])

Raises
------
ValueError on mapping missing 'method' key
"""
compression_method: Optional[str]
if isinstance(compression, Mapping):
compression_args = dict(compression)
try:
compression = compression_args.pop("method")
compression_method = compression_args.pop("method")
except KeyError as err:
raise ValueError("If mapping, compression must have key 'method'") from err
else:
compression_args = {}
return compression, compression_args
compression_method = compression
return compression_method, compression_args


def infer_compression(
Expand Down Expand Up @@ -434,28 +436,19 @@ def get_handle(

if compression:

# GH33398 the type ignores here seem related to mypy issue #5382;
# it may be possible to remove them once that is resolved.

# GZ Compression
if compression == "gzip":
if is_path:
f = gzip.open(
path_or_buf, mode, **compression_args # type: ignore
)
f = gzip.open(path_or_buf, mode, **compression_args)
else:
f = gzip.GzipFile(
fileobj=path_or_buf, **compression_args # type: ignore
)
f = gzip.GzipFile(fileobj=path_or_buf, **compression_args)

# BZ Compression
elif compression == "bz2":
if is_path:
f = bz2.BZ2File(
path_or_buf, mode, **compression_args # type: ignore
)
f = bz2.BZ2File(path_or_buf, mode, **compression_args)
else:
f = bz2.BZ2File(path_or_buf, **compression_args) # type: ignore
f = bz2.BZ2File(path_or_buf, **compression_args)

# ZIP Compression
elif compression == "zip":
Expand Down