Skip to content

Commit db1db7e

Browse files
TYP: use from __future__ import annotations more - batch 2 (#41894)
1 parent 8b9b1a1 commit db1db7e

File tree

10 files changed

+150
-177
lines changed

10 files changed

+150
-177
lines changed

pandas/io/excel/_xlsxwriter.py

+6-10
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
1-
from typing import (
2-
Any,
3-
Dict,
4-
List,
5-
Optional,
6-
Tuple,
7-
)
1+
from __future__ import annotations
2+
3+
from typing import Any
84

95
import pandas._libs.json as json
106
from pandas._typing import StorageOptions
@@ -17,7 +13,7 @@ class _XlsxStyler:
1713
# Map from openpyxl-oriented styles to flatter xlsxwriter representation
1814
# Ordering necessary for both determinism and because some are keyed by
1915
# prefixes of others.
20-
STYLE_MAPPING: Dict[str, List[Tuple[Tuple[str, ...], str]]] = {
16+
STYLE_MAPPING: dict[str, list[tuple[tuple[str, ...], str]]] = {
2117
"font": [
2218
(("name",), "font_name"),
2319
(("sz",), "font_size"),
@@ -177,8 +173,8 @@ def __init__(
177173
datetime_format=None,
178174
mode: str = "w",
179175
storage_options: StorageOptions = None,
180-
if_sheet_exists: Optional[str] = None,
181-
engine_kwargs: Optional[Dict[str, Any]] = None,
176+
if_sheet_exists: str | None = None,
177+
engine_kwargs: dict[str, Any] | None = None,
182178
):
183179
# Use the xlsxwriter module as the Excel writer.
184180
from xlsxwriter import Workbook

pandas/io/excel/_xlwt.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1+
from __future__ import annotations
2+
13
from typing import (
24
TYPE_CHECKING,
35
Any,
4-
Dict,
5-
Optional,
66
)
77

88
import pandas._libs.json as json
@@ -28,8 +28,8 @@ def __init__(
2828
encoding=None,
2929
mode: str = "w",
3030
storage_options: StorageOptions = None,
31-
if_sheet_exists: Optional[str] = None,
32-
engine_kwargs: Optional[Dict[str, Any]] = None,
31+
if_sheet_exists: str | None = None,
32+
engine_kwargs: dict[str, Any] | None = None,
3333
):
3434
# Use the xlwt module as the Excel writer.
3535
import xlwt
@@ -76,7 +76,7 @@ def write_cells(
7676
wks.set_horz_split_pos(freeze_panes[0])
7777
wks.set_vert_split_pos(freeze_panes[1])
7878

79-
style_dict: Dict[str, XFStyle] = {}
79+
style_dict: dict[str, XFStyle] = {}
8080

8181
for cell in cells:
8282
val, fmt = self._value_with_fmt(cell.val)

pandas/io/formats/css.py

+11-14
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
"""
22
Utilities for interpreting CSS from Stylers for formatting non-HTML outputs.
33
"""
4+
from __future__ import annotations
45

56
import re
6-
from typing import (
7-
Dict,
8-
Optional,
9-
)
107
import warnings
118

129

@@ -91,8 +88,8 @@ class CSSResolver:
9188
def __call__(
9289
self,
9390
declarations_str: str,
94-
inherited: Optional[Dict[str, str]] = None,
95-
) -> Dict[str, str]:
91+
inherited: dict[str, str] | None = None,
92+
) -> dict[str, str]:
9693
"""
9794
The given declarations to atomic properties.
9895
@@ -140,9 +137,9 @@ def __call__(
140137

141138
def _update_initial(
142139
self,
143-
props: Dict[str, str],
144-
inherited: Dict[str, str],
145-
) -> Dict[str, str]:
140+
props: dict[str, str],
141+
inherited: dict[str, str],
142+
) -> dict[str, str]:
146143
# 1. resolve inherited, initial
147144
for prop, val in inherited.items():
148145
if prop not in props:
@@ -162,9 +159,9 @@ def _update_initial(
162159

163160
def _update_font_size(
164161
self,
165-
props: Dict[str, str],
166-
inherited: Dict[str, str],
167-
) -> Dict[str, str]:
162+
props: dict[str, str],
163+
inherited: dict[str, str],
164+
) -> dict[str, str]:
168165
# 2. resolve relative font size
169166
if props.get("font-size"):
170167
props["font-size"] = self.size_to_pt(
@@ -174,7 +171,7 @@ def _update_font_size(
174171
)
175172
return props
176173

177-
def _get_font_size(self, props: Dict[str, str]) -> Optional[float]:
174+
def _get_font_size(self, props: dict[str, str]) -> float | None:
178175
if props.get("font-size"):
179176
font_size_string = props["font-size"]
180177
return self._get_float_font_size_from_pt(font_size_string)
@@ -184,7 +181,7 @@ def _get_float_font_size_from_pt(self, font_size_string: str) -> float:
184181
assert font_size_string.endswith("pt")
185182
return float(font_size_string.rstrip("pt"))
186183

187-
def _update_other_units(self, props: Dict[str, str]) -> Dict[str, str]:
184+
def _update_other_units(self, props: dict[str, str]) -> dict[str, str]:
188185
font_size = self._get_font_size(props)
189186
# 3. TODO: resolve other font-relative units
190187
for side in self.SIDES:

pandas/io/formats/excel.py

+29-33
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
11
"""
22
Utilities for conversion to writer-agnostic Excel representation.
33
"""
4+
from __future__ import annotations
45

56
from functools import reduce
67
import itertools
78
import re
89
from typing import (
910
Callable,
10-
Dict,
1111
Hashable,
1212
Iterable,
1313
Mapping,
14-
Optional,
1514
Sequence,
16-
Union,
1715
cast,
1816
)
1917
import warnings
@@ -61,8 +59,8 @@ def __init__(
6159
col: int,
6260
val,
6361
style=None,
64-
mergestart: Optional[int] = None,
65-
mergeend: Optional[int] = None,
62+
mergestart: int | None = None,
63+
mergeend: int | None = None,
6664
):
6765
self.row = row
6866
self.col = col
@@ -135,17 +133,17 @@ class CSSToExcelConverter:
135133
# and __call__ make use of instance attributes. We leave them as
136134
# instancemethods so that users can easily experiment with extensions
137135
# without monkey-patching.
138-
inherited: Optional[Dict[str, str]]
136+
inherited: dict[str, str] | None
139137

140-
def __init__(self, inherited: Optional[str] = None):
138+
def __init__(self, inherited: str | None = None):
141139
if inherited is not None:
142140
self.inherited = self.compute_css(inherited)
143141
else:
144142
self.inherited = None
145143

146144
compute_css = CSSResolver()
147145

148-
def __call__(self, declarations_str: str) -> Dict[str, Dict[str, str]]:
146+
def __call__(self, declarations_str: str) -> dict[str, dict[str, str]]:
149147
"""
150148
Convert CSS declarations to ExcelWriter style.
151149
@@ -165,7 +163,7 @@ def __call__(self, declarations_str: str) -> Dict[str, Dict[str, str]]:
165163
properties = self.compute_css(declarations_str, self.inherited)
166164
return self.build_xlstyle(properties)
167165

168-
def build_xlstyle(self, props: Mapping[str, str]) -> Dict[str, Dict[str, str]]:
166+
def build_xlstyle(self, props: Mapping[str, str]) -> dict[str, dict[str, str]]:
169167
out = {
170168
"alignment": self.build_alignment(props),
171169
"border": self.build_border(props),
@@ -176,7 +174,7 @@ def build_xlstyle(self, props: Mapping[str, str]) -> Dict[str, Dict[str, str]]:
176174

177175
# TODO: handle cell width and height: needs support in pandas.io.excel
178176

179-
def remove_none(d: Dict[str, str]) -> None:
177+
def remove_none(d: dict[str, str]) -> None:
180178
"""Remove key where value is None, through nested dicts"""
181179
for k, v in list(d.items()):
182180
if v is None:
@@ -189,30 +187,28 @@ def remove_none(d: Dict[str, str]) -> None:
189187
remove_none(out)
190188
return out
191189

192-
def build_alignment(
193-
self, props: Mapping[str, str]
194-
) -> Dict[str, Optional[Union[bool, str]]]:
190+
def build_alignment(self, props: Mapping[str, str]) -> dict[str, bool | str | None]:
195191
# TODO: text-indent, padding-left -> alignment.indent
196192
return {
197193
"horizontal": props.get("text-align"),
198194
"vertical": self._get_vertical_alignment(props),
199195
"wrap_text": self._get_is_wrap_text(props),
200196
}
201197

202-
def _get_vertical_alignment(self, props: Mapping[str, str]) -> Optional[str]:
198+
def _get_vertical_alignment(self, props: Mapping[str, str]) -> str | None:
203199
vertical_align = props.get("vertical-align")
204200
if vertical_align:
205201
return self.VERTICAL_MAP.get(vertical_align)
206202
return None
207203

208-
def _get_is_wrap_text(self, props: Mapping[str, str]) -> Optional[bool]:
204+
def _get_is_wrap_text(self, props: Mapping[str, str]) -> bool | None:
209205
if props.get("white-space") is None:
210206
return None
211207
return bool(props["white-space"] not in ("nowrap", "pre", "pre-line"))
212208

213209
def build_border(
214210
self, props: Mapping[str, str]
215-
) -> Dict[str, Dict[str, Optional[str]]]:
211+
) -> dict[str, dict[str, str | None]]:
216212
return {
217213
side: {
218214
"style": self._border_style(
@@ -224,7 +220,7 @@ def build_border(
224220
for side in ["top", "right", "bottom", "left"]
225221
}
226222

227-
def _border_style(self, style: Optional[str], width: Optional[str]):
223+
def _border_style(self, style: str | None, width: str | None):
228224
# convert styles and widths to openxml, one of:
229225
# 'dashDot'
230226
# 'dashDotDot'
@@ -263,7 +259,7 @@ def _border_style(self, style: Optional[str], width: Optional[str]):
263259
return "dashed"
264260
return "mediumDashed"
265261

266-
def _get_width_name(self, width_input: Optional[str]) -> Optional[str]:
262+
def _get_width_name(self, width_input: str | None) -> str | None:
267263
width = self._width_to_float(width_input)
268264
if width < 1e-5:
269265
return None
@@ -273,7 +269,7 @@ def _get_width_name(self, width_input: Optional[str]) -> Optional[str]:
273269
return "medium"
274270
return "thick"
275271

276-
def _width_to_float(self, width: Optional[str]) -> float:
272+
def _width_to_float(self, width: str | None) -> float:
277273
if width is None:
278274
width = "2pt"
279275
return self._pt_to_float(width)
@@ -289,12 +285,12 @@ def build_fill(self, props: Mapping[str, str]):
289285
if fill_color not in (None, "transparent", "none"):
290286
return {"fgColor": self.color_to_excel(fill_color), "patternType": "solid"}
291287

292-
def build_number_format(self, props: Mapping[str, str]) -> Dict[str, Optional[str]]:
288+
def build_number_format(self, props: Mapping[str, str]) -> dict[str, str | None]:
293289
return {"format_code": props.get("number-format")}
294290

295291
def build_font(
296292
self, props: Mapping[str, str]
297-
) -> Dict[str, Optional[Union[bool, int, float, str]]]:
293+
) -> dict[str, bool | int | float | str | None]:
298294
font_names = self._get_font_names(props)
299295
decoration = self._get_decoration(props)
300296
return {
@@ -316,13 +312,13 @@ def build_font(
316312
# 'condense': ,
317313
}
318314

319-
def _get_is_bold(self, props: Mapping[str, str]) -> Optional[bool]:
315+
def _get_is_bold(self, props: Mapping[str, str]) -> bool | None:
320316
weight = props.get("font-weight")
321317
if weight:
322318
return self.BOLD_MAP.get(weight)
323319
return None
324320

325-
def _get_is_italic(self, props: Mapping[str, str]) -> Optional[bool]:
321+
def _get_is_italic(self, props: Mapping[str, str]) -> bool | None:
326322
font_style = props.get("font-style")
327323
if font_style:
328324
return self.ITALIC_MAP.get(font_style)
@@ -335,12 +331,12 @@ def _get_decoration(self, props: Mapping[str, str]) -> Sequence[str]:
335331
else:
336332
return ()
337333

338-
def _get_underline(self, decoration: Sequence[str]) -> Optional[str]:
334+
def _get_underline(self, decoration: Sequence[str]) -> str | None:
339335
if "underline" in decoration:
340336
return "single"
341337
return None
342338

343-
def _get_shadow(self, props: Mapping[str, str]) -> Optional[bool]:
339+
def _get_shadow(self, props: Mapping[str, str]) -> bool | None:
344340
if "text-shadow" in props:
345341
return bool(re.search("^[^#(]*[1-9]", props["text-shadow"]))
346342
return None
@@ -371,13 +367,13 @@ def _get_font_names(self, props: Mapping[str, str]) -> Sequence[str]:
371367
font_names.append(name)
372368
return font_names
373369

374-
def _get_font_size(self, props: Mapping[str, str]) -> Optional[float]:
370+
def _get_font_size(self, props: Mapping[str, str]) -> float | None:
375371
size = props.get("font-size")
376372
if size is None:
377373
return size
378374
return self._pt_to_float(size)
379375

380-
def _select_font_family(self, font_names) -> Optional[int]:
376+
def _select_font_family(self, font_names) -> int | None:
381377
family = None
382378
for name in font_names:
383379
family = self.FAMILY_MAP.get(name)
@@ -386,7 +382,7 @@ def _select_font_family(self, font_names) -> Optional[int]:
386382

387383
return family
388384

389-
def color_to_excel(self, val: Optional[str]) -> Optional[str]:
385+
def color_to_excel(self, val: str | None) -> str | None:
390386
if val is None:
391387
return None
392388

@@ -463,14 +459,14 @@ def __init__(
463459
self,
464460
df,
465461
na_rep: str = "",
466-
float_format: Optional[str] = None,
467-
cols: Optional[Sequence[Hashable]] = None,
468-
header: Union[Sequence[Hashable], bool] = True,
462+
float_format: str | None = None,
463+
cols: Sequence[Hashable] | None = None,
464+
header: Sequence[Hashable] | bool = True,
469465
index: bool = True,
470-
index_label: Optional[IndexLabel] = None,
466+
index_label: IndexLabel | None = None,
471467
merge_cells: bool = False,
472468
inf_rep: str = "inf",
473-
style_converter: Optional[Callable] = None,
469+
style_converter: Callable | None = None,
474470
):
475471
self.rowcounter = 0
476472
self.na_rep = na_rep

0 commit comments

Comments
 (0)