From 980d0dac23af9e40051799389a0d5bf612f1db70 Mon Sep 17 00:00:00 2001 From: MomIsBestFriend <50263213+MomIsBestFriend@users.noreply.github.com> Date: Fri, 27 Dec 2019 18:02:50 +0200 Subject: [PATCH] TYP: Typing hints in pandas/io/formats/{css,csvs}.py (#30398) --- pandas/io/formats/css.py | 30 +++++++++++++--------- pandas/io/formats/csvs.py | 52 +++++++++++++++++++-------------------- 2 files changed, 45 insertions(+), 37 deletions(-) diff --git a/pandas/io/formats/css.py b/pandas/io/formats/css.py index 583dd49d4c66a..b40d2a57b8106 100644 --- a/pandas/io/formats/css.py +++ b/pandas/io/formats/css.py @@ -1,4 +1,5 @@ -"""Utilities for interpreting CSS from Stylers for formatting non-HTML outputs +""" +Utilities for interpreting CSS from Stylers for formatting non-HTML outputs. """ import re @@ -6,13 +7,15 @@ class CSSWarning(UserWarning): - """This CSS syntax cannot currently be parsed""" + """ + This CSS syntax cannot currently be parsed. + """ pass def _side_expander(prop_fmt: str): - def expand(self, prop, value): + def expand(self, prop, value: str): tokens = value.split() try: mapping = self.SIDE_SHORTHANDS[len(tokens)] @@ -28,12 +31,13 @@ def expand(self, prop, value): class CSSResolver: - """A callable for parsing and resolving CSS to atomic properties - + """ + A callable for parsing and resolving CSS to atomic properties. """ def __call__(self, declarations_str, inherited=None): - """ the given declarations to atomic properties + """ + The given declarations to atomic properties. Parameters ---------- @@ -46,8 +50,8 @@ def __call__(self, declarations_str, inherited=None): Returns ------- - props : dict - Atomic CSS 2.2 properties + dict + Atomic CSS 2.2 properties. Examples -------- @@ -69,7 +73,6 @@ def __call__(self, declarations_str, inherited=None): ('font-size', '24pt'), ('font-weight', 'bold')] """ - props = dict(self.atomize(self.parse(declarations_str))) if inherited is None: inherited = {} @@ -235,10 +238,15 @@ def atomize(self, declarations): expand_margin = _side_expander("margin-{:s}") expand_padding = _side_expander("padding-{:s}") - def parse(self, declarations_str): - """Generates (prop, value) pairs from declarations + def parse(self, declarations_str: str): + """ + Generates (prop, value) pairs from declarations. In a future version may generate parsed tokens from tinycss/tinycss2 + + Parameters + ---------- + declarations_str : str """ for decl in declarations_str.split(";"): if not decl.strip(): diff --git a/pandas/io/formats/csvs.py b/pandas/io/formats/csvs.py index 72ba1a892cb8f..0d581f30e50e7 100644 --- a/pandas/io/formats/csvs.py +++ b/pandas/io/formats/csvs.py @@ -5,13 +5,14 @@ import csv as csvlib from io import StringIO import os -from typing import List +from typing import Hashable, List, Mapping, Optional, Sequence, Union import warnings from zipfile import ZipFile import numpy as np from pandas._libs import writers as libwriters +from pandas._typing import FilePathOrBuffer from pandas.core.dtypes.generic import ( ABCDatetimeIndex, @@ -33,27 +34,26 @@ class CSVFormatter: def __init__( self, obj, - path_or_buf=None, - sep=",", - na_rep="", - float_format=None, + path_or_buf: Optional[FilePathOrBuffer[str]] = None, + sep: str = ",", + na_rep: str = "", + float_format: Optional[str] = None, cols=None, - header=True, - index=True, - index_label=None, - mode="w", - encoding=None, - compression="infer", - quoting=None, + header: Union[bool, Sequence[Hashable]] = True, + index: bool = True, + index_label: Optional[Union[bool, Hashable, Sequence[Hashable]]] = None, + mode: str = "w", + encoding: Optional[str] = None, + compression: Union[str, Mapping[str, str], None] = "infer", + quoting: Optional[int] = None, line_terminator="\n", - chunksize=None, + chunksize: Optional[int] = None, quotechar='"', - date_format=None, - doublequote=True, - escapechar=None, + date_format: Optional[str] = None, + doublequote: bool = True, + escapechar: Optional[str] = None, decimal=".", ): - self.obj = obj if path_or_buf is None: @@ -154,14 +154,17 @@ def __init__( if not index: self.nlevels = 0 - def save(self): + def save(self) -> None: """ - Create the writer & save + Create the writer & save. """ # GH21227 internal compression is not used when file-like passed. if self.compression and hasattr(self.path_or_buf, "write"): - msg = "compression has no effect when passing file-like object as input." - warnings.warn(msg, RuntimeWarning, stacklevel=2) + warnings.warn( + "compression has no effect when passing file-like object as input.", + RuntimeWarning, + stacklevel=2, + ) # when zip compression is called. is_zip = isinstance(self.path_or_buf, ZipFile) or ( @@ -223,7 +226,6 @@ def save(self): _fh.close() def _save_header(self): - writer = self.writer obj = self.obj index_label = self.index_label @@ -303,8 +305,7 @@ def _save_header(self): encoded_labels.extend([""] * len(columns)) writer.writerow(encoded_labels) - def _save(self): - + def _save(self) -> None: self._save_header() nrows = len(self.data_index) @@ -321,8 +322,7 @@ def _save(self): self._save_chunk(start_i, end_i) - def _save_chunk(self, start_i: int, end_i: int): - + def _save_chunk(self, start_i: int, end_i: int) -> None: data_index = self.data_index # create the data for a chunk