30
30
from pandas .core .dtypes .dtypes import CategoricalDtype
31
31
from pandas .core .dtypes .missing import isna
32
32
33
+ from pandas ._typing import FilePathOrBuffer
33
34
from pandas .core import algorithms
34
35
from pandas .core .arrays import Categorical
35
36
from pandas .core .frame import DataFrame
@@ -400,7 +401,7 @@ def _validate_names(names):
400
401
return names
401
402
402
403
403
- def _read (filepath_or_buffer , kwds ):
404
+ def _read (filepath_or_buffer : FilePathOrBuffer , kwds ):
404
405
"""Generic reader of line files."""
405
406
encoding = kwds .get ('encoding' , None )
406
407
if encoding is not None :
@@ -409,7 +410,12 @@ def _read(filepath_or_buffer, kwds):
409
410
410
411
compression = kwds .get ('compression' , 'infer' )
411
412
compression = _infer_compression (filepath_or_buffer , compression )
412
- filepath_or_buffer , _ , compression , should_close = get_filepath_or_buffer (
413
+
414
+ # TODO: get_filepath_or_buffer could return
415
+ # Union[FilePathOrBuffer, s3fs.S3File, gcsfs.GCSFile]
416
+ # though mypy handling of conditional imports is difficult.
417
+ # See https://github.com/python/mypy/issues/1297
418
+ fp_or_buf , _ , compression , should_close = get_filepath_or_buffer (
413
419
filepath_or_buffer , encoding , compression )
414
420
kwds ['compression' ] = compression
415
421
@@ -426,7 +432,7 @@ def _read(filepath_or_buffer, kwds):
426
432
_validate_names (kwds .get ("names" , None ))
427
433
428
434
# Create the parser.
429
- parser = TextFileReader (filepath_or_buffer , ** kwds )
435
+ parser = TextFileReader (fp_or_buf , ** kwds )
430
436
431
437
if chunksize or iterator :
432
438
return parser
@@ -438,7 +444,7 @@ def _read(filepath_or_buffer, kwds):
438
444
439
445
if should_close :
440
446
try :
441
- filepath_or_buffer .close ()
447
+ fp_or_buf .close ()
442
448
except ValueError :
443
449
pass
444
450
@@ -533,7 +539,7 @@ def _make_parser_function(name, default_sep=','):
533
539
else :
534
540
sep = default_sep
535
541
536
- def parser_f (filepath_or_buffer ,
542
+ def parser_f (filepath_or_buffer : FilePathOrBuffer ,
537
543
sep = sep ,
538
544
delimiter = None ,
539
545
@@ -725,8 +731,11 @@ def parser_f(filepath_or_buffer,
725
731
)(read_table )
726
732
727
733
728
- def read_fwf (filepath_or_buffer , colspecs = 'infer' , widths = None ,
729
- infer_nrows = 100 , ** kwds ):
734
+ def read_fwf (filepath_or_buffer : FilePathOrBuffer ,
735
+ colspecs = 'infer' ,
736
+ widths = None ,
737
+ infer_nrows = 100 ,
738
+ ** kwds ):
730
739
731
740
r"""
732
741
Read a table of fixed-width formatted lines into DataFrame.
0 commit comments