Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
bind input type of encding and mode with the returned type; removed i…
…gnore statements (mypy will compile about filepath_or_buffer)
  • Loading branch information
twoertwein committed Aug 31, 2020
commit 935fc4bd676bcb38d003352c9ccb418dad8f1ea9
18 changes: 13 additions & 5 deletions pandas/_typing.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from dataclasses import dataclass
from datetime import datetime, timedelta, tzinfo
from pathlib import Path
from typing import (
Expand All @@ -8,6 +9,7 @@
Callable,
Collection,
Dict,
Generic,
Hashable,
List,
Mapping,
Expand Down Expand Up @@ -117,7 +119,13 @@
CompressionOptions = Optional[Union[str, CompressionDict]]


class IOargs(NamedTuple):
# lets us bind types
ModeVar = TypeVar("ModeVar", str, None)
EncodingVar = TypeVar("EncodingVar", str, None)


@dataclass
class IOargs(Generic[ModeVar, EncodingVar]):
"""
Return value of io/common.py:get_filepath_or_buffer.

Expand All @@ -128,7 +136,7 @@ class IOargs(NamedTuple):
"""

filepath_or_buffer: FilePathOrBuffer
encoding: Optional[str]
compression: CompressionOptions = None
should_close: bool = False
mode: Optional[str] = None
encoding: EncodingVar
compression: CompressionOptions
should_close: bool
mode: Union[ModeVar, str]
2 changes: 1 addition & 1 deletion pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -2281,7 +2281,7 @@ def to_markdown(
result = tabulate.tabulate(self, **kwargs)
if buf is None:
return result
buf, _, _, should_close, _ = get_filepath_or_buffer( # type: ignore
buf, _, _, should_close, _ = get_filepath_or_buffer(
buf, mode=mode, storage_options=storage_options
)
assert not isinstance(buf, str)
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -3249,7 +3249,7 @@ def to_csv(
formatter.save()

if path_or_buf is None:
return formatter.path_or_buf.getvalue() # type: ignore
return formatter.path_or_buf.getvalue()

return None

Expand Down
9 changes: 6 additions & 3 deletions pandas/io/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,11 @@
from pandas._typing import (
CompressionDict,
CompressionOptions,
EncodingVar,
FilePathOrBuffer,
FilePathOrBufferVar,
IOargs,
ModeVar,
StorageOptions,
)
from pandas.compat import _get_lzma_file, _import_lzma
Expand Down Expand Up @@ -166,11 +169,11 @@ def is_fsspec_url(url: FilePathOrBuffer) -> bool:

def get_filepath_or_buffer(
filepath_or_buffer: FilePathOrBuffer,
encoding: Optional[str] = None,
encoding: EncodingVar = None, # type: ignore[assignment]
compression: CompressionOptions = None,
mode: Optional[str] = None,
mode: ModeVar = None, # type: ignore[assignment]
storage_options: StorageOptions = None,
) -> IOargs:
) -> IOargs[ModeVar, EncodingVar]:
"""
If the filepath_or_buffer is a url, translate and return the buffer.
Otherwise passthrough.
Expand Down
8 changes: 1 addition & 7 deletions pandas/io/formats/csvs.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,7 @@ def __init__(
compression, self.compression_args = get_compression_method(compression)
self.compression = infer_compression(path_or_buf, compression)

(
self.path_or_buf,
_,
_,
self.should_close,
mode, # type: ignore
) = get_filepath_or_buffer(
(self.path_or_buf, _, _, self.should_close, mode,) = get_filepath_or_buffer(
path_or_buf,
encoding=encoding,
compression=self.compression,
Expand Down
2 changes: 1 addition & 1 deletion pandas/io/json/_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -645,7 +645,7 @@ def read_json(

result = json_reader.read()
if should_close:
filepath_or_buffer.close() # type: ignore
filepath_or_buffer.close()

return result

Expand Down
2 changes: 1 addition & 1 deletion pandas/io/parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,7 @@ def _read(filepath_or_buffer: FilePathOrBuffer, kwds):

if should_close:
try:
fp_or_buf.close() # type: ignore
fp_or_buf.close()
except ValueError:
pass

Expand Down
2 changes: 1 addition & 1 deletion pandas/io/pickle.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ def to_pickle(
_f.close()
if should_close:
try:
fp_or_buf.close() # type: ignore
fp_or_buf.close()
except ValueError:
pass

Expand Down