Skip to content

Commit d3c62ad

Browse files
authored
TYP: Add types for excel writer classes (#45246)
1 parent 37c3343 commit d3c62ad

File tree

5 files changed

+72
-34
lines changed

5 files changed

+72
-34
lines changed

pandas/io/excel/_base.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -533,7 +533,7 @@ def _workbook_class(self):
533533
def load_workbook(self, filepath_or_buffer):
534534
pass
535535

536-
def close(self):
536+
def close(self) -> None:
537537
if hasattr(self, "book") and hasattr(self.book, "close"):
538538
# pyxlsb: opens a TemporaryFile
539539
# openpyxl: https://stackoverflow.com/questions/31416842/
@@ -1083,7 +1083,7 @@ def __init__(
10831083
mode: str = "w",
10841084
storage_options: StorageOptions = None,
10851085
if_sheet_exists: str | None = None,
1086-
engine_kwargs: dict | None = None,
1086+
engine_kwargs: dict[str, Any] | None = None,
10871087
**kwargs,
10881088
):
10891089
# validate that this engine can handle the extension

pandas/io/excel/_odswriter.py

+10-4
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,16 @@
55
from typing import (
66
Any,
77
DefaultDict,
8+
Tuple,
9+
cast,
810
)
911

1012
import pandas._libs.json as json
11-
from pandas._typing import StorageOptions
13+
from pandas._typing import (
14+
FilePath,
15+
StorageOptions,
16+
WriteExcelBuffer,
17+
)
1218

1319
from pandas.io.excel._base import ExcelWriter
1420
from pandas.io.excel._util import (
@@ -24,9 +30,9 @@ class ODSWriter(ExcelWriter):
2430

2531
def __init__(
2632
self,
27-
path: str,
33+
path: FilePath | WriteExcelBuffer | ExcelWriter,
2834
engine: str | None = None,
29-
date_format=None,
35+
date_format: str | None = None,
3036
datetime_format=None,
3137
mode: str = "w",
3238
storage_options: StorageOptions = None,
@@ -88,7 +94,7 @@ def write_cells(
8894
self.sheets[sheet_name] = wks
8995

9096
if validate_freeze_panes(freeze_panes):
91-
assert freeze_panes is not None
97+
freeze_panes = cast(Tuple[int, int], freeze_panes)
9298
self._create_freeze_panes(sheet_name, freeze_panes)
9399

94100
for _ in range(startrow):

pandas/io/excel/_openpyxl.py

+17-8
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
from typing import (
55
TYPE_CHECKING,
66
Any,
7+
Tuple,
8+
cast,
79
)
810

911
import numpy as np
@@ -13,6 +15,7 @@
1315
ReadBuffer,
1416
Scalar,
1517
StorageOptions,
18+
WriteExcelBuffer,
1619
)
1720
from pandas.compat._optional import import_optional_dependency
1821

@@ -35,10 +38,10 @@ class OpenpyxlWriter(ExcelWriter):
3538

3639
def __init__(
3740
self,
38-
path,
39-
engine=None,
40-
date_format=None,
41-
datetime_format=None,
41+
path: FilePath | WriteExcelBuffer | ExcelWriter,
42+
engine: str | None = None,
43+
date_format: str | None = None,
44+
datetime_format: str | None = None,
4245
mode: str = "w",
4346
storage_options: StorageOptions = None,
4447
if_sheet_exists: str | None = None,
@@ -74,7 +77,7 @@ def __init__(
7477
if self.book.worksheets:
7578
self.book.remove(self.book.worksheets[0])
7679

77-
def save(self):
80+
def save(self) -> None:
7881
"""
7982
Save workbook to disk.
8083
"""
@@ -217,7 +220,7 @@ def _convert_to_stop(cls, stop_seq):
217220
return map(cls._convert_to_color, stop_seq)
218221

219222
@classmethod
220-
def _convert_to_fill(cls, fill_dict):
223+
def _convert_to_fill(cls, fill_dict: dict[str, Any]):
221224
"""
222225
Convert ``fill_dict`` to an openpyxl v2 Fill object.
223226
@@ -418,8 +421,13 @@ def _convert_to_protection(cls, protection_dict):
418421
return Protection(**protection_dict)
419422

420423
def write_cells(
421-
self, cells, sheet_name=None, startrow=0, startcol=0, freeze_panes=None
422-
):
424+
self,
425+
cells,
426+
sheet_name: str | None = None,
427+
startrow: int = 0,
428+
startcol: int = 0,
429+
freeze_panes: tuple[int, int] | None = None,
430+
) -> None:
423431
# Write the frame cells using openpyxl.
424432
sheet_name = self._get_sheet_name(sheet_name)
425433

@@ -453,6 +461,7 @@ def write_cells(
453461
self.sheets[sheet_name] = wks
454462

455463
if validate_freeze_panes(freeze_panes):
464+
freeze_panes = cast(Tuple[int, int], freeze_panes)
456465
wks.freeze_panes = wks.cell(
457466
row=freeze_panes[0] + 1, column=freeze_panes[1] + 1
458467
)

pandas/io/excel/_xlsxwriter.py

+18-9
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@
33
from typing import Any
44

55
import pandas._libs.json as json
6-
from pandas._typing import StorageOptions
6+
from pandas._typing import (
7+
FilePath,
8+
StorageOptions,
9+
WriteExcelBuffer,
10+
)
711

812
from pandas.io.excel._base import ExcelWriter
913
from pandas.io.excel._util import (
@@ -170,10 +174,10 @@ class XlsxWriter(ExcelWriter):
170174

171175
def __init__(
172176
self,
173-
path,
174-
engine=None,
175-
date_format=None,
176-
datetime_format=None,
177+
path: FilePath | WriteExcelBuffer | ExcelWriter,
178+
engine: str | None = None,
179+
date_format: str | None = None,
180+
datetime_format: str | None = None,
177181
mode: str = "w",
178182
storage_options: StorageOptions = None,
179183
if_sheet_exists: str | None = None,
@@ -201,15 +205,20 @@ def __init__(
201205

202206
self.book = Workbook(self.handles.handle, **engine_kwargs)
203207

204-
def save(self):
208+
def save(self) -> None:
205209
"""
206210
Save workbook to disk.
207211
"""
208-
return self.book.close()
212+
self.book.close()
209213

210214
def write_cells(
211-
self, cells, sheet_name=None, startrow=0, startcol=0, freeze_panes=None
212-
):
215+
self,
216+
cells,
217+
sheet_name: str | None = None,
218+
startrow: int = 0,
219+
startcol: int = 0,
220+
freeze_panes: tuple[int, int] | None = None,
221+
) -> None:
213222
# Write the frame cells using xlsxwriter.
214223
sheet_name = self._get_sheet_name(sheet_name)
215224

pandas/io/excel/_xlwt.py

+25-11
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,16 @@
33
from typing import (
44
TYPE_CHECKING,
55
Any,
6+
Tuple,
7+
cast,
68
)
79

810
import pandas._libs.json as json
9-
from pandas._typing import StorageOptions
11+
from pandas._typing import (
12+
FilePath,
13+
StorageOptions,
14+
WriteExcelBuffer,
15+
)
1016

1117
from pandas.io.excel._base import ExcelWriter
1218
from pandas.io.excel._util import (
@@ -24,11 +30,11 @@ class XlwtWriter(ExcelWriter):
2430

2531
def __init__(
2632
self,
27-
path,
28-
engine=None,
29-
date_format=None,
30-
datetime_format=None,
31-
encoding=None,
33+
path: FilePath | WriteExcelBuffer | ExcelWriter,
34+
engine: str | None = None,
35+
date_format: str | None = None,
36+
datetime_format: str | None = None,
37+
encoding: str | None = None,
3238
mode: str = "w",
3339
storage_options: StorageOptions = None,
3440
if_sheet_exists: str | None = None,
@@ -57,7 +63,7 @@ def __init__(
5763
self.fm_datetime = xlwt.easyxf(num_format_str=self.datetime_format)
5864
self.fm_date = xlwt.easyxf(num_format_str=self.date_format)
5965

60-
def save(self):
66+
def save(self) -> None:
6167
"""
6268
Save workbook to disk.
6369
"""
@@ -66,8 +72,13 @@ def save(self):
6672
self.book.save(self.handles.handle)
6773

6874
def write_cells(
69-
self, cells, sheet_name=None, startrow=0, startcol=0, freeze_panes=None
70-
):
75+
self,
76+
cells,
77+
sheet_name: str | None = None,
78+
startrow: int = 0,
79+
startcol: int = 0,
80+
freeze_panes: tuple[int, int] | None = None,
81+
) -> None:
7182

7283
sheet_name = self._get_sheet_name(sheet_name)
7384

@@ -78,6 +89,7 @@ def write_cells(
7889
self.sheets[sheet_name] = wks
7990

8091
if validate_freeze_panes(freeze_panes):
92+
freeze_panes = cast(Tuple[int, int], freeze_panes)
8193
wks.set_panes_frozen(True)
8294
wks.set_horz_split_pos(freeze_panes[0])
8395
wks.set_vert_split_pos(freeze_panes[1])
@@ -111,7 +123,7 @@ def write_cells(
111123

112124
@classmethod
113125
def _style_to_xlwt(
114-
cls, item, firstlevel: bool = True, field_sep=",", line_sep=";"
126+
cls, item, firstlevel: bool = True, field_sep: str = ",", line_sep: str = ";"
115127
) -> str:
116128
"""
117129
helper which recursively generate an xlwt easy style string
@@ -150,7 +162,9 @@ def _style_to_xlwt(
150162
return item
151163

152164
@classmethod
153-
def _convert_to_style(cls, style_dict, num_format_str=None):
165+
def _convert_to_style(
166+
cls, style_dict, num_format_str: str | None = None
167+
) -> XFStyle:
154168
"""
155169
converts a style_dict to an xlwt style object
156170

0 commit comments

Comments
 (0)