Skip to content

Commit 449fd22

Browse files
committed
feat: replace 'assert' added to satisfy type checking
Based on feedback from @T4rk1n, removed assertions added to satisfy type checker and used other mechanisms instead.
1 parent 993283b commit 449fd22

File tree

6 files changed

+22
-26
lines changed

6 files changed

+22
-26
lines changed

dash/_pages.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from pathlib import Path
99
from os.path import isfile, join
1010
from urllib.parse import parse_qs, unquote
11+
from typing import cast
1112

1213
import flask
1314

@@ -86,8 +87,7 @@ def _infer_path(module_name, template):
8687

8788

8889
def _module_name_is_package(module_name):
89-
file_path = sys.modules[module_name].__file__
90-
assert file_path is not None # to make type checker happy
90+
file_path = cast(str, sys.modules[module_name].__file__) # to satisfy type checking
9191
return module_name in sys.modules and Path(file_path).name == "__init__.py"
9292

9393

@@ -441,11 +441,8 @@ def _import_layouts_from_pages(pages_folder):
441441

442442
module_name = _infer_module_name(page_path)
443443
spec = importlib.util.spec_from_file_location(module_name, page_path)
444-
assert (
445-
spec is not None and spec.loader is not None
446-
) # to satisfy type checking
447-
page_module = importlib.util.module_from_spec(spec)
448-
spec.loader.exec_module(page_module)
444+
page_module = importlib.util.module_from_spec(spec) # type: ignore[reportArgumentType]
445+
spec.loader.exec_module(page_module) # type: ignore[reportOptionalMemberAccess]
449446
sys.modules[module_name] = page_module
450447

451448
if (

dash/background_callback/managers/celery_manager.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,8 +157,7 @@ def _set_props(_id, props):
157157
ctx = copy_context()
158158

159159
def run():
160-
assert isinstance(context, dict) # to help type checking
161-
c = AttributeDict(**context)
160+
c = AttributeDict(**context) # type: ignore[reportCallIssue]
162161
c.ignore_register_page = False
163162
c.updated_props = ProxySetProps(_set_props)
164163
context_value.set(c)

dash/dash.py

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

2323
import flask
2424

@@ -452,7 +452,7 @@ def __init__( # pylint: disable=too-many-statements
452452
url_base_pathname, routes_pathname_prefix, requests_pathname_prefix
453453
)
454454

455-
assert isinstance(name, str) # to satisfy type checking
455+
name = cast(str, name) # to satisfy type checking
456456
self.config = AttributeDict(
457457
name=name,
458458
assets_folder=os.path.join(
@@ -767,8 +767,7 @@ def layout(self, value):
767767

768768
def _layout_value(self):
769769
if self._layout_is_function:
770-
assert callable(self._layout)
771-
layout = self._layout()
770+
layout = self._layout() # type: ignore[reportOptionalCall]
772771
else:
773772
layout = self._layout
774773

dash/development/base_component.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -301,12 +301,10 @@ def _get_set_or_delete(self, id, operation, new_item=None):
301301
if operation == "get":
302302
return item
303303
if operation == "set":
304-
assert self.children is not None # to satisfy type checking
305-
self.children[i] = new_item
304+
self.children[i] = new_item # type: ignore[reportOptionalSubscript]
306305
return
307306
if operation == "delete":
308-
assert self.children is not None # to satisfy type checking
309-
del self.children[i]
307+
del self.children[i] # type: ignore[reportOptionalSubscript]
310308
return
311309

312310
# Otherwise, recursively dig into that item's subtree

dash/testing/application_runners.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import logging
1010
import inspect
1111
import ctypes
12+
from typing import cast
1213

1314
import runpy
1415
import requests
@@ -353,15 +354,16 @@ def start(self, app, start_timeout=2, cwd=None): # type: ignore[reportIncompati
353354
# app is a string chunk, we make a temporary folder to store app.R
354355
# and its relevant assets
355356
tmp_dir = "/tmp" if not self.is_windows else os.getenv("TEMP")
356-
assert isinstance(tmp_dir, str)
357+
tmp_dir = cast(str, tmp_dir) # to satisfy type checking
357358
hex_id = uuid.uuid4().hex
358-
self._tmp_app_path = os.path.join(tmp_dir, hex_id)
359-
assert isinstance(self.tmp_app_path, str) # to satisfy type checking
359+
self._tmp_app_path = cast(
360+
str, os.path.join(tmp_dir, hex_id)
361+
) # to satisfy type checking
360362
try:
361-
os.mkdir(self.tmp_app_path)
363+
os.mkdir(self.tmp_app_path) # type: ignore[reportArgumentType]
362364
except OSError:
363365
logger.exception("cannot make temporary folder %s", self.tmp_app_path)
364-
path = os.path.join(self.tmp_app_path, "app.R")
366+
path = os.path.join(self.tmp_app_path, "app.R") # type: ignore[reportCallIssue]
365367

366368
logger.info("RRunner start => app is R code chunk")
367369
logger.info("make a temporary R file for execution => %s", path)
@@ -392,7 +394,7 @@ def start(self, app, start_timeout=2, cwd=None): # type: ignore[reportIncompati
392394
]
393395

394396
for asset in assets:
395-
target = os.path.join(self.tmp_app_path, os.path.basename(asset))
397+
target = os.path.join(self.tmp_app_path, os.path.basename(asset)) # type: ignore[reportCallIssue]
396398
if os.path.exists(target):
397399
logger.debug("delete existing target %s", target)
398400
shutil.rmtree(target)

dash/testing/browser.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import sys
44
import time
55
import logging
6+
from typing import cast
67
import warnings
78
import percy
89
import requests
@@ -119,7 +120,7 @@ def visit_and_snapshot(
119120
try:
120121
if path != resource_path:
121122
logger.warning("we stripped the left '/' in resource_path")
122-
assert isinstance(self.server_url, str) # to satisfy type checking
123+
self.server_url = cast(str, self.server_url) # to satisfy type checking
123124
self.driver.get(f"{self.server_url.rstrip('/')}/{path}")
124125

125126
# wait for the hook_id to present and all callbacks get fired
@@ -228,7 +229,7 @@ def take_snapshot(self, name):
228229
running selenium session id
229230
"""
230231
target = "/tmp/dash_artifacts" if not self._is_windows() else os.getenv("TEMP")
231-
assert isinstance(target, str) # to satisfy type checking
232+
target = cast(str, target) # to satisfy type checking
232233
if not os.path.exists(target):
233234
try:
234235
os.mkdir(target)
@@ -533,7 +534,7 @@ def _get_firefox(self):
533534
"browser.helperApps.neverAsk.saveToDisk",
534535
"application/octet-stream", # this MIME is generic for binary
535536
)
536-
assert isinstance(self._remote_url, str) # to satisfy type checking
537+
self._remote_url = cast(str, self._remote_url) # to satisfy type checking
537538
return (
538539
webdriver.Remote(
539540
command_executor=self._remote_url,

0 commit comments

Comments
 (0)