1818 Optional ,
1919 Tuple ,
2020 Type ,
21- Union ,
2221)
2322from urllib .parse import (
2423 urljoin ,
2928)
3029import zipfile
3130
32- from pandas ._typing import FilePathOrBuffer , StorageOptions
31+ from pandas ._typing import (
32+ CompressionDict ,
33+ CompressionOptions ,
34+ FilePathOrBuffer ,
35+ StorageOptions ,
36+ )
3337from pandas .compat import _get_lzma_file , _import_lzma
3438from pandas .compat ._optional import import_optional_dependency
3539
@@ -160,7 +164,7 @@ def is_fsspec_url(url: FilePathOrBuffer) -> bool:
160164def get_filepath_or_buffer (
161165 filepath_or_buffer : FilePathOrBuffer ,
162166 encoding : Optional [str ] = None ,
163- compression : Optional [ str ] = None ,
167+ compression : CompressionOptions = None ,
164168 mode : Optional [str ] = None ,
165169 storage_options : StorageOptions = None ,
166170):
@@ -188,7 +192,7 @@ def get_filepath_or_buffer(
188192
189193 Returns
190194 -------
191- Tuple[FilePathOrBuffer, str, str , bool]
195+ Tuple[FilePathOrBuffer, str, CompressionOptions , bool]
192196 Tuple containing the filepath or buffer, the encoding, the compression
193197 and should_close.
194198 """
@@ -291,8 +295,8 @@ def file_path_to_url(path: str) -> str:
291295
292296
293297def get_compression_method (
294- compression : Optional [ Union [ str , Mapping [ str , Any ]]]
295- ) -> Tuple [Optional [str ], Dict [ str , Any ] ]:
298+ compression : CompressionOptions ,
299+ ) -> Tuple [Optional [str ], CompressionDict ]:
296300 """
297301 Simplifies a compression argument to a compression method string and
298302 a mapping containing additional arguments.
@@ -316,7 +320,7 @@ def get_compression_method(
316320 if isinstance (compression , Mapping ):
317321 compression_args = dict (compression )
318322 try :
319- compression_method = compression_args .pop ("method" )
323+ compression_method = compression_args .pop ("method" ) # type: ignore
320324 except KeyError as err :
321325 raise ValueError ("If mapping, compression must have key 'method'" ) from err
322326 else :
@@ -383,7 +387,7 @@ def get_handle(
383387 path_or_buf ,
384388 mode : str ,
385389 encoding = None ,
386- compression : Optional [ Union [ str , Mapping [ str , Any ]]] = None ,
390+ compression : CompressionOptions = None ,
387391 memory_map : bool = False ,
388392 is_text : bool = True ,
389393 errors = None ,
@@ -464,16 +468,13 @@ def get_handle(
464468 # GZ Compression
465469 if compression == "gzip" :
466470 if is_path :
467- f = gzip .open ( path_or_buf , mode , ** compression_args )
471+ f = gzip .GzipFile ( filename = path_or_buf , mode = mode , ** compression_args )
468472 else :
469473 f = gzip .GzipFile (fileobj = path_or_buf , mode = mode , ** compression_args )
470474
471475 # BZ Compression
472476 elif compression == "bz2" :
473- if is_path :
474- f = bz2 .BZ2File (path_or_buf , mode , ** compression_args )
475- else :
476- f = bz2 .BZ2File (path_or_buf , mode = mode , ** compression_args )
477+ f = bz2 .BZ2File (path_or_buf , mode = mode , ** compression_args )
477478
478479 # ZIP Compression
479480 elif compression == "zip" :
@@ -577,7 +578,9 @@ def __init__(
577578 if mode in ["wb" , "rb" ]:
578579 mode = mode .replace ("b" , "" )
579580 self .archive_name = archive_name
580- super ().__init__ (file , mode , zipfile .ZIP_DEFLATED , ** kwargs )
581+ kwargs_zip : Dict [str , Any ] = {"compression" : zipfile .ZIP_DEFLATED }
582+ kwargs_zip .update (kwargs )
583+ super ().__init__ (file , mode , ** kwargs_zip )
581584
582585 def write (self , data ):
583586 archive_name = self .filename
0 commit comments