Skip to content

Commit 435f7ef

Browse files
committed
Add more types to Dash
1 parent 0443c55 commit 435f7ef

File tree

1 file changed

+32
-29
lines changed

1 file changed

+32
-29
lines changed

dash/dash.py

Lines changed: 32 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,11 @@
1818
import base64
1919
import traceback
2020
from urllib.parse import urlparse
21-
from typing import Any, Callable, Dict, Optional, Union, Sequence, cast
21+
from typing import Any, Callable, Dict, Optional, Union, Sequence, cast, Literal
2222

2323
import flask
2424

25+
from flask.typing import RouteCallable
2526
from importlib_metadata import version as _get_distribution_version
2627

2728
from dash import dcc
@@ -622,7 +623,7 @@ def _setup_hooks(self):
622623
if self._hooks.get_hooks("error"):
623624
self._on_error = self._hooks.HookErrorHandler(self._on_error)
624625

625-
def init_app(self, app=None, **kwargs):
626+
def init_app(self, app: Optional[flask.Flask] = None, **kwargs) -> None:
626627
"""Initialize the parts of Dash that require a flask app."""
627628

628629
config = self.config
@@ -694,7 +695,7 @@ def _handle_error(_):
694695

695696
self._setup_plotlyjs()
696697

697-
def _add_url(self, name, view_func, methods=("GET",)):
698+
def _add_url(self, name: str, view_func: RouteCallable, methods=("GET",)) -> None:
698699
full_name = self.config.routes_pathname_prefix + name
699700

700701
self.server.add_url_rule(
@@ -748,11 +749,11 @@ def _setup_plotlyjs(self):
748749
self._plotlyjs_url = url
749750

750751
@property
751-
def layout(self):
752+
def layout(self) -> Any:
752753
return self._layout
753754

754755
@layout.setter
755-
def layout(self, value):
756+
def layout(self, value: Any):
756757
_validate.validate_layout_type(value)
757758
self._layout_is_function = callable(value)
758759
self._layout = value
@@ -782,11 +783,11 @@ def _layout_value(self):
782783
return layout
783784

784785
@property
785-
def index_string(self):
786+
def index_string(self) -> str:
786787
return self._index_string
787788

788789
@index_string.setter
789-
def index_string(self, value):
790+
def index_string(self, value: str) -> None:
790791
checks = (_re_index_entry, _re_index_config, _re_index_scripts)
791792
_validate.validate_index("index string", checks, value)
792793
self._index_string = value
@@ -861,7 +862,7 @@ def serve_reload_hash(self):
861862
}
862863
)
863864

864-
def get_dist(self, libraries):
865+
def get_dist(self, libraries: Sequence[str]) -> list:
865866
dists = []
866867
for dist_type in ("_js_dist", "_css_dist"):
867868
resources = ComponentRegistry.get_resources(dist_type, libraries)
@@ -963,7 +964,7 @@ def _generate_css_dist_html(self):
963964
]
964965
)
965966

966-
def _generate_scripts_html(self):
967+
def _generate_scripts_html(self) -> str:
967968
# Dash renderer has dependencies like React which need to be rendered
968969
# before every other script. However, the dash renderer bundle
969970
# itself needs to be rendered after all of the component's
@@ -1020,10 +1021,10 @@ def _generate_scripts_html(self):
10201021
+ [f"<script>{src}</script>" for src in self._inline_scripts]
10211022
)
10221023

1023-
def _generate_config_html(self):
1024+
def _generate_config_html(self) -> str:
10241025
return f'<script id="_dash-config" type="application/json">{to_json(self._config())}</script>'
10251026

1026-
def _generate_renderer(self):
1027+
def _generate_renderer(self) -> str:
10271028
return f'<script id="_dash-renderer" type="application/javascript">{self.renderer}</script>'
10281029

10291030
def _generate_meta(self):
@@ -1545,7 +1546,7 @@ def _serve_default_favicon():
15451546
pkgutil.get_data("dash", "favicon.ico"), content_type="image/x-icon"
15461547
)
15471548

1548-
def csp_hashes(self, hash_algorithm="sha256"):
1549+
def csp_hashes(self, hash_algorithm="sha256") -> Sequence[str]:
15491550
"""Calculates CSP hashes (sha + base64) of all inline scripts, such that
15501551
one of the biggest benefits of CSP (disallowing general inline scripts)
15511552
can be utilized together with Dash clientside callbacks (inline scripts).
@@ -1584,7 +1585,7 @@ def _hash(script):
15841585
for script in (self._inline_scripts + [self.renderer])
15851586
]
15861587

1587-
def get_asset_url(self, path):
1588+
def get_asset_url(self, path: str) -> str:
15881589
"""
15891590
Return the URL for the provided `path` in the assets directory.
15901591
@@ -1655,7 +1656,7 @@ def display_content(path):
16551656
self.config.requests_pathname_prefix, path
16561657
)
16571658

1658-
def strip_relative_path(self, path):
1659+
def strip_relative_path(self, path: str) -> Union[str, None]:
16591660
"""
16601661
Return a path with `requests_pathname_prefix` and leading and trailing
16611662
slashes stripped from it. Also, if None is passed in, None is returned.
@@ -1707,7 +1708,9 @@ def display_content(path):
17071708
)
17081709

17091710
@staticmethod
1710-
def add_startup_route(name, view_func, methods):
1711+
def add_startup_route(
1712+
name: str, view_func: RouteCallable, methods: Sequence[Literal["POST", "GET"]]
1713+
) -> None:
17111714
"""
17121715
Add a route to the app to be initialized at the end of Dash initialization.
17131716
Use this if the package requires a route to be added to the app, and you will not need to worry about at what point to add it.
@@ -1731,7 +1734,7 @@ def add_startup_route(name, view_func, methods):
17311734

17321735
Dash.STARTUP_ROUTES.append((name, view_func, methods))
17331736

1734-
def setup_startup_routes(self):
1737+
def setup_startup_routes(self) -> None:
17351738
"""
17361739
Initialize the startup routes stored in STARTUP_ROUTES.
17371740
"""
@@ -1774,18 +1777,18 @@ def _setup_dev_tools(self, **kwargs):
17741777

17751778
def enable_dev_tools(
17761779
self,
1777-
debug=None,
1778-
dev_tools_ui=None,
1779-
dev_tools_props_check=None,
1780-
dev_tools_serve_dev_bundles=None,
1781-
dev_tools_hot_reload=None,
1782-
dev_tools_hot_reload_interval=None,
1783-
dev_tools_hot_reload_watch_interval=None,
1784-
dev_tools_hot_reload_max_retry=None,
1785-
dev_tools_silence_routes_logging=None,
1786-
dev_tools_disable_version_check=None,
1787-
dev_tools_prune_errors=None,
1788-
):
1780+
debug: Optional[bool] = None,
1781+
dev_tools_ui: Optional[bool] = None,
1782+
dev_tools_props_check: Optional[bool] = None,
1783+
dev_tools_serve_dev_bundles: Optional[bool] = None,
1784+
dev_tools_hot_reload: Optional[bool] = None,
1785+
dev_tools_hot_reload_interval: Optional[int] = None,
1786+
dev_tools_hot_reload_watch_interval: Optional[int] = None,
1787+
dev_tools_hot_reload_max_retry: Optional[int] = None,
1788+
dev_tools_silence_routes_logging: Optional[bool] = None,
1789+
dev_tools_disable_version_check: Optional[bool] = None,
1790+
dev_tools_prune_errors: Optional[bool] = None,
1791+
) -> None:
17891792
"""Activate the dev tools, called by `run`. If your application
17901793
is served by wsgi and you want to activate the dev tools, you can call
17911794
this method out of `__main__`.
@@ -2275,7 +2278,7 @@ def verify_url_part(served_part, url_part, part_name):
22752278
else:
22762279
self.server.run(host=host, port=port, debug=debug, **flask_run_options)
22772280

2278-
def enable_pages(self):
2281+
def enable_pages(self) -> None:
22792282
if not self.use_pages:
22802283
return
22812284
if self.pages_folder:

0 commit comments

Comments
 (0)