Skip to content

Commit 1c1068f

Browse files
Puneet DixitDeepak kudi
authored andcommitted
Fix DataTable conditional style typing
1 parent bb2d0f0 commit 1c1068f

2 files changed

Lines changed: 91 additions & 0 deletions

File tree

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# This file is automatically loaded on build time to generate types.
2+
3+
from dash.development._py_prop_typing import get_prop_typing
4+
5+
6+
def generate_conditional_style(type_info, component_name, prop_name):
7+
condition_type = get_prop_typing(
8+
type_info["value"]["name"],
9+
component_name,
10+
prop_name,
11+
type_info["value"],
12+
)
13+
return (
14+
"typing.Sequence["
15+
f"typing.Union[{condition_type}, typing.Dict[str, typing.Any]]"
16+
"]"
17+
)
18+
19+
20+
custom_props = {
21+
"DataTable": {
22+
"style_cell_conditional": generate_conditional_style,
23+
"style_data_conditional": generate_conditional_style,
24+
"style_filter_conditional": generate_conditional_style,
25+
"style_header_conditional": generate_conditional_style,
26+
},
27+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import sys
2+
from pathlib import Path
3+
4+
import pytest
5+
6+
from dash.development._py_components_generation import generate_class_string
7+
from dash.development._py_prop_typing import shapes
8+
9+
CONDITIONAL_STYLE_TYPE = {
10+
"name": "arrayOf",
11+
"value": {
12+
"name": "shape",
13+
"value": {
14+
"if": {
15+
"name": "exact",
16+
"value": {
17+
"column_id": {"name": "string", "required": False},
18+
},
19+
"required": False,
20+
},
21+
},
22+
},
23+
}
24+
25+
26+
@pytest.mark.parametrize(
27+
"prop_name, shape_name",
28+
[
29+
("style_cell_conditional", "StyleCellConditional"),
30+
("style_data_conditional", "StyleDataConditional"),
31+
("style_filter_conditional", "StyleFilterConditional"),
32+
("style_header_conditional", "StyleHeaderConditional"),
33+
],
34+
)
35+
def test_datatable_conditional_styles_allow_style_keys(
36+
monkeypatch, prop_name, shape_name
37+
):
38+
dash_table_dir = Path(__file__).resolve().parents[3] / "components" / "dash-table"
39+
monkeypatch.syspath_prepend(str(dash_table_dir))
40+
sys.modules.pop("dash_prop_typing", None)
41+
shapes.clear()
42+
43+
generated = generate_class_string(
44+
typename="DataTable",
45+
props={
46+
prop_name: {
47+
"type": CONDITIONAL_STYLE_TYPE,
48+
"required": False,
49+
"description": "Conditional style",
50+
},
51+
},
52+
description="DataTable",
53+
namespace="dash_table",
54+
custom_typing_module="dash_prop_typing",
55+
)
56+
57+
assert (
58+
f'{prop_name}: typing.Optional[typing.Sequence[typing.Union["{shape_name}", '
59+
"typing.Dict[str, typing.Any]]]] = None"
60+
) in generated
61+
assert f'"if": NotRequired["{shape_name}If"]' in generated
62+
63+
shapes.clear()
64+
sys.modules.pop("dash_prop_typing", None)

0 commit comments

Comments
 (0)