Skip to content

Commit 980d0da

Browse files
ShaharNavehjreback
authored andcommitted
TYP: Typing hints in pandas/io/formats/{css,csvs}.py (#30398)
1 parent 698f689 commit 980d0da

File tree

2 files changed

+45
-37
lines changed

2 files changed

+45
-37
lines changed

pandas/io/formats/css.py

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
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+
def expand(self, prop, value: str):
1619
tokens = value.split()
1720
try:
1821
mapping = self.SIDE_SHORTHANDS[len(tokens)]
@@ -28,12 +31,13 @@ def expand(self, prop, value):
2831

2932

3033
class CSSResolver:
31-
"""A callable for parsing and resolving CSS to atomic properties
32-
34+
"""
35+
A callable for parsing and resolving CSS to atomic properties.
3336
"""
3437

3538
def __call__(self, declarations_str, inherited=None):
36-
""" the given declarations to atomic properties
39+
"""
40+
The given declarations to atomic properties.
3741
3842
Parameters
3943
----------
@@ -46,8 +50,8 @@ def __call__(self, declarations_str, inherited=None):
4650
4751
Returns
4852
-------
49-
props : dict
50-
Atomic CSS 2.2 properties
53+
dict
54+
Atomic CSS 2.2 properties.
5155
5256
Examples
5357
--------
@@ -69,7 +73,6 @@ def __call__(self, declarations_str, inherited=None):
6973
('font-size', '24pt'),
7074
('font-weight', 'bold')]
7175
"""
72-
7376
props = dict(self.atomize(self.parse(declarations_str)))
7477
if inherited is None:
7578
inherited = {}
@@ -235,10 +238,15 @@ def atomize(self, declarations):
235238
expand_margin = _side_expander("margin-{:s}")
236239
expand_padding = _side_expander("padding-{:s}")
237240

238-
def parse(self, declarations_str):
239-
"""Generates (prop, value) pairs from declarations
241+
def parse(self, declarations_str: str):
242+
"""
243+
Generates (prop, value) pairs from declarations.
240244
241245
In a future version may generate parsed tokens from tinycss/tinycss2
246+
247+
Parameters
248+
----------
249+
declarations_str : str
242250
"""
243251
for decl in declarations_str.split(";"):
244252
if not decl.strip():

pandas/io/formats/csvs.py

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@
55
import csv as csvlib
66
from io import StringIO
77
import os
8-
from typing import List
8+
from typing import Hashable, List, Mapping, Optional, Sequence, Union
99
import warnings
1010
from zipfile import ZipFile
1111

1212
import numpy as np
1313

1414
from pandas._libs import writers as libwriters
15+
from pandas._typing import FilePathOrBuffer
1516

1617
from pandas.core.dtypes.generic import (
1718
ABCDatetimeIndex,
@@ -33,27 +34,26 @@ class CSVFormatter:
3334
def __init__(
3435
self,
3536
obj,
36-
path_or_buf=None,
37-
sep=",",
38-
na_rep="",
39-
float_format=None,
37+
path_or_buf: Optional[FilePathOrBuffer[str]] = None,
38+
sep: str = ",",
39+
na_rep: str = "",
40+
float_format: Optional[str] = None,
4041
cols=None,
41-
header=True,
42-
index=True,
43-
index_label=None,
44-
mode="w",
45-
encoding=None,
46-
compression="infer",
47-
quoting=None,
42+
header: Union[bool, Sequence[Hashable]] = True,
43+
index: bool = True,
44+
index_label: Optional[Union[bool, Hashable, Sequence[Hashable]]] = None,
45+
mode: str = "w",
46+
encoding: Optional[str] = None,
47+
compression: Union[str, Mapping[str, str], None] = "infer",
48+
quoting: Optional[int] = None,
4849
line_terminator="\n",
49-
chunksize=None,
50+
chunksize: Optional[int] = None,
5051
quotechar='"',
51-
date_format=None,
52-
doublequote=True,
53-
escapechar=None,
52+
date_format: Optional[str] = None,
53+
doublequote: bool = True,
54+
escapechar: Optional[str] = None,
5455
decimal=".",
5556
):
56-
5757
self.obj = obj
5858

5959
if path_or_buf is None:
@@ -154,14 +154,17 @@ def __init__(
154154
if not index:
155155
self.nlevels = 0
156156

157-
def save(self):
157+
def save(self) -> None:
158158
"""
159-
Create the writer & save
159+
Create the writer & save.
160160
"""
161161
# GH21227 internal compression is not used when file-like passed.
162162
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)
163+
warnings.warn(
164+
"compression has no effect when passing file-like object as input.",
165+
RuntimeWarning,
166+
stacklevel=2,
167+
)
165168

166169
# when zip compression is called.
167170
is_zip = isinstance(self.path_or_buf, ZipFile) or (
@@ -223,7 +226,6 @@ def save(self):
223226
_fh.close()
224227

225228
def _save_header(self):
226-
227229
writer = self.writer
228230
obj = self.obj
229231
index_label = self.index_label
@@ -303,8 +305,7 @@ def _save_header(self):
303305
encoded_labels.extend([""] * len(columns))
304306
writer.writerow(encoded_labels)
305307

306-
def _save(self):
307-
308+
def _save(self) -> None:
308309
self._save_header()
309310

310311
nrows = len(self.data_index)
@@ -321,8 +322,7 @@ def _save(self):
321322

322323
self._save_chunk(start_i, end_i)
323324

324-
def _save_chunk(self, start_i: int, end_i: int):
325-
325+
def _save_chunk(self, start_i: int, end_i: int) -> None:
326326
data_index = self.data_index
327327

328328
# create the data for a chunk

0 commit comments

Comments
 (0)