diff --git a/pandas/io/formats/console.py b/pandas/io/formats/console.py index 7f8f2fbea2352..757ca86855cbe 100644 --- a/pandas/io/formats/console.py +++ b/pandas/io/formats/console.py @@ -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() @@ -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 diff --git a/pandas/io/formats/css.py b/pandas/io/formats/css.py index 92fe87cddb35b..690762433b456 100644 --- a/pandas/io/formats/css.py +++ b/pandas/io/formats/css.py @@ -2,6 +2,7 @@ """ import re +from typing import Optional import warnings @@ -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 @@ -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. diff --git a/pandas/io/formats/csvs.py b/pandas/io/formats/csvs.py index e25862537cbfc..42968d49dd6a8 100644 --- a/pandas/io/formats/csvs.py +++ b/pandas/io/formats/csvs.py @@ -5,6 +5,7 @@ import csv as csvlib from io import StringIO import os +from typing import List import warnings from zipfile import ZipFile @@ -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): diff --git a/pandas/io/formats/excel.py b/pandas/io/formats/excel.py index b9c847ad64c57..79447151351ca 100644 --- a/pandas/io/formats/excel.py +++ b/pandas/io/formats/excel.py @@ -4,6 +4,7 @@ from functools import reduce import itertools import re +from typing import Iterable import warnings import numpy as np @@ -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] + [ "" diff --git a/pandas/io/formats/format.py b/pandas/io/formats/format.py index 8ff4b9bda0430..87d40708cc63e 100644 --- a/pandas/io/formats/format.py +++ b/pandas/io/formats/format.py @@ -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 @@ -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 diff --git a/pandas/io/formats/style.py b/pandas/io/formats/style.py index 033d93d1456c8..d993648e97927 100644 --- a/pandas/io/formats/style.py +++ b/pandas/io/formats/style.py @@ -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 @@ -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.") @@ -47,6 +49,11 @@ def _mpl(func): raise ImportError(no_mpl_message.format(func.__name__)) +_ApplyArgs = Tuple[ + Callable[[FrameOrSeries], FrameOrSeries], Axis, Optional[_IndexSlice] +] + + class Styler: """ Helps style a DataFrame or Series according to the data with HTML and CSS. @@ -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") @@ -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 @@ -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): """ @@ -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 @@ -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.