Skip to content

TYPING: check-untyped-defs for io.formats #28129

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

Closed
wants to merge 8 commits into from
6 changes: 4 additions & 2 deletions pandas/io/formats/console.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ def check_main():
return not hasattr(main, "__file__") or get_option("mode.sim_interactive")

try:
return __IPYTHON__ or check_main() # noqa
# error: Name '__IPYTHON__' is not defined
return __IPYTHON__ or check_main() # type:ignore # noqa
except NameError:
return check_main()

Expand All @@ -75,7 +76,8 @@ def in_ipython_frontend():
check if we're inside an an IPython zmq frontend
"""
try:
ip = get_ipython() # noqa
# error: Name 'get_ipython' is not defined
ip = get_ipython() # type:ignore # noqa
return "zmq" in str(type(ip)).lower()
except NameError:
pass
Expand Down
10 changes: 6 additions & 4 deletions pandas/io/formats/css.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"""

import re
from typing import Optional
import warnings


Expand Down Expand Up @@ -87,7 +88,7 @@ def __call__(self, declarations_str, inherited=None):
props["font-size"], em_pt, conversions=self.FONT_SIZE_RATIOS
)

font_size = float(props["font-size"][:-2])
font_size = float(props["font-size"][:-2]) # type: Optional[float]
else:
font_size = None

Expand Down Expand Up @@ -159,9 +160,10 @@ def _error():
warnings.warn("Unhandled size: {val!r}".format(val=in_val), CSSWarning)
return self.size_to_pt("1!!default", conversions=conversions)

try:
val, unit = re.match(r"^(\S*?)([a-zA-Z%!].*)", in_val).groups()
except AttributeError:
match = re.match(r"^(\S*?)([a-zA-Z%!].*)", in_val)
if match:
val, unit = match.groups()
else:
return _error()
if val == "":
# hack for 'large' etc.
Expand Down
3 changes: 2 additions & 1 deletion pandas/io/formats/csvs.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import csv as csvlib
from io import StringIO
import os
from typing import List
import warnings
from zipfile import ZipFile

Expand Down Expand Up @@ -233,7 +234,7 @@ def _save_header(self):
cols = self.cols
has_mi_columns = self.has_mi_columns
header = self.header
encoded_labels = []
encoded_labels = [] # type: List[str]

has_aliases = isinstance(header, (tuple, list, np.ndarray, ABCIndexClass))
if not (has_aliases or self.header):
Expand Down
3 changes: 2 additions & 1 deletion pandas/io/formats/excel.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from functools import reduce
import itertools
import re
from typing import Iterable
import warnings

import numpy as np
Expand Down Expand Up @@ -528,7 +529,7 @@ def _format_header(self):
else:
gen = self._format_header_regular()

gen2 = ()
gen2 = () # type: Iterable[ExcelCell]
if self.df.index.names:
row = [x if x is not None else "" for x in self.df.index.names] + [
""
Expand Down
5 changes: 3 additions & 2 deletions pandas/io/formats/format.py
Original file line number Diff line number Diff line change
Expand Up @@ -1232,7 +1232,7 @@ class FloatArrayFormatter(GenericArrayFormatter):
"""

def __init__(self, *args, **kwargs):
GenericArrayFormatter.__init__(self, *args, **kwargs)
super().__init__(*args, **kwargs)

# float_format is expected to be a string
# formatter should be used to pass a function
Expand Down Expand Up @@ -1709,7 +1709,8 @@ def _make_fixed_width(

def just(x):
if conf_max is not None:
if (conf_max > 3) & (adj.len(x) > max_len):
# https://github.com/python/mypy/issues/2608
if (conf_max > 3) & (adj.len(x) > max_len): # type: ignore
x = x[: max_len - 3] + "..."
return x

Expand Down
29 changes: 22 additions & 7 deletions pandas/io/formats/style.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import copy
from functools import partial
from itertools import product
from typing import Any, Callable, DefaultDict, Dict, List, Optional, Tuple
from uuid import uuid1

import numpy as np
Expand All @@ -21,10 +22,11 @@
from pandas.core.dtypes.generic import ABCSeries

import pandas as pd
from pandas._typing import Axis, FrameOrSeries
from pandas.api.types import is_dict_like, is_list_like
import pandas.core.common as com
from pandas.core.generic import _shared_docs
from pandas.core.indexing import _maybe_numeric_slice, _non_reducing_slice
from pandas.core.indexing import _IndexSlice, _maybe_numeric_slice, _non_reducing_slice

jinja2 = import_optional_dependency("jinja2", extra="DataFrame.style requires jinja2.")

Expand All @@ -47,6 +49,11 @@ def _mpl(func):
raise ImportError(no_mpl_message.format(func.__name__))


_ApplyArgs = Tuple[
Copy link
Member

Choose a reason for hiding this comment

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

I assume this is a function of the code itself and not necessarily the annotation, but I find this pretty tough to digest. Is it documented already somewhere how this works? If not can you comment?

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'd rather not in this pass. I don't know this module well enough yet.

This pass is just check-untyped-defs as "Working through this flag in advance may lead to less surprises as annotations are added" xref #27568

Also the sooner we resolve these issues, the sooner we can enable this setting in CI to ensure that code modified in functions without annotations is checked for PRs.

Callable[[FrameOrSeries], FrameOrSeries], Axis, Optional[_IndexSlice]
]


class Styler:
"""
Helps style a DataFrame or Series according to the data with HTML and CSS.
Expand Down Expand Up @@ -122,8 +129,8 @@ def __init__(
table_attributes=None,
cell_ids=True,
):
self.ctx = defaultdict(list)
self._todo = []
self.ctx = defaultdict(list) # type: DefaultDict[Tuple[int, int], List[str]]
self._todo = [] # type: List[Tuple[Callable, _ApplyArgs, Dict[str, Any]]]

if not isinstance(data, (pd.Series, pd.DataFrame)):
raise TypeError("``data`` must be a Series or DataFrame")
Expand All @@ -144,7 +151,7 @@ def __init__(
self.precision = precision
self.table_attributes = table_attributes
self.hidden_index = False
self.hidden_columns = []
self.hidden_columns = [] # type: List[int]
self.cell_ids = cell_ids

# display_funcs maps (row, col) -> formatting function
Expand All @@ -155,7 +162,9 @@ def default_display_func(x):
else:
return x

self._display_funcs = defaultdict(lambda: default_display_func)
self._display_funcs = defaultdict(
lambda: default_display_func
) # type: DefaultDict[Tuple[int, int], Callable]

def _repr_html_(self):
"""
Expand Down Expand Up @@ -244,7 +253,7 @@ def format_attr(pair):
idx_lengths = _get_level_lengths(self.index)
col_lengths = _get_level_lengths(self.columns, hidden_columns)

cell_context = dict()
cell_context = dict() # type: Dict[str, Dict]

n_rlvls = self.data.index.nlevels
n_clvls = self.data.columns.nlevels
Expand Down Expand Up @@ -626,7 +635,13 @@ def _apply(self, func, axis=0, subset=None, **kwargs):
self._update_ctx(result)
return self

def apply(self, func, axis=0, subset=None, **kwargs):
def apply(
self,
func: Callable[[FrameOrSeries], FrameOrSeries],
axis: Axis = 0,
subset: Optional[_IndexSlice] = None,
**kwargs
) -> "Styler":
"""
Apply a function column-wise, row-wise, or table-wise,
updating the HTML representation with the result.
Expand Down