Skip to content

Commit b9691bb

Browse files
MarcoGorelliJulianWgs
authored andcommitted
TYP use typeddict to define cssdict (pandas-dev#40947)
1 parent 013572b commit b9691bb

File tree

2 files changed

+21
-16
lines changed

2 files changed

+21
-16
lines changed

pandas/_typing.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,10 @@
3636
# and use a string literal forward reference to it in subsequent types
3737
# https://mypy.readthedocs.io/en/latest/common_issues.html#import-cycles
3838
if TYPE_CHECKING:
39-
from typing import final
39+
from typing import (
40+
TypedDict,
41+
final,
42+
)
4043

4144
from pandas._libs import (
4245
Period,
@@ -70,6 +73,8 @@
7073
else:
7174
# typing.final does not exist until py38
7275
final = lambda x: x
76+
# typing.TypedDict does not exist until py38
77+
TypedDict = dict
7378

7479

7580
# array-like

pandas/io/formats/style_render.py

+15-15
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
Sequence,
1313
Tuple,
1414
Union,
15-
cast,
1615
)
1716
from uuid import uuid4
1817

@@ -21,7 +20,10 @@
2120
from pandas._config import get_option
2221

2322
from pandas._libs import lib
24-
from pandas._typing import FrameOrSeriesUnion
23+
from pandas._typing import (
24+
FrameOrSeriesUnion,
25+
TypedDict,
26+
)
2527
from pandas.compat._optional import import_optional_dependency
2628

2729
from pandas.core.dtypes.generic import ABCSeries
@@ -45,10 +47,14 @@
4547
CSSPair = Tuple[str, Union[str, int, float]]
4648
CSSList = List[CSSPair]
4749
CSSProperties = Union[str, CSSList]
48-
CSSStyles = List[Dict[str, CSSProperties]] # = List[CSSDict]
49-
# class CSSDict(TypedDict): # available when TypedDict is valid in pandas
50-
# selector: str
51-
# props: CSSProperties
50+
51+
52+
class CSSDict(TypedDict):
53+
selector: str
54+
props: CSSProperties
55+
56+
57+
CSSStyles = List[CSSDict]
5258

5359

5460
class StylerRenderer:
@@ -583,15 +589,9 @@ def _format_table_styles(styles: CSSStyles) -> CSSStyles:
583589
{'selector': 'th', 'props': 'a:v;'}]
584590
"""
585591
return [
586-
item
587-
for sublist in [
588-
[ # this is a CSSDict when TypedDict is available to avoid cast.
589-
{"selector": x, "props": style["props"]}
590-
for x in cast(str, style["selector"]).split(",")
591-
]
592-
for style in styles
593-
]
594-
for item in sublist
592+
{"selector": selector, "props": css_dict["props"]}
593+
for css_dict in styles
594+
for selector in css_dict["selector"].split(",")
595595
]
596596

597597

0 commit comments

Comments
 (0)