Skip to content

Commit 6331aca

Browse files
authored
TYP: stricter type for compression (#44854)
1 parent d19b47f commit 6331aca

File tree

5 files changed

+22
-11
lines changed

5 files changed

+22
-11
lines changed

pandas/_typing.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,9 @@ def closed(self) -> bool:
242242

243243
# compression keywords and compression
244244
CompressionDict = Dict[str, Any]
245-
CompressionOptions = Optional[Union[str, CompressionDict]]
245+
CompressionOptions = Optional[
246+
Union[Literal["infer", "gzip", "bz2", "zip", "xz"], CompressionDict]
247+
]
246248

247249

248250
# types in DataFrameFormatter

pandas/io/parsers/readers.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
from pandas._libs.parsers import STR_NA_VALUES
2020
from pandas._typing import (
2121
ArrayLike,
22+
CompressionOptions,
2223
DtypeArg,
2324
FilePath,
2425
ReadCsvBuffer,
@@ -618,7 +619,7 @@ def read_csv(
618619
iterator=False,
619620
chunksize=None,
620621
# Quoting, Compression, and File Format
621-
compression="infer",
622+
compression: CompressionOptions = "infer",
622623
thousands=None,
623624
decimal: str = ".",
624625
lineterminator=None,
@@ -716,7 +717,7 @@ def read_table(
716717
iterator=False,
717718
chunksize=None,
718719
# Quoting, Compression, and File Format
719-
compression="infer",
720+
compression: CompressionOptions = "infer",
720721
thousands=None,
721722
decimal: str = ".",
722723
lineterminator=None,

pandas/io/xml.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,8 @@ def __init__(
105105
names,
106106
encoding,
107107
stylesheet,
108-
compression,
109-
storage_options,
108+
compression: CompressionOptions,
109+
storage_options: StorageOptions,
110110
) -> None:
111111
self.path_or_buffer = path_or_buffer
112112
self.xpath = xpath
@@ -570,8 +570,8 @@ def _transform_doc(self) -> bytes:
570570
def get_data_from_filepath(
571571
filepath_or_buffer: FilePath | bytes | ReadBuffer[bytes] | ReadBuffer[str],
572572
encoding,
573-
compression,
574-
storage_options,
573+
compression: CompressionOptions,
574+
storage_options: StorageOptions,
575575
) -> str | bytes | ReadBuffer[bytes] | ReadBuffer[str]:
576576
"""
577577
Extract raw XML data.
@@ -666,8 +666,8 @@ def _parse(
666666
encoding,
667667
parser,
668668
stylesheet,
669-
compression,
670-
storage_options,
669+
compression: CompressionOptions,
670+
storage_options: StorageOptions,
671671
**kwargs,
672672
) -> DataFrame:
673673
"""

pandas/tests/io/xml/test_to_xml.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -1311,7 +1311,12 @@ def test_filename_and_suffix_comp(parser, comp, compfile):
13111311
def test_unsuported_compression(datapath, parser):
13121312
with pytest.raises(ValueError, match="Unrecognized compression type"):
13131313
with tm.ensure_clean() as path:
1314-
geom_df.to_xml(path, parser=parser, compression="7z")
1314+
# Argument "compression" to "to_xml" of "DataFrame" has incompatible type
1315+
# "Literal['7z']"; expected "Union[Literal['infer'], Literal['gzip'],
1316+
# Literal['bz2'], Literal['zip'], Literal['xz'], Dict[str, Any], None]"
1317+
geom_df.to_xml(
1318+
path, parser=parser, compression="7z" # type: ignore[arg-type]
1319+
)
13151320

13161321

13171322
# STORAGE OPTIONS

pandas/tests/io/xml/test_xml.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -1069,7 +1069,10 @@ def test_wrong_compression_zip(parser, comp):
10691069
def test_unsuported_compression(datapath, parser):
10701070
with pytest.raises(ValueError, match="Unrecognized compression type"):
10711071
with tm.ensure_clean() as path:
1072-
read_xml(path, parser=parser, compression="7z")
1072+
# error: Argument "compression" to "read_xml" has incompatible type
1073+
# "Literal['7z']"; expected "Union[Literal['infer'], Literal['gzip'],
1074+
# Literal['bz2'], Literal['zip'], Literal['xz'], Dict[str, Any], None]"
1075+
read_xml(path, parser=parser, compression="7z") # type: ignore[arg-type]
10731076

10741077

10751078
# STORAGE OPTIONS

0 commit comments

Comments
 (0)