Skip to content

Commit b257db3

Browse files
committed
Add typing to dependencies
1 parent 32da2c0 commit b257db3

File tree

1 file changed

+24
-9
lines changed

1 file changed

+24
-9
lines changed

dash/dependencies.py

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
1+
from typing import Union, Sequence
2+
13
from dash.development.base_component import Component
24

35
from ._validate import validate_callback
46
from ._grouping import flatten_grouping, make_grouping_by_index
57
from ._utils import stringify_id
68

79

10+
ComponentIdType = Union[str, Component, dict]
11+
12+
813
class _Wildcard: # pylint: disable=too-few-public-methods
9-
def __init__(self, name):
14+
def __init__(self, name: str):
1015
self._name = name
1116

1217
def __str__(self):
@@ -15,7 +20,7 @@ def __str__(self):
1520
def __repr__(self):
1621
return f"<{self}>"
1722

18-
def to_json(self):
23+
def to_json(self) -> str:
1924
# used in serializing wildcards - arrays are not allowed as
2025
# id values, so make the wildcards look like length-1 arrays.
2126
return f'["{self._name}"]'
@@ -27,7 +32,12 @@ def to_json(self):
2732

2833

2934
class DashDependency: # pylint: disable=too-few-public-methods
30-
def __init__(self, component_id, component_property):
35+
component_id: ComponentIdType
36+
allow_duplicate: bool
37+
component_property: str
38+
allowed_wildcards: Sequence[_Wildcard]
39+
40+
def __init__(self, component_id: ComponentIdType, component_property: str):
3141

3242
if isinstance(component_id, Component):
3343
self.component_id = component_id._set_random_id()
@@ -43,10 +53,10 @@ def __str__(self):
4353
def __repr__(self):
4454
return f"<{self.__class__.__name__} `{self}`>"
4555

46-
def component_id_str(self):
56+
def component_id_str(self) -> str:
4757
return stringify_id(self.component_id)
4858

49-
def to_dict(self):
59+
def to_dict(self) -> dict:
5060
return {"id": self.component_id_str(), "property": self.component_property}
5161

5262
def __eq__(self, other):
@@ -61,7 +71,7 @@ def __eq__(self, other):
6171
and self._id_matches(other)
6272
)
6373

64-
def _id_matches(self, other):
74+
def _id_matches(self, other) -> bool:
6575
my_id = self.component_id
6676
other_id = other.component_id
6777
self_dict = isinstance(my_id, dict)
@@ -96,7 +106,7 @@ def _id_matches(self, other):
96106
def __hash__(self):
97107
return hash(str(self))
98108

99-
def has_wildcard(self):
109+
def has_wildcard(self) -> bool:
100110
"""
101111
Return true if id contains a wildcard (MATCH, ALL, or ALLSMALLER)
102112
"""
@@ -112,7 +122,12 @@ class Output(DashDependency): # pylint: disable=too-few-public-methods
112122

113123
allowed_wildcards = (MATCH, ALL)
114124

115-
def __init__(self, component_id, component_property, allow_duplicate=False):
125+
def __init__(
126+
self,
127+
component_id: ComponentIdType,
128+
component_property: str,
129+
allow_duplicate: bool = False,
130+
):
116131
super().__init__(component_id, component_property)
117132
self.allow_duplicate = allow_duplicate
118133

@@ -130,7 +145,7 @@ class State(DashDependency): # pylint: disable=too-few-public-methods
130145

131146

132147
class ClientsideFunction: # pylint: disable=too-few-public-methods
133-
def __init__(self, namespace=None, function_name=None):
148+
def __init__(self, namespace: str, function_name: str):
134149

135150
if namespace.startswith("_dashprivate_"):
136151
raise ValueError("Namespaces cannot start with '_dashprivate_'.")

0 commit comments

Comments
 (0)