Skip to content

TYP: stricter type for compression #44854

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 2 commits into from
Dec 14, 2021
Merged
Show file tree
Hide file tree
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
4 changes: 3 additions & 1 deletion pandas/_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,9 @@ def closed(self) -> bool:

# compression keywords and compression
CompressionDict = Dict[str, Any]
CompressionOptions = Optional[Union[str, CompressionDict]]
CompressionOptions = Optional[
Union[Literal["infer", "gzip", "bz2", "zip", "xz"], CompressionDict]
]


# types in DataFrameFormatter
Expand Down
5 changes: 3 additions & 2 deletions pandas/io/parsers/readers.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from pandas._libs.parsers import STR_NA_VALUES
from pandas._typing import (
ArrayLike,
CompressionOptions,
DtypeArg,
FilePath,
ReadCsvBuffer,
Expand Down Expand Up @@ -618,7 +619,7 @@ def read_csv(
iterator=False,
chunksize=None,
# Quoting, Compression, and File Format
compression="infer",
compression: CompressionOptions = "infer",
thousands=None,
decimal: str = ".",
lineterminator=None,
Expand Down Expand Up @@ -716,7 +717,7 @@ def read_table(
iterator=False,
chunksize=None,
# Quoting, Compression, and File Format
compression="infer",
compression: CompressionOptions = "infer",
thousands=None,
decimal: str = ".",
lineterminator=None,
Expand Down
12 changes: 6 additions & 6 deletions pandas/io/xml.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,8 @@ def __init__(
names,
encoding,
stylesheet,
compression,
storage_options,
compression: CompressionOptions,
storage_options: StorageOptions,
) -> None:
self.path_or_buffer = path_or_buffer
self.xpath = xpath
Expand Down Expand Up @@ -570,8 +570,8 @@ def _transform_doc(self) -> bytes:
def get_data_from_filepath(
filepath_or_buffer: FilePath | bytes | ReadBuffer[bytes] | ReadBuffer[str],
encoding,
compression,
storage_options,
compression: CompressionOptions,
storage_options: StorageOptions,
) -> str | bytes | ReadBuffer[bytes] | ReadBuffer[str]:
"""
Extract raw XML data.
Expand Down Expand Up @@ -666,8 +666,8 @@ def _parse(
encoding,
parser,
stylesheet,
compression,
storage_options,
compression: CompressionOptions,
storage_options: StorageOptions,
**kwargs,
) -> DataFrame:
"""
Expand Down
7 changes: 6 additions & 1 deletion pandas/tests/io/xml/test_to_xml.py
Original file line number Diff line number Diff line change
Expand Up @@ -1311,7 +1311,12 @@ def test_filename_and_suffix_comp(parser, comp, compfile):
def test_unsuported_compression(datapath, parser):
with pytest.raises(ValueError, match="Unrecognized compression type"):
with tm.ensure_clean() as path:
geom_df.to_xml(path, parser=parser, compression="7z")
# Argument "compression" to "to_xml" of "DataFrame" has incompatible type
# "Literal['7z']"; expected "Union[Literal['infer'], Literal['gzip'],
# Literal['bz2'], Literal['zip'], Literal['xz'], Dict[str, Any], None]"
geom_df.to_xml(
path, parser=parser, compression="7z" # type: ignore[arg-type]
)


# STORAGE OPTIONS
Expand Down
5 changes: 4 additions & 1 deletion pandas/tests/io/xml/test_xml.py
Original file line number Diff line number Diff line change
Expand Up @@ -1069,7 +1069,10 @@ def test_wrong_compression_zip(parser, comp):
def test_unsuported_compression(datapath, parser):
with pytest.raises(ValueError, match="Unrecognized compression type"):
with tm.ensure_clean() as path:
read_xml(path, parser=parser, compression="7z")
# error: Argument "compression" to "read_xml" has incompatible type
# "Literal['7z']"; expected "Union[Literal['infer'], Literal['gzip'],
# Literal['bz2'], Literal['zip'], Literal['xz'], Dict[str, Any], None]"
read_xml(path, parser=parser, compression="7z") # type: ignore[arg-type]


# STORAGE OPTIONS
Expand Down