Skip to content

Commit 502997d

Browse files
authored
Merge pull request #3540 from tlauli/expose_more_types
Expose more types for better static typing options
2 parents a6a03d7 + 5af2dbf commit 502997d

File tree

3 files changed

+21
-25
lines changed

3 files changed

+21
-25
lines changed

dash/dependencies.py

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from enum import Enum
12
from typing import Union, Sequence, Any
23

34
from .development.base_component import Component
@@ -9,32 +10,33 @@
910
ComponentIdType = Union[str, Component, dict]
1011

1112

12-
class _Wildcard: # pylint: disable=too-few-public-methods
13-
def __init__(self, name: str):
14-
self._name = name
13+
class Wildcard(Enum):
14+
MATCH = "MATCH"
15+
ALL = "ALL"
16+
ALLSMALLER = "ALLSMALLER"
1517

1618
def __str__(self):
17-
return self._name
19+
return self.value
1820

1921
def __repr__(self):
20-
return f"<{self}>"
22+
return f"<{self.value}>"
2123

2224
def to_json(self) -> str:
2325
# used in serializing wildcards - arrays are not allowed as
2426
# id values, so make the wildcards look like length-1 arrays.
25-
return f'["{self._name}"]'
27+
return f'["{self.value}"]'
2628

2729

28-
MATCH = _Wildcard("MATCH")
29-
ALL = _Wildcard("ALL")
30-
ALLSMALLER = _Wildcard("ALLSMALLER")
30+
MATCH = Wildcard.MATCH
31+
ALL = Wildcard.ALL
32+
ALLSMALLER = Wildcard.ALLSMALLER
3133

3234

3335
class DashDependency: # pylint: disable=too-few-public-methods
3436
component_id: ComponentIdType
3537
allow_duplicate: bool
3638
component_property: str
37-
allowed_wildcards: Sequence[_Wildcard]
39+
allowed_wildcards: Sequence[Wildcard]
3840
allow_optional: bool
3941

4042
def __init__(self, component_id: ComponentIdType, component_property: str):
@@ -95,8 +97,8 @@ def _id_matches(self, other) -> bool:
9597
other_v = other_id[k]
9698
if v == other_v:
9799
continue
98-
v_wild = isinstance(v, _Wildcard)
99-
other_wild = isinstance(other_v, _Wildcard)
100+
v_wild = isinstance(v, Wildcard)
101+
other_wild = isinstance(other_v, Wildcard)
100102
if v_wild or other_wild:
101103
if not (v_wild and other_wild):
102104
continue # one wild, one not
@@ -120,7 +122,7 @@ def has_wildcard(self) -> bool:
120122
"""
121123
if isinstance(self.component_id, dict):
122124
for v in self.component_id.values():
123-
if isinstance(v, _Wildcard):
125+
if isinstance(v, Wildcard):
124126
return True
125127
return False
126128

dash/development/_py_components_generation.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,10 @@
2525
from typing_extensions import TypedDict, NotRequired, Literal # noqa: F401
2626
from dash.development.base_component import Component, _explicitize_args
2727
{custom_imports}
28+
ComponentSingleType = typing.Union[str, int, float, Component, None]
2829
ComponentType = typing.Union[
29-
str,
30-
int,
31-
float,
32-
Component,
33-
None,
34-
typing.Sequence[typing.Union[str, int, float, Component, None]],
30+
ComponentSingleType,
31+
typing.Sequence[ComponentSingleType],
3532
]
3633
3734
NumberType = typing.Union[

dash/development/base_component.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -454,14 +454,11 @@ def _validate_deprecation(self):
454454
warnings.warn(DeprecationWarning(textwrap.dedent(deprecation_message)))
455455

456456

457+
ComponentSingleType = typing.Union[str, int, float, Component, None]
457458
# Renderable node type.
458459
ComponentType = typing.Union[
459-
str,
460-
int,
461-
float,
462-
Component,
463-
None,
464-
typing.Sequence[typing.Union[str, int, float, Component, None]],
460+
ComponentSingleType,
461+
typing.Sequence[ComponentSingleType],
465462
]
466463

467464
ComponentTemplate = typing.TypeVar("ComponentTemplate")

0 commit comments

Comments
 (0)