Skip to content

Commit 8734e1b

Browse files
committed
Use ComponentType
1 parent 197180c commit 8734e1b

File tree

3 files changed

+15
-8
lines changed

3 files changed

+15
-8
lines changed

dash/development/_py_components_generation.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from ._all_keywords import python_keywords
1212
from ._collect_nodes import collect_nodes, filter_base_nodes
1313
from ._py_prop_typing import get_prop_typing, shapes, custom_imports
14-
from .base_component import Component
14+
from .base_component import Component, ComponentType
1515

1616

1717
# pylint: disable=unused-argument,too-many-locals,too-many-branches
@@ -213,6 +213,7 @@ def generate_class_file(
213213
"import typing # noqa: F401\n"
214214
"import numbers # noqa: F401\n"
215215
"from typing_extensions import TypedDict, NotRequired, Literal # noqa: F401\n"
216+
"from dash.development.base_component import ComponentType # noqa: F401\n"
216217
"from dash.development.base_component import "
217218
"Component, _explicitize_args\n\n\n"
218219
)
@@ -282,6 +283,7 @@ def generate_class(
282283
)
283284
scope = {
284285
"Component": Component,
286+
"ComponentType": ComponentType,
285287
"_explicitize_args": _explicitize_args,
286288
"typing": typing,
287289
"numbers": numbers,

dash/development/_py_prop_typing.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ def generate_array_of(
8282
typed = get_prop_typing(
8383
type_info["value"]["name"], component_name, prop_name, type_info["value"]
8484
)
85-
return f"typing.Union[typing.List[{typed}], typing.Tuple]"
85+
return f"typing.Union[typing.Sequence[{typed}], typing.Tuple]"
8686

8787

8888
def generate_object_of(type_info, component_name: str, prop_name: str):
@@ -146,7 +146,7 @@ def generate_plotly_figure(*_):
146146

147147

148148
PROP_TYPING = {
149-
"array": generate_type("typing.Union[typing.List, typing.Tuple]"),
149+
"array": generate_type("typing.Union[typing.Sequence, typing.Tuple]"),
150150
"arrayOf": generate_array_of,
151151
"object": generate_type("dict"),
152152
"shape": generate_shape,
@@ -155,12 +155,12 @@ def generate_plotly_figure(*_):
155155
"bool": generate_type("bool"),
156156
"number": generate_type("typing.Union[int, float, numbers.Number]"),
157157
"node": generate_type(
158-
"typing.Union[str, int, float, Component,"
159-
" typing.List[typing.Union"
160-
"[str, int, float, Component]]]"
158+
"typing.Union[str, int, float, ComponentType,"
159+
" typing.Sequence[typing.Union"
160+
"[str, int, float, ComponentType]]]"
161161
),
162162
"func": generate_any,
163-
"element": generate_type("Component"),
163+
"element": generate_type("ComponentType"),
164164
"union": generate_union,
165165
"any": generate_any,
166166
"custom": generate_any,

dash/development/base_component.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,9 @@ def _check_if_has_indexable_children(item):
7676
class Component(metaclass=ComponentMeta):
7777
_children_props = []
7878
_base_nodes = ["children"]
79+
_namespace: str
80+
_type: str
81+
_prop_names: list[str]
7982

8083
_valid_wildcard_attributes: typing.List[str]
8184
available_wildcard_properties: typing.List[str]
@@ -101,7 +104,6 @@ def __str__(self):
101104
def __init__(self, **kwargs):
102105
import dash # pylint: disable=import-outside-toplevel, cyclic-import
103106

104-
# pylint: disable=super-init-not-called
105107
for k, v in list(kwargs.items()):
106108
# pylint: disable=no-member
107109
k_in_propnames = k in self._prop_names
@@ -410,6 +412,9 @@ def __repr__(self):
410412
return f"{self._type}({props_string})"
411413

412414

415+
ComponentType = typing.TypeVar("ComponentType", bound=Component)
416+
417+
413418
# This wrapper adds an argument given to generated Component.__init__
414419
# with the actual given parameters by the user as a list of string.
415420
# This is then checked in the generated init to check if required

0 commit comments

Comments
 (0)