Skip to content

Commit b405e1b

Browse files
committed
Add more typing/comments to hooks.
1 parent 998171a commit b405e1b

File tree

1 file changed

+20
-11
lines changed

1 file changed

+20
-11
lines changed

dash/_hooks.py

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626

2727
# pylint: disable=too-few-public-methods
2828
class _Hook(_tx.Generic[HookDataType]):
29-
def __init__(self, func, priority, final=False, data: HookDataType = None):
29+
def __init__(self, func, priority=0, final=False, data: HookDataType = None):
3030
self.func = func
3131
self.final = final
3232
self.data = data
@@ -51,24 +51,32 @@ def __init__(self) -> None:
5151
self._clientside_callbacks: _t.List[
5252
_t.Tuple[ClientsideFuncType, _t.Any, _t.Any]
5353
] = []
54+
55+
# final hooks are a single hook added to the end of regular hooks.
5456
self._finals = {}
5557

5658
def add_hook(
57-
self, hook: str, func: _t.Callable, priority=None, final=False, data=None
59+
self,
60+
hook: str,
61+
func: _t.Callable,
62+
priority: _t.Optional[int] = None,
63+
final=False,
64+
data=None,
5865
):
5966
if final:
6067
existing = self._finals.get(hook)
6168
if existing:
6269
raise HookError("Final hook already present")
63-
self._finals[hook] = _Hook(func, priority, final, data=data)
70+
self._finals[hook] = _Hook(func, final, data=data)
6471
return
6572
hks = self._ns.get(hook, [])
73+
74+
p = 0
6675
if not priority and len(hks):
6776
priority_max = max(h.priority for h in hks)
68-
priority = priority_max - 1
69-
elif not priority:
70-
priority = 0
71-
hks.append(_Hook(func, priority=priority, data=data))
77+
p = priority_max - 1
78+
79+
hks.append(_Hook(func, priority=p, data=data))
7280
self._ns[hook] = sorted(hks, reverse=True, key=lambda h: h.priority)
7381

7482
def get_hooks(self, hook: str) -> _t.List[_Hook]:
@@ -106,7 +114,7 @@ def route(
106114
self,
107115
name: _t.Optional[str] = None,
108116
methods: _t.Sequence[str] = ("GET",),
109-
priority=None,
117+
priority: _t.Optional[int] = None,
110118
final=False,
111119
):
112120
"""
@@ -126,7 +134,7 @@ def wrap(func: _t.Callable[[], _f.Response]):
126134

127135
return wrap
128136

129-
def error(self, priority=None, final=False):
137+
def error(self, priority: _t.Optional[int] = None, final=False):
130138
"""Automatically add an error handler to the dash app."""
131139

132140
def _error(func: _t.Callable[[Exception], _t.Any]):
@@ -135,7 +143,7 @@ def _error(func: _t.Callable[[Exception], _t.Any]):
135143

136144
return _error
137145

138-
def callback(self, *args, priority=None, final=False, **kwargs):
146+
def callback(self, *args, priority: _t.Optional[int] = None, final=False, **kwargs):
139147
"""
140148
Add a callback to all the apps with the hook installed.
141149
"""
@@ -168,7 +176,7 @@ def stylesheet(self, distribution: _t.List[ResourceType]):
168176
"""Add stylesheets to the page."""
169177
self._css_dist.extend(distribution)
170178

171-
def index(self, priority=None, final=False):
179+
def index(self, priority: _t.Optional[int] = None, final=False):
172180
"""Modify the index of the apps."""
173181

174182
def wrap(func):
@@ -187,6 +195,7 @@ def wrap(func):
187195

188196

189197
class HooksManager:
198+
# Flag to only run `register_setuptools` once
190199
_registered = False
191200
hooks = hooks
192201

0 commit comments

Comments
 (0)