Skip to content

add type annotations to io\formats\html.py #27355

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

Merged
merged 3 commits into from
Jul 12, 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
1 change: 0 additions & 1 deletion pandas/io/formats/format.py
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,6 @@ def _get_adjustment():

class TableFormatter:

is_truncated = False
show_dimensions = None

@property
Expand Down
98 changes: 59 additions & 39 deletions pandas/io/formats/html.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,20 @@

from collections import OrderedDict
from textwrap import dedent
from typing import Dict, List, Optional, Tuple, Union

from pandas._config import get_option

from pandas.core.dtypes.generic import ABCMultiIndex
from pandas.core.dtypes.generic import ABCIndex, ABCMultiIndex

from pandas import option_context

from pandas.io.common import _is_url
from pandas.io.formats.format import TableFormatter, get_level_lengths
from pandas.io.formats.format import (
DataFrameFormatter,
TableFormatter,
get_level_lengths,
)
from pandas.io.formats.printing import pprint_thing


Expand All @@ -28,13 +33,18 @@ class HTMLFormatter(TableFormatter):

indent_delta = 2

def __init__(self, formatter, classes=None, border=None):
def __init__(
self,
formatter: DataFrameFormatter,
classes: Optional[Union[str, List, Tuple]] = None,
border: Optional[bool] = None,
) -> None:
self.fmt = formatter
self.classes = classes

self.frame = self.fmt.frame
self.columns = self.fmt.tr_frame.columns
self.elements = []
self.elements = [] # type: List[str]
self.bold_rows = self.fmt.kwds.get("bold_rows", False)
self.escape = self.fmt.kwds.get("escape", True)
self.show_dimensions = self.fmt.show_dimensions
Expand All @@ -47,15 +57,15 @@ def __init__(self, formatter, classes=None, border=None):
self.fmt.col_space = "{colspace}px".format(colspace=self.fmt.col_space)

@property
def show_row_idx_names(self):
def show_row_idx_names(self) -> bool:
return self.fmt.show_row_idx_names

@property
def show_col_idx_names(self):
def show_col_idx_names(self) -> bool:
return self.fmt.show_col_idx_names

@property
def row_levels(self):
def row_levels(self) -> int:
if self.fmt.index:
# showing (row) index
return self.frame.index.nlevels
Expand All @@ -69,22 +79,24 @@ def row_levels(self):
# not showing (row) index
return 0

def _get_columns_formatted_values(self):
def _get_columns_formatted_values(self) -> ABCIndex:
return self.columns

@property
def is_truncated(self):
def is_truncated(self) -> bool:
return self.fmt.is_truncated

@property
def ncols(self):
def ncols(self) -> int:
return len(self.fmt.tr_frame.columns)

def write(self, s, indent=0):
def write(self, s: str, indent: int = 0) -> None:
rs = pprint_thing(s)
self.elements.append(" " * indent + rs)

def write_th(self, s, header=False, indent=0, tags=None):
def write_th(
self, s: str, header: bool = False, indent: int = 0, tags: Optional[str] = None
) -> None:
"""
Method for writting a formatted <th> cell.

Expand All @@ -111,20 +123,24 @@ def write_th(self, s, header=False, indent=0, tags=None):
tags = tags or ""
tags += 'style="min-width: {colspace};"'.format(colspace=self.fmt.col_space)

return self._write_cell(s, kind="th", indent=indent, tags=tags)
self._write_cell(s, kind="th", indent=indent, tags=tags)

def write_td(self, s, indent=0, tags=None):
return self._write_cell(s, kind="td", indent=indent, tags=tags)
def write_td(self, s: str, indent: int = 0, tags: Optional[str] = None) -> None:
self._write_cell(s, kind="td", indent=indent, tags=tags)

def _write_cell(self, s, kind="td", indent=0, tags=None):
def _write_cell(
self, s: str, kind: str = "td", indent: int = 0, tags: Optional[str] = None
) -> None:
if tags is not None:
start_tag = "<{kind} {tags}>".format(kind=kind, tags=tags)
else:
start_tag = "<{kind}>".format(kind=kind)

if self.escape:
# escape & first to prevent double escaping of &
esc = OrderedDict([("&", r"&amp;"), ("<", r"&lt;"), (">", r"&gt;")])
esc = OrderedDict(
[("&", r"&amp;"), ("<", r"&lt;"), (">", r"&gt;")]
) # type: Union[OrderedDict[str, str], Dict]
Copy link
Member

Choose a reason for hiding this comment

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

I think also OK to just make this an ordereddict all of the time; simplifies annotation and code

Copy link
Member Author

Choose a reason for hiding this comment

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

i was getting pandas\io\formats\html.py:145: error: Incompatible types in assignment (expression has type "Dict[<nothing>, <nothing>]", variable has type "OrderedDict[str, str]")

Copy link
Member

Choose a reason for hiding this comment

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

Yea I so meant you could change the dict on line 145 to be an OrderedDict to match the signature here. I think OK to refactor code if it simplifies annotations and keeps same functionality, as there probably helps readability too

Though this is obviously fine as is too!

else:
esc = {}

Expand All @@ -146,14 +162,14 @@ def _write_cell(self, s, kind="td", indent=0, tags=None):

def write_tr(
self,
line,
indent=0,
indent_delta=0,
header=False,
align=None,
tags=None,
nindex_levels=0,
):
line: List[str],
indent: int = 0,
indent_delta: int = 0,
header: bool = False,
align: Optional[str] = None,
tags: Optional[Dict[int, str]] = None,
nindex_levels: int = 0,
) -> None:
if tags is None:
tags = {}

Expand All @@ -173,7 +189,7 @@ def write_tr(
indent -= indent_delta
self.write("</tr>", indent)

def render(self):
def render(self) -> List[str]:
self._write_table()

if self.should_show_dimensions:
Expand All @@ -186,7 +202,7 @@ def render(self):

return self.elements

def _write_table(self, indent=0):
def _write_table(self, indent: int = 0) -> None:
_classes = ["dataframe"] # Default class.
use_mathjax = get_option("display.html.use_mathjax")
if not use_mathjax:
Expand Down Expand Up @@ -220,7 +236,7 @@ def _write_table(self, indent=0):

self.write("</table>", indent)

def _write_col_header(self, indent):
def _write_col_header(self, indent: int) -> None:
truncate_h = self.fmt.truncate_h
if isinstance(self.columns, ABCMultiIndex):
template = 'colspan="{span:d}" halign="left"'
Expand Down Expand Up @@ -337,14 +353,14 @@ def _write_col_header(self, indent):

self.write_tr(row, indent, self.indent_delta, header=True, align=align)

def _write_row_header(self, indent):
def _write_row_header(self, indent: int) -> None:
truncate_h = self.fmt.truncate_h
row = [x if x is not None else "" for x in self.frame.index.names] + [""] * (
self.ncols + (1 if truncate_h else 0)
)
self.write_tr(row, indent, self.indent_delta, header=True)

def _write_header(self, indent):
def _write_header(self, indent: int) -> None:
self.write("<thead>", indent)

if self.fmt.header:
Expand All @@ -355,12 +371,12 @@ def _write_header(self, indent):

self.write("</thead>", indent)

def _get_formatted_values(self):
def _get_formatted_values(self) -> Dict[int, List[str]]:
with option_context("display.max_colwidth", 999999):
fmt_values = {i: self.fmt._format_col(i) for i in range(self.ncols)}
return fmt_values

def _write_body(self, indent):
def _write_body(self, indent: int) -> None:
self.write("<tbody>", indent)
fmt_values = self._get_formatted_values()

Expand All @@ -372,7 +388,9 @@ def _write_body(self, indent):

self.write("</tbody>", indent)

def _write_regular_rows(self, fmt_values, indent):
def _write_regular_rows(
self, fmt_values: Dict[int, List[str]], indent: int
) -> None:
truncate_h = self.fmt.truncate_h
truncate_v = self.fmt.truncate_v

Expand All @@ -385,7 +403,7 @@ def _write_regular_rows(self, fmt_values, indent):
else:
index_values = self.fmt.tr_frame.index.format()

row = []
row = [] # type: List[str]
for i in range(nrows):

if truncate_v and i == (self.fmt.tr_row_num):
Expand Down Expand Up @@ -416,7 +434,9 @@ def _write_regular_rows(self, fmt_values, indent):
row, indent, self.indent_delta, tags=None, nindex_levels=self.row_levels
)

def _write_hierarchical_rows(self, fmt_values, indent):
def _write_hierarchical_rows(
self, fmt_values: Dict[int, List[str]], indent: int
) -> None:
template = 'rowspan="{span}" valign="top"'

truncate_h = self.fmt.truncate_h
Expand Down Expand Up @@ -546,13 +566,13 @@ class NotebookFormatter(HTMLFormatter):
DataFrame._repr_html_() and DataFrame.to_html(notebook=True)
"""

def _get_formatted_values(self):
def _get_formatted_values(self) -> Dict[int, List[str]]:
return {i: self.fmt._format_col(i) for i in range(self.ncols)}

def _get_columns_formatted_values(self):
def _get_columns_formatted_values(self) -> List[str]:
return self.columns.format()

def write_style(self):
def write_style(self) -> None:
# We use the "scoped" attribute here so that the desired
# style properties for the data frame are not then applied
# throughout the entire notebook.
Expand Down Expand Up @@ -580,7 +600,7 @@ def write_style(self):
template = dedent("\n".join((template_first, template_mid, template_last)))
self.write(template)

def render(self):
def render(self) -> List[str]:
self.write("<div>")
self.write_style()
super().render()
Expand Down