18
18
Optional ,
19
19
Tuple ,
20
20
Type ,
21
- Union ,
22
21
)
23
22
from urllib .parse import (
24
23
urljoin ,
29
28
)
30
29
import zipfile
31
30
32
- from pandas ._typing import FilePathOrBuffer , StorageOptions
31
+ from pandas ._typing import (
32
+ CompressionDict ,
33
+ CompressionOptions ,
34
+ FilePathOrBuffer ,
35
+ StorageOptions ,
36
+ )
33
37
from pandas .compat import _get_lzma_file , _import_lzma
34
38
from pandas .compat ._optional import import_optional_dependency
35
39
@@ -160,7 +164,7 @@ def is_fsspec_url(url: FilePathOrBuffer) -> bool:
160
164
def get_filepath_or_buffer (
161
165
filepath_or_buffer : FilePathOrBuffer ,
162
166
encoding : Optional [str ] = None ,
163
- compression : Optional [ str ] = None ,
167
+ compression : CompressionOptions = None ,
164
168
mode : Optional [str ] = None ,
165
169
storage_options : StorageOptions = None ,
166
170
):
@@ -188,7 +192,7 @@ def get_filepath_or_buffer(
188
192
189
193
Returns
190
194
-------
191
- Tuple[FilePathOrBuffer, str, str , bool]
195
+ Tuple[FilePathOrBuffer, str, CompressionOptions , bool]
192
196
Tuple containing the filepath or buffer, the encoding, the compression
193
197
and should_close.
194
198
"""
@@ -291,8 +295,8 @@ def file_path_to_url(path: str) -> str:
291
295
292
296
293
297
def 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 ]:
296
300
"""
297
301
Simplifies a compression argument to a compression method string and
298
302
a mapping containing additional arguments.
@@ -316,7 +320,7 @@ def get_compression_method(
316
320
if isinstance (compression , Mapping ):
317
321
compression_args = dict (compression )
318
322
try :
319
- compression_method = compression_args .pop ("method" )
323
+ compression_method = compression_args .pop ("method" ) # type: ignore
320
324
except KeyError as err :
321
325
raise ValueError ("If mapping, compression must have key 'method'" ) from err
322
326
else :
@@ -383,7 +387,7 @@ def get_handle(
383
387
path_or_buf ,
384
388
mode : str ,
385
389
encoding = None ,
386
- compression : Optional [ Union [ str , Mapping [ str , Any ]]] = None ,
390
+ compression : CompressionOptions = None ,
387
391
memory_map : bool = False ,
388
392
is_text : bool = True ,
389
393
errors = None ,
@@ -464,16 +468,13 @@ def get_handle(
464
468
# GZ Compression
465
469
if compression == "gzip" :
466
470
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 )
468
472
else :
469
473
f = gzip .GzipFile (fileobj = path_or_buf , mode = mode , ** compression_args )
470
474
471
475
# BZ Compression
472
476
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 )
477
478
478
479
# ZIP Compression
479
480
elif compression == "zip" :
@@ -577,7 +578,9 @@ def __init__(
577
578
if mode in ["wb" , "rb" ]:
578
579
mode = mode .replace ("b" , "" )
579
580
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 )
581
584
582
585
def write (self , data ):
583
586
archive_name = self .filename
0 commit comments