3030from pandas .core .dtypes .dtypes import CategoricalDtype
3131from pandas .core .dtypes .missing import isna
3232
33+ from pandas ._typing import FilePathOrBuffer
3334from pandas .core import algorithms
3435from pandas .core .arrays import Categorical
3536from pandas .core .frame import DataFrame
@@ -400,7 +401,7 @@ def _validate_names(names):
400401 return names
401402
402403
403- def _read (filepath_or_buffer , kwds ):
404+ def _read (filepath_or_buffer : FilePathOrBuffer , kwds ):
404405 """Generic reader of line files."""
405406 encoding = kwds .get ('encoding' , None )
406407 if encoding is not None :
@@ -409,7 +410,12 @@ def _read(filepath_or_buffer, kwds):
409410
410411 compression = kwds .get ('compression' , 'infer' )
411412 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 (
413419 filepath_or_buffer , encoding , compression )
414420 kwds ['compression' ] = compression
415421
@@ -426,7 +432,7 @@ def _read(filepath_or_buffer, kwds):
426432 _validate_names (kwds .get ("names" , None ))
427433
428434 # Create the parser.
429- parser = TextFileReader (filepath_or_buffer , ** kwds )
435+ parser = TextFileReader (fp_or_buf , ** kwds )
430436
431437 if chunksize or iterator :
432438 return parser
@@ -438,7 +444,7 @@ def _read(filepath_or_buffer, kwds):
438444
439445 if should_close :
440446 try :
441- filepath_or_buffer .close ()
447+ fp_or_buf .close ()
442448 except ValueError :
443449 pass
444450
@@ -533,7 +539,7 @@ def _make_parser_function(name, default_sep=','):
533539 else :
534540 sep = default_sep
535541
536- def parser_f (filepath_or_buffer ,
542+ def parser_f (filepath_or_buffer : FilePathOrBuffer ,
537543 sep = sep ,
538544 delimiter = None ,
539545
@@ -725,8 +731,11 @@ def parser_f(filepath_or_buffer,
725731 )(read_table )
726732
727733
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 ):
730739
731740 r"""
732741 Read a table of fixed-width formatted lines into DataFrame.
0 commit comments