diff --git a/dash/dependencies.py b/dash/dependencies.py index 0b5dbe6867..aa4c1aa1db 100644 --- a/dash/dependencies.py +++ b/dash/dependencies.py @@ -1,3 +1,4 @@ +from enum import Enum from typing import Union, Sequence, Any from .development.base_component import Component @@ -9,32 +10,33 @@ ComponentIdType = Union[str, Component, dict] -class _Wildcard: # pylint: disable=too-few-public-methods - def __init__(self, name: str): - self._name = name +class Wildcard(Enum): + MATCH = "MATCH" + ALL = "ALL" + ALLSMALLER = "ALLSMALLER" def __str__(self): - return self._name + return self.value def __repr__(self): - return f"<{self}>" + return f"<{self.value}>" def to_json(self) -> str: # used in serializing wildcards - arrays are not allowed as # id values, so make the wildcards look like length-1 arrays. - return f'["{self._name}"]' + return f'["{self.value}"]' -MATCH = _Wildcard("MATCH") -ALL = _Wildcard("ALL") -ALLSMALLER = _Wildcard("ALLSMALLER") +MATCH = Wildcard.MATCH +ALL = Wildcard.ALL +ALLSMALLER = Wildcard.ALLSMALLER class DashDependency: # pylint: disable=too-few-public-methods component_id: ComponentIdType allow_duplicate: bool component_property: str - allowed_wildcards: Sequence[_Wildcard] + allowed_wildcards: Sequence[Wildcard] allow_optional: bool def __init__(self, component_id: ComponentIdType, component_property: str): @@ -95,8 +97,8 @@ def _id_matches(self, other) -> bool: other_v = other_id[k] if v == other_v: continue - v_wild = isinstance(v, _Wildcard) - other_wild = isinstance(other_v, _Wildcard) + v_wild = isinstance(v, Wildcard) + other_wild = isinstance(other_v, Wildcard) if v_wild or other_wild: if not (v_wild and other_wild): continue # one wild, one not @@ -120,7 +122,7 @@ def has_wildcard(self) -> bool: """ if isinstance(self.component_id, dict): for v in self.component_id.values(): - if isinstance(v, _Wildcard): + if isinstance(v, Wildcard): return True return False diff --git a/dash/development/_py_components_generation.py b/dash/development/_py_components_generation.py index c6f3ae6c90..492864fb5f 100644 --- a/dash/development/_py_components_generation.py +++ b/dash/development/_py_components_generation.py @@ -24,15 +24,8 @@ import typing # noqa: F401 from typing_extensions import TypedDict, NotRequired, Literal # noqa: F401 from dash.development.base_component import Component, _explicitize_args +from dash.development.base_component import ComponentType # noqa: F401 {custom_imports} -ComponentType = typing.Union[ - str, - int, - float, - Component, - None, - typing.Sequence[typing.Union[str, int, float, Component, None]], -] NumberType = typing.Union[ typing.SupportsFloat, typing.SupportsInt, typing.SupportsComplex diff --git a/dash/development/base_component.py b/dash/development/base_component.py index 63ccc4ee20..02579ff2e2 100644 --- a/dash/development/base_component.py +++ b/dash/development/base_component.py @@ -454,14 +454,11 @@ def _validate_deprecation(self): warnings.warn(DeprecationWarning(textwrap.dedent(deprecation_message))) +ComponentSingleType = typing.Union[str, int, float, Component, None] # Renderable node type. ComponentType = typing.Union[ - str, - int, - float, - Component, - None, - typing.Sequence[typing.Union[str, int, float, Component, None]], + ComponentSingleType, + typing.Sequence[ComponentSingleType], ] ComponentTemplate = typing.TypeVar("ComponentTemplate")