Skip to content

Commit e1aa6f3

Browse files
WillAydjreback
authored andcommitted
Initial pandas.typing Module (#25884)
1 parent 2792705 commit e1aa6f3

File tree

4 files changed

+24
-9
lines changed

4 files changed

+24
-9
lines changed

pandas/_typing.py

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
from pathlib import Path
2+
from typing import IO, AnyStr, Union
3+
4+
FilePathOrBuffer = Union[str, Path, IO[AnyStr]]

pandas/io/gcs.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,6 @@ def get_filepath_or_buffer(filepath_or_buffer, encoding=None,
1212
mode = 'rb'
1313

1414
fs = gcsfs.GCSFileSystem()
15-
filepath_or_buffer = fs.open(filepath_or_buffer, mode)
15+
filepath_or_buffer = fs.open(
16+
filepath_or_buffer, mode) # type: gcsfs.GCSFile
1617
return filepath_or_buffer, None, compression, True

pandas/io/parsers.py

+16-7
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
from pandas.core.dtypes.dtypes import CategoricalDtype
3131
from pandas.core.dtypes.missing import isna
3232

33+
from pandas._typing import FilePathOrBuffer
3334
from pandas.core import algorithms
3435
from pandas.core.arrays import Categorical
3536
from 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.

pandas/io/s3.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,6 @@ def get_filepath_or_buffer(filepath_or_buffer, encoding=None,
3131
# A NoCredentialsError is raised if you don't have creds
3232
# for that bucket.
3333
fs = s3fs.S3FileSystem(anon=True)
34-
filepath_or_buffer = fs.open(_strip_schema(filepath_or_buffer), mode)
34+
filepath_or_buffer = fs.open(
35+
_strip_schema(filepath_or_buffer), mode) # type: s3fs.S3File
3536
return filepath_or_buffer, None, compression, True

0 commit comments

Comments
 (0)