Skip to content

Commit c2d3472

Browse files
author
MomIsBestFriend
committed
TYP: Typing hints in pandas/io/formats/{css,csvs}.py
1 parent 3577b5a commit c2d3472

File tree

2 files changed

+61
-38
lines changed

2 files changed

+61
-38
lines changed

pandas/io/formats/css.py

+35-13
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,39 @@
1-
"""Utilities for interpreting CSS from Stylers for formatting non-HTML outputs
1+
"""
2+
Utilities for interpreting CSS from Stylers for formatting non-HTML outputs.
23
"""
34

45
import re
56
import warnings
67

78

89
class CSSWarning(UserWarning):
9-
"""This CSS syntax cannot currently be parsed"""
10+
"""
11+
This CSS syntax cannot currently be parsed.
12+
"""
1013

1114
pass
1215

1316

1417
def _side_expander(prop_fmt: str):
15-
def expand(self, prop, value):
18+
"""
19+
Parameters
20+
----------
21+
prop_fmt : str
22+
"""
23+
24+
def expand(self, prop, value: str):
25+
"""
26+
Parameters
27+
----------
28+
prop
29+
value : str
30+
"""
1631
tokens = value.split()
1732
try:
1833
mapping = self.SIDE_SHORTHANDS[len(tokens)]
1934
except KeyError:
2035
warnings.warn(
21-
f'Could not expand "{prop}: {value}"', CSSWarning,
36+
f"Could not expand {prop}: {value}", CSSWarning,
2237
)
2338
return
2439
for key, idx in zip(self.SIDES, mapping):
@@ -28,12 +43,13 @@ def expand(self, prop, value):
2843

2944

3045
class CSSResolver:
31-
"""A callable for parsing and resolving CSS to atomic properties
32-
46+
"""
47+
A callable for parsing and resolving CSS to atomic properties.
3348
"""
3449

3550
def __call__(self, declarations_str, inherited=None):
36-
""" the given declarations to atomic properties
51+
"""
52+
The given declarations to atomic properties.
3753
3854
Parameters
3955
----------
@@ -46,8 +62,8 @@ def __call__(self, declarations_str, inherited=None):
4662
4763
Returns
4864
-------
49-
props : dict
50-
Atomic CSS 2.2 properties
65+
dict
66+
Atomic CSS 2.2 properties.
5167
5268
Examples
5369
--------
@@ -69,7 +85,6 @@ def __call__(self, declarations_str, inherited=None):
6985
('font-size', '24pt'),
7086
('font-weight', 'bold')]
7187
"""
72-
7388
props = dict(self.atomize(self.parse(declarations_str)))
7489
if inherited is None:
7590
inherited = {}
@@ -172,7 +187,9 @@ def __call__(self, declarations_str, inherited=None):
172187

173188
def size_to_pt(self, in_val, em_pt=None, conversions=UNIT_RATIOS):
174189
def _error():
175-
warnings.warn(f"Unhandled size: {repr(in_val)}", CSSWarning)
190+
warnings.warn(
191+
f"Unhandled size: {repr(in_val)}", CSSWarning,
192+
)
176193
return self.size_to_pt("1!!default", conversions=conversions)
177194

178195
try:
@@ -235,10 +252,15 @@ def atomize(self, declarations):
235252
expand_margin = _side_expander("margin-{:s}")
236253
expand_padding = _side_expander("padding-{:s}")
237254

238-
def parse(self, declarations_str):
239-
"""Generates (prop, value) pairs from declarations
255+
def parse(self, declarations_str: str):
256+
"""
257+
Generates (prop, value) pairs from declarations.
240258
241259
In a future version may generate parsed tokens from tinycss/tinycss2
260+
261+
Parameters
262+
----------
263+
declarations_str : str
242264
"""
243265
for decl in declarations_str.split(";"):
244266
if not decl.strip():

pandas/io/formats/csvs.py

+26-25
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import csv as csvlib
66
from io import StringIO
77
import os
8-
from typing import List
8+
from typing import List, Optional, Sequence, Union
99
import warnings
1010
from zipfile import ZipFile
1111

@@ -21,6 +21,8 @@
2121
)
2222
from pandas.core.dtypes.missing import notna
2323

24+
from pandas._typing import FilePathOrBuffer
25+
2426
from pandas.io.common import (
2527
get_compression_method,
2628
get_filepath_or_buffer,
@@ -33,27 +35,26 @@ class CSVFormatter:
3335
def __init__(
3436
self,
3537
obj,
36-
path_or_buf=None,
37-
sep=",",
38-
na_rep="",
38+
path_or_buf: Optional[FilePathOrBuffer[str]] = None,
39+
sep: str = ",",
40+
na_rep: str = "",
3941
float_format=None,
4042
cols=None,
41-
header=True,
42-
index=True,
43+
header: Union[bool, Sequence[str]] = True,
44+
index: bool = True,
4345
index_label=None,
44-
mode="w",
45-
encoding=None,
46+
mode: str = "w",
47+
encoding: Optional[str] = None,
4648
compression="infer",
47-
quoting=None,
48-
line_terminator="\n",
49-
chunksize=None,
50-
quotechar='"',
51-
date_format=None,
52-
doublequote=True,
49+
quoting: Optional[int] = None,
50+
line_terminator: Optional[str] = "\n",
51+
chunksize: Optional[int] = None,
52+
quotechar: Optional[str] = '"',
53+
date_format: Optional[str] = None,
54+
doublequote: bool = True,
5355
escapechar=None,
54-
decimal=".",
56+
decimal: Optional[str] = ".",
5557
):
56-
5758
self.obj = obj
5859

5960
if path_or_buf is None:
@@ -154,14 +155,17 @@ def __init__(
154155
if not index:
155156
self.nlevels = 0
156157

157-
def save(self):
158+
def save(self) -> None:
158159
"""
159-
Create the writer & save
160+
Create the writer & save.
160161
"""
161162
# GH21227 internal compression is not used when file-like passed.
162163
if self.compression and hasattr(self.path_or_buf, "write"):
163-
msg = "compression has no effect when passing file-like object as input."
164-
warnings.warn(msg, RuntimeWarning, stacklevel=2)
164+
warnings.warn(
165+
"compression has no effect when passing file-like object as input.",
166+
RuntimeWarning,
167+
stacklevel=2,
168+
)
165169

166170
# when zip compression is called.
167171
is_zip = isinstance(self.path_or_buf, ZipFile) or (
@@ -223,7 +227,6 @@ def save(self):
223227
_fh.close()
224228

225229
def _save_header(self):
226-
227230
writer = self.writer
228231
obj = self.obj
229232
index_label = self.index_label
@@ -306,8 +309,7 @@ def _save_header(self):
306309
encoded_labels.extend([""] * len(columns))
307310
writer.writerow(encoded_labels)
308311

309-
def _save(self):
310-
312+
def _save(self) -> None:
311313
self._save_header()
312314

313315
nrows = len(self.data_index)
@@ -324,8 +326,7 @@ def _save(self):
324326

325327
self._save_chunk(start_i, end_i)
326328

327-
def _save_chunk(self, start_i: int, end_i: int):
328-
329+
def _save_chunk(self, start_i: int, end_i: int) -> None:
329330
data_index = self.data_index
330331

331332
# create the data for a chunk

0 commit comments

Comments
 (0)