Skip to content

Commit 3349174

Browse files
committed
Fix typing generation & explicitize_args
1 parent 17fca44 commit 3349174

File tree

3 files changed

+36
-20
lines changed

3 files changed

+36
-20
lines changed

dash/development/_py_components_generation.py

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,21 @@
2222
import_string = """# AUTO GENERATED FILE - DO NOT EDIT
2323
2424
import typing # noqa: F401
25-
import numbers # noqa: F401
2625
from typing_extensions import TypedDict, NotRequired, Literal # noqa: F401
27-
from dash.development.base_component import Component
28-
try:
29-
from dash.development.base_component import ComponentType # noqa: F401
30-
except ImportError:
31-
ComponentType = typing.TypeVar("ComponentType", bound=Component)
26+
from dash.development.base_component import Component, _explicitize_args
27+
{custom_imports}
28+
ComponentType = typing.Union[
29+
str,
30+
int,
31+
float,
32+
Component,
33+
None,
34+
typing.Sequence[typing.Union[str, int, float, Component, None]],
35+
]
36+
37+
NumberType = typing.Union[
38+
typing.SupportsFloat, typing.SupportsInt, typing.SupportsComplex
39+
]
3240
3341
3442
"""
@@ -80,7 +88,6 @@ def generate_class_string(
8088
_namespace = '{namespace}'
8189
_type = '{typename}'
8290
{shapes}
83-
_explicitize_dash_init = True
8491
8592
def __init__(
8693
self,
@@ -98,6 +105,8 @@ def __init__(
98105
args = {args}
99106
{required_validation}
100107
super({typename}, self).__init__({argtext})
108+
109+
setattr({typename}, "__init__", _explicitize_args({typename}.__init__))
101110
'''
102111

103112
filtered_props = (
@@ -239,7 +248,6 @@ def generate_class_file(
239248
Returns
240249
-------
241250
"""
242-
imports = import_string
243251

244252
class_string = generate_class_string(
245253
typename,
@@ -255,8 +263,11 @@ def generate_class_file(
255263
custom_imp = custom_imp.get(typename) or custom_imp.get("*")
256264

257265
if custom_imp:
258-
imports += "\n".join(custom_imp)
259-
imports += "\n\n"
266+
imports = import_string.format(
267+
custom_imports="\n" + "\n".join(custom_imp) + "\n\n"
268+
)
269+
else:
270+
imports = import_string.format(custom_imports="")
260271

261272
file_name = f"{typename:s}.py"
262273

dash/development/_py_prop_typing.py

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -183,16 +183,10 @@ def get_prop_typing(
183183
"exact": generate_shape,
184184
"string": generate_type("str"),
185185
"bool": generate_type("bool"),
186-
"number": generate_type(
187-
"typing.Union[typing.SupportsFloat, typing.SupportsInt, typing.SupportsComplex]"
188-
),
189-
"node": generate_type(
190-
"typing.Union[str, int, float, ComponentType,"
191-
" typing.Sequence[typing.Union"
192-
"[str, int, float, ComponentType]]]"
193-
),
186+
"number": generate_type("NumberType"),
187+
"node": generate_type("ComponentType"),
194188
"func": generate_any,
195-
"element": generate_type("ComponentType"),
189+
"element": generate_type("Component"),
196190
"union": generate_union,
197191
"any": generate_any,
198192
"custom": generate_any,

dash/development/base_component.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ def __new__(mcs, name, bases, attributes):
5757
# We only want to patch the new generated component without
5858
# the `@_explicitize_args` decorator for mypy support
5959
# See issue: https://github.com/plotly/dash/issues/3226
60+
# Only for component that were generated by 3.0.3
61+
# Better to setattr on the component afterwards to ensure
62+
# backward compatibility.
6063
attributes["__init__"] = _explicitize_args(attributes["__init__"])
6164

6265
_component = abc.ABCMeta.__new__(mcs, name, bases, attributes)
@@ -441,7 +444,15 @@ def _validate_deprecation(self):
441444
warnings.warn(DeprecationWarning(textwrap.dedent(deprecation_message)))
442445

443446

444-
ComponentType = typing.TypeVar("ComponentType", bound=Component)
447+
# Renderable node type.
448+
ComponentType = typing.Union[
449+
str,
450+
int,
451+
float,
452+
Component,
453+
None,
454+
typing.Sequence[typing.Union[str, int, float, Component, None]],
455+
]
445456

446457
ComponentTemplate = typing.TypeVar("ComponentTemplate")
447458

0 commit comments

Comments
 (0)