Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 15 additions & 13 deletions dash/dependencies.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from enum import Enum
from typing import Union, Sequence, Any

from .development.base_component import Component
Expand All @@ -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):
Expand Down Expand Up @@ -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
Expand All @@ -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

Expand Down
9 changes: 3 additions & 6 deletions dash/development/_py_components_generation.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,10 @@
from typing_extensions import TypedDict, NotRequired, Literal # noqa: F401
from dash.development.base_component import Component, _explicitize_args
{custom_imports}
ComponentSingleType = typing.Union[str, int, float, Component, None]
ComponentType = typing.Union[
str,
int,
float,
Component,
None,
typing.Sequence[typing.Union[str, int, float, Component, None]],
ComponentSingleType,
typing.Sequence[ComponentSingleType],
]

NumberType = typing.Union[
Expand Down
9 changes: 3 additions & 6 deletions dash/development/base_component.py
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Comment on lines +457 to +461
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This type is also duplicated on generate components for backward compatibility. The template is here:

ComponentType = typing.Union[
str,
int,
float,
Component,
None,
typing.Sequence[typing.Union[str, int, float, Component, None]],
]

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I replaced the duplicated code with import from the base_component, is that ok?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, it's for backward compatibility of generated components in external components libraries, if the library is built with the new imports but used with a previous version of dash it would not work.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I see. I removed the import and used the new type.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be ok to replace the duplicated code with the import for 4.0?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, it's really just building the components need to be backward compatible as we don't want to force components libraries to depend on a particular version of dash.

]

ComponentTemplate = typing.TypeVar("ComponentTemplate")
Expand Down