Skip to content

Commit faeed4c

Browse files
committed
fix typing and tests
1 parent 1f7ffea commit faeed4c

File tree

2 files changed

+21
-7
lines changed

2 files changed

+21
-7
lines changed

pandas/io/formats/csvs.py

+16-6
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,20 @@
1212
import csv as csvlib
1313
import os
1414
from typing import (
15+
IO,
1516
TYPE_CHECKING,
1617
Any,
18+
AnyStr,
1719
cast,
1820
)
1921

2022
import numpy as np
2123

2224
from pandas._libs import writers as libwriters
23-
from pandas.compat import pa_version_under11p0
25+
from pandas.compat import (
26+
pa_version_under8p0,
27+
pa_version_under11p0,
28+
)
2429
from pandas.compat._optional import import_optional_dependency
2530
from pandas.util._decorators import cache_readonly
2631

@@ -258,9 +263,12 @@ def save(self) -> None:
258263
is_text=self.engine == "python",
259264
) as handles:
260265
# Note: self.encoding is irrelevant here
261-
self._save(handles.handle)
262266

263-
def _save_pyarrow(self, handle) -> None:
267+
# This is a mypy bug?
268+
# error: Cannot infer type argument 1 of "_save" of "CSVFormatter" [misc]
269+
self._save(handles.handle) # type: ignore[misc]
270+
271+
def _save_pyarrow(self, handle: IO[AnyStr]) -> None:
264272
pa = import_optional_dependency("pyarrow")
265273
pa_csv = import_optional_dependency("pyarrow.csv")
266274
# Convert index to column and rename name to empty string
@@ -289,19 +297,21 @@ def _save_pyarrow(self, handle) -> None:
289297
f"Quoting option {self.quoting} is not supported with engine='pyarrow'"
290298
)
291299

292-
kwargs = {
300+
kwargs: dict[str, Any] = {
293301
"include_header": self._need_to_save_header,
294302
"batch_size": self.chunksize,
295-
"delimiter": self.sep,
296303
}
297304

305+
if not pa_version_under8p0:
306+
kwargs["delimiter"] = self.sep
307+
298308
if not pa_version_under11p0:
299309
kwargs["quoting_style"] = pa_quoting
300310

301311
write_options = pa_csv.WriteOptions(**kwargs)
302312
pa_csv.write_csv(table, handle, write_options)
303313

304-
def _save(self, handle) -> None:
314+
def _save(self, handle: IO[AnyStr]) -> None:
305315
if self.engine == "pyarrow":
306316
self._save_pyarrow(handle)
307317
else:

pandas/io/formats/format.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
Any,
3232
Callable,
3333
Final,
34+
Union,
3435
cast,
3536
)
3637
from unicodedata import east_asian_width
@@ -1157,11 +1158,14 @@ def to_csv(
11571158
csv_formatter.save()
11581159

11591160
if created_buffer:
1161+
path_or_buf = cast(Union[BytesIO, StringIO], path_or_buf)
11601162
content = path_or_buf.getvalue()
1161-
if isinstance(path_or_buf, BytesIO):
1163+
if isinstance(content, bytes):
11621164
# Need to decode into string since the
11631165
# pyarrow engine only writes binary data
1166+
# content = cast(bytes, content)
11641167
content = content.decode("utf-8")
1168+
# content = cast(str, content)
11651169
path_or_buf.close()
11661170
return content
11671171

0 commit comments

Comments
 (0)