From 81664300347c2d8c126380a35f7f105579cc24d8 Mon Sep 17 00:00:00 2001 From: Marco Gorelli Date: Wed, 14 Apr 2021 16:41:46 +0100 Subject: [PATCH 1/4] use typeddict to define cssdict --- pandas/_typing.py | 7 ++++++- pandas/io/formats/style_render.py | 24 +++++++++++++++--------- 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/pandas/_typing.py b/pandas/_typing.py index 9f23fcc56597f..a58dc0dba1bf1 100644 --- a/pandas/_typing.py +++ b/pandas/_typing.py @@ -36,7 +36,10 @@ # and use a string literal forward reference to it in subsequent types # https://mypy.readthedocs.io/en/latest/common_issues.html#import-cycles if TYPE_CHECKING: - from typing import final + from typing import ( + TypedDict, + final, + ) from pandas._libs import ( Period, @@ -70,6 +73,8 @@ else: # typing.final does not exist until py38 final = lambda x: x + # typing.TypedDict does not exist until py38 + TypedDict = dict # array-like diff --git a/pandas/io/formats/style_render.py b/pandas/io/formats/style_render.py index 45b1d97b9694c..3c14a5c67420c 100644 --- a/pandas/io/formats/style_render.py +++ b/pandas/io/formats/style_render.py @@ -12,7 +12,6 @@ Sequence, Tuple, Union, - cast, ) from uuid import uuid4 @@ -21,7 +20,10 @@ from pandas._config import get_option from pandas._libs import lib -from pandas._typing import FrameOrSeriesUnion +from pandas._typing import ( + FrameOrSeriesUnion, + TypedDict, +) from pandas.compat._optional import import_optional_dependency from pandas.core.dtypes.generic import ABCSeries @@ -45,10 +47,14 @@ CSSPair = Tuple[str, Union[str, int, float]] CSSList = List[CSSPair] CSSProperties = Union[str, CSSList] -CSSStyles = List[Dict[str, CSSProperties]] # = List[CSSDict] -# class CSSDict(TypedDict): # available when TypedDict is valid in pandas -# selector: str -# props: CSSProperties + + +class CSSDict(TypedDict): + selector: str + props: CSSProperties + + +CSSStyles = List[CSSDict] class StylerRenderer: @@ -597,9 +603,9 @@ def _format_table_styles(styles: CSSStyles) -> CSSStyles: return [ item for sublist in [ - [ # this is a CSSDict when TypedDict is available to avoid cast. - {"selector": x, "props": style["props"]} - for x in cast(str, style["selector"]).split(",") + [ + CSSDict(selector=x, props=style["props"]) + for x in style["selector"].split(",") ] for style in styles ] From 1d5c6c1c9ab43469b7ad173bb835ca4200ba19cb Mon Sep 17 00:00:00 2001 From: Marco Gorelli Date: Sun, 25 Apr 2021 20:55:38 +0100 Subject: [PATCH 2/4] wip --- pandas/io/formats/style_render.py | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/pandas/io/formats/style_render.py b/pandas/io/formats/style_render.py index 9dbacfb3e2f46..e109c56373aac 100644 --- a/pandas/io/formats/style_render.py +++ b/pandas/io/formats/style_render.py @@ -620,17 +620,12 @@ def _format_table_styles(styles: CSSStyles) -> CSSStyles: ---> [{'selector': 'td', 'props': 'a:v;'}, {'selector': 'th', 'props': 'a:v;'}] """ - return [ - item - for sublist in [ - [ - CSSDict(selector=x, props=style["props"]) - for x in style["selector"].split(",") - ] - for style in styles - ] - for item in sublist + ret = [ + CSSDict(selector=x, props=style["props"]) + for style in styles + for x in style["selector"].split(",") ] + return ret def _default_formatter(x: Any, precision: int, thousands: bool = False) -> Any: From ab033a008c590ce81abdec2bb0c7b2da3b6ecc61 Mon Sep 17 00:00:00 2001 From: Marco Gorelli Date: Sun, 25 Apr 2021 20:57:29 +0100 Subject: [PATCH 3/4] clean up --- pandas/io/formats/style_render.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/pandas/io/formats/style_render.py b/pandas/io/formats/style_render.py index e109c56373aac..15fb9f82625d2 100644 --- a/pandas/io/formats/style_render.py +++ b/pandas/io/formats/style_render.py @@ -620,12 +620,11 @@ def _format_table_styles(styles: CSSStyles) -> CSSStyles: ---> [{'selector': 'td', 'props': 'a:v;'}, {'selector': 'th', 'props': 'a:v;'}] """ - ret = [ + return [ CSSDict(selector=x, props=style["props"]) for style in styles for x in style["selector"].split(",") ] - return ret def _default_formatter(x: Any, precision: int, thousands: bool = False) -> Any: From b6aa69fe003146b66e4c6a550c66ab0479521d41 Mon Sep 17 00:00:00 2001 From: Marco Gorelli Date: Sun, 25 Apr 2021 21:00:39 +0100 Subject: [PATCH 4/4] dont construct cssdict --- pandas/io/formats/style_render.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pandas/io/formats/style_render.py b/pandas/io/formats/style_render.py index 15fb9f82625d2..776cedcf11592 100644 --- a/pandas/io/formats/style_render.py +++ b/pandas/io/formats/style_render.py @@ -621,9 +621,9 @@ def _format_table_styles(styles: CSSStyles) -> CSSStyles: {'selector': 'th', 'props': 'a:v;'}] """ return [ - CSSDict(selector=x, props=style["props"]) - for style in styles - for x in style["selector"].split(",") + {"selector": selector, "props": css_dict["props"]} + for css_dict in styles + for selector in css_dict["selector"].split(",") ]