Skip to content

Commit 0f70991

Browse files
committed
TYP/CLN: cleanup, add type annotation pandas-dev#36021
1 parent d7f30b4 commit 0f70991

File tree

2 files changed

+13
-46
lines changed

2 files changed

+13
-46
lines changed

pandas/io/excel/_openpyxl.py

+13-43
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import List
1+
from typing import Any, Dict, List, Optional
22

33
import numpy as np
44

@@ -22,53 +22,22 @@ def __init__(self, path, engine=None, mode="w", **engine_kwargs):
2222
if self.mode == "a": # Load from existing workbook
2323
from openpyxl import load_workbook
2424

25-
book = load_workbook(self.path)
26-
self.book = book
25+
self.book = load_workbook(self.path)
2726
else:
2827
# Create workbook object with default optimized_write=True.
2928
self.book = Workbook()
3029

3130
if self.book.worksheets:
32-
try:
33-
self.book.remove(self.book.worksheets[0])
34-
except AttributeError:
35-
36-
# compat - for openpyxl <= 2.4
37-
self.book.remove_sheet(self.book.worksheets[0])
31+
self.book.remove(self.book.worksheets[0])
3832

3933
def save(self):
4034
"""
4135
Save workbook to disk.
4236
"""
43-
return self.book.save(self.path)
37+
self.book.save(self.path)
4438

4539
@classmethod
46-
def _convert_to_style(cls, style_dict):
47-
"""
48-
Converts a style_dict to an openpyxl style object.
49-
50-
Parameters
51-
----------
52-
style_dict : style dictionary to convert
53-
"""
54-
from openpyxl.style import Style
55-
56-
xls_style = Style()
57-
for key, value in style_dict.items():
58-
for nk, nv in value.items():
59-
if key == "borders":
60-
(
61-
xls_style.borders.__getattribute__(nk).__setattr__(
62-
"border_style", nv
63-
)
64-
)
65-
else:
66-
xls_style.__getattribute__(key).__setattr__(nk, nv)
67-
68-
return xls_style
69-
70-
@classmethod
71-
def _convert_to_style_kwargs(cls, style_dict):
40+
def _convert_to_style_kwargs(cls, style_dict: dict) -> Dict[str, Any]:
7241
"""
7342
Convert a style_dict to a set of kwargs suitable for initializing
7443
or updating-on-copy an openpyxl v2 style object.
@@ -93,7 +62,7 @@ def _convert_to_style_kwargs(cls, style_dict):
9362
"""
9463
_style_key_map = {"borders": "border"}
9564

96-
style_kwargs = {}
65+
style_kwargs: Dict[str, Any] = {}
9766
for k, v in style_dict.items():
9867
if k in _style_key_map:
9968
k = _style_key_map[k]
@@ -404,7 +373,7 @@ def write_cells(
404373
# Write the frame cells using openpyxl.
405374
sheet_name = self._get_sheet_name(sheet_name)
406375

407-
_style_cache = {}
376+
_style_cache: Dict[str, Dict[str, Any]] = {}
408377

409378
if sheet_name in self.sheets:
410379
wks = self.sheets[sheet_name]
@@ -426,7 +395,7 @@ def write_cells(
426395
if fmt:
427396
xcell.number_format = fmt
428397

429-
style_kwargs = {}
398+
style_kwargs: Optional[Dict[str, Any]] = {}
430399
if cell.style:
431400
key = str(cell.style)
432401
style_kwargs = _style_cache.get(key)
@@ -515,16 +484,17 @@ def get_sheet_by_index(self, index: int):
515484

516485
def _convert_cell(self, cell, convert_float: bool) -> Scalar:
517486

518-
# TODO: replace with openpyxl constants
487+
from openpyxl.cell.cell import TYPE_BOOL, TYPE_ERROR, TYPE_NUMERIC
488+
519489
if cell.is_date:
520490
return cell.value
521-
elif cell.data_type == "e":
491+
elif cell.data_type == TYPE_ERROR:
522492
return np.nan
523-
elif cell.data_type == "b":
493+
elif cell.data_type == TYPE_BOOL:
524494
return bool(cell.value)
525495
elif cell.value is None:
526496
return "" # compat with xlrd
527-
elif cell.data_type == "n":
497+
elif cell.data_type == TYPE_NUMERIC:
528498
# GH5394
529499
if convert_float:
530500
val = int(cell.value)

setup.cfg

-3
Original file line numberDiff line numberDiff line change
@@ -217,9 +217,6 @@ check_untyped_defs=False
217217
[mypy-pandas.io.excel._base]
218218
check_untyped_defs=False
219219

220-
[mypy-pandas.io.excel._openpyxl]
221-
check_untyped_defs=False
222-
223220
[mypy-pandas.io.excel._util]
224221
check_untyped_defs=False
225222

0 commit comments

Comments
 (0)