Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TYP: Typing hints in pandas/io/formats/{css,csvs}.py #30398

Merged
merged 3 commits into from
Dec 27, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 19 additions & 11 deletions pandas/io/formats/css.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
"""Utilities for interpreting CSS from Stylers for formatting non-HTML outputs
"""
Utilities for interpreting CSS from Stylers for formatting non-HTML outputs.
"""

import re
import warnings


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)]
Expand All @@ -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
----------
Expand All @@ -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
--------
Expand All @@ -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 = {}
Expand Down Expand Up @@ -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():
Expand Down
52 changes: 26 additions & 26 deletions pandas/io/formats/csvs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
cols=None,
cols: Sequence[Hashable] = None,

Copy link
Member Author

@ShaharNaveh ShaharNaveh Dec 24, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any suggestions?


mypy's log for 3 diffrent annotations:

cols: Sequence[Hashable] = None
$ mypy pandas
pandas/io/formats/csvs.py:34: error: Incompatible default for argument "cols" (default has type "None", argument has type "Sequence[Hashable]")
pandas/core/generic.py:3171: error: Argument "cols" to "CSVFormatter" has incompatible type "Optional[Sequence[Optional[Hashable]]]"; expected "Sequence[Hashable]"
Found 2 errors in 2 files (checked 816 source files)

cols: Optional[Sequence[Hashable]] = None
$ mypy pandas
pandas/io/formats/csvs.py:128: error: Argument 1 to "list" has incompatible type "Optional[Sequence[Hashable]]"; expected "Iterable[Hashable]"
pandas/io/formats/csvs.py:139: error: Argument 1 to "len" has incompatible type "Optional[Sequence[Hashable]]"; expected "Sized"
pandas/core/generic.py:3171: error: Argument "cols" to "CSVFormatter" has incompatible type "Optional[Sequence[Optional[Hashable]]]"; expected "Optional[Sequence[Hashable]]"
Found 3 errors in 2 files (checked 816 source files)

cols: Optional[Sequence[Optional[Hashable]]] = None
$ mypy pandas
pandas/io/formats/csvs.py:128: error: Argument 1 to "list" has incompatible type "Optional[Sequence[Optional[Hashable]]]"; expected "Iterable[Optional[Hashable]]"
pandas/io/formats/csvs.py:139: error: Argument 1 to "len" has incompatible type "Optional[Sequence[Optional[Hashable]]]"; expected "Sized"
Found 2 errors in 2 files (checked 816 source files)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like the annotations for columns might not be consistent in the IO methods. Can you open up a follow up issue for this and just leave unannotated for now? I think might be more difficult so OK to branch off given there's already a good deal in this PR

header=True,
index=True,
index_label=None,
mode="w",
encoding=None,
compression="infer",
quoting=None,
header: Union[bool, Sequence[Hashable]] = True,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

question for @WillAyd - do we want Sequence[Optional[Hashable]]] or is this for readability and only add Optional when mypy complains?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Personal preference for only when complaining but it isn't strong if you feel otherwise

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fine with that. (although may need to make exception for public apis)

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",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
compression: Union[str, Mapping[str, str], None] = "infer",
compression: Optional[Union[str, Mapping[str, str]]] = "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:
Expand Down Expand Up @@ -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 (
Expand Down Expand Up @@ -223,7 +226,6 @@ def save(self):
_fh.close()

def _save_header(self):

writer = self.writer
obj = self.obj
index_label = self.index_label
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down