Skip to content

Commit 26a83e5

Browse files
committed
Backward compatible ComponentType.
1 parent dd5cb44 commit 26a83e5

File tree

3 files changed

+28
-29
lines changed

3 files changed

+28
-29
lines changed

dash/development/_py_components_generation.py

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,20 @@
1313
from ._py_prop_typing import get_prop_typing, shapes, custom_imports
1414
from .base_component import Component, ComponentType
1515

16+
import_string = """# AUTO GENERATED FILE - DO NOT EDIT
17+
18+
import typing # noqa: F401
19+
import numbers # noqa: F401
20+
from typing_extensions import TypedDict, NotRequired, Literal # noqa: F401
21+
from dash.development.base_component import Component, _explicitize_args
22+
try:
23+
from dash.development.base_component import ComponentType # noqa: F401
24+
except ImportError:
25+
ComponentType = typing.TypeVar("ComponentType", bound=Component)
26+
27+
28+
"""
29+
1630

1731
# pylint: disable=unused-argument,too-many-locals,too-many-branches
1832
def generate_class_string(
@@ -206,30 +220,22 @@ def generate_class_file(
206220
Returns
207221
-------
208222
"""
209-
import_string = (
210-
"# AUTO GENERATED FILE - DO NOT EDIT\n\n"
211-
"import typing # noqa: F401\n"
212-
"import numbers # noqa: F401\n"
213-
"from typing_extensions import TypedDict, NotRequired, Literal # noqa: F401\n"
214-
"from dash.development.base_component import ComponentType # noqa: F401\n"
215-
"from dash.development.base_component import "
216-
"Component, _explicitize_args\n\n\n"
217-
)
223+
imports = import_string
218224

219225
class_string = generate_class_string(
220226
typename, props, description, namespace, prop_reorder_exceptions, max_props
221227
)
222228

223229
custom_imp = custom_imports[namespace][typename]
224230
if custom_imp:
225-
import_string += "\n".join(custom_imp)
226-
import_string += "\n\n"
231+
imports += "\n".join(custom_imp)
232+
imports += "\n\n"
227233

228234
file_name = f"{typename:s}.py"
229235

230236
file_path = os.path.join(namespace, file_name)
231237
with open(file_path, "w", encoding="utf-8") as f:
232-
f.write(import_string)
238+
f.write(imports)
233239
f.write(class_string)
234240

235241
print(f"Generated {file_name}")

tests/unit/development/metadata_test.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@
33
import typing # noqa: F401
44
import numbers # noqa: F401
55
from typing_extensions import TypedDict, NotRequired, Literal # noqa: F401
6-
from dash.development.base_component import ComponentType # noqa: F401
76
from dash.development.base_component import Component, _explicitize_args
7+
try:
8+
from dash.development.base_component import ComponentType # noqa: F401
9+
except ImportError:
10+
ComponentType = typing.TypeVar("ComponentType", bound=Component)
811

912

1013
class Table(Component):
@@ -97,7 +100,7 @@ class Table(Component):
97100
OptionalObjectWithExactAndNestedDescriptionFigure = TypedDict(
98101
"OptionalObjectWithExactAndNestedDescriptionFigure",
99102
{
100-
"data": NotRequired[typing.Union[typing.Sequence[dict], typing.Tuple]],
103+
"data": NotRequired[typing.Sequence[dict]],
101104
"layout": NotRequired[dict]
102105
}
103106
)
@@ -114,7 +117,7 @@ class Table(Component):
114117
OptionalObjectWithShapeAndNestedDescriptionFigure = TypedDict(
115118
"OptionalObjectWithShapeAndNestedDescriptionFigure",
116119
{
117-
"data": NotRequired[typing.Union[typing.Sequence[dict], typing.Tuple]],
120+
"data": NotRequired[typing.Sequence[dict]],
118121
"layout": NotRequired[dict]
119122
}
120123
)
@@ -132,7 +135,7 @@ class Table(Component):
132135
def __init__(
133136
self,
134137
children: typing.Optional[typing.Union[str, int, float, ComponentType, typing.Sequence[typing.Union[str, int, float, ComponentType]]]] = None,
135-
optionalArray: typing.Optional[typing.Union[typing.Sequence, typing.Tuple]] = None,
138+
optionalArray: typing.Optional[typing.Sequence] = None,
136139
optionalBool: typing.Optional[bool] = None,
137140
optionalFunc: typing.Optional[typing.Any] = None,
138141
optionalNumber: typing.Optional[typing.Union[int, float, numbers.Number]] = None,
@@ -144,13 +147,13 @@ def __init__(
144147
optionalMessage: typing.Optional[typing.Any] = None,
145148
optionalEnum: typing.Optional[Literal["News", "Photos"]] = None,
146149
optionalUnion: typing.Optional[typing.Union[str, typing.Union[int, float, numbers.Number], typing.Any]] = None,
147-
optionalArrayOf: typing.Optional[typing.Union[typing.Sequence[typing.Union[int, float, numbers.Number]], typing.Tuple]] = None,
150+
optionalArrayOf: typing.Optional[typing.Sequence[typing.Union[int, float, numbers.Number]]] = None,
148151
optionalObjectOf: typing.Optional[typing.Dict[typing.Union[str, float, int], typing.Union[int, float, numbers.Number]]] = None,
149152
optionalObjectWithExactAndNestedDescription: typing.Optional["OptionalObjectWithExactAndNestedDescription"] = None,
150153
optionalObjectWithShapeAndNestedDescription: typing.Optional["OptionalObjectWithShapeAndNestedDescription"] = None,
151154
optionalAny: typing.Optional[typing.Any] = None,
152155
customProp: typing.Optional[typing.Any] = None,
153-
customArrayProp: typing.Optional[typing.Union[typing.Sequence[typing.Any], typing.Tuple]] = None,
156+
customArrayProp: typing.Optional[typing.Sequence[typing.Any]] = None,
154157
id: typing.Optional[str] = None,
155158
**kwargs
156159
):

tests/unit/development/test_generate_class_file.py

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,10 @@
88
from dash.development._py_components_generation import (
99
generate_class_string,
1010
generate_class_file,
11+
import_string,
1112
)
1213
from . import _dir, has_trailing_space
1314

14-
# Import string not included in generated class string
15-
import_string = (
16-
"# AUTO GENERATED FILE - DO NOT EDIT\n\n"
17-
"import typing # noqa: F401\n"
18-
"import numbers # noqa: F401\n"
19-
"from typing_extensions import TypedDict, NotRequired, Literal # noqa: F401\n"
20-
"from dash.development.base_component import ComponentType # noqa: F401\n"
21-
"from dash.development.base_component import "
22-
"Component, _explicitize_args\n\n\n"
23-
)
24-
2515

2616
@pytest.fixture
2717
def make_component_dir(load_test_metadata_json):

0 commit comments

Comments
 (0)