Skip to content

Commit dacf692

Browse files
Add dependency on component stub packages (#1725)
* Prevent warning when using built-in dash components, when stubs are installed * Add stub dependencies * Updating package stubs * Updating version for component error test * Fixing dependency for CI test Co-authored-by: Hammad Khan <[email protected]> Co-authored-by: HammadTheOne <[email protected]>
1 parent 421c3e4 commit dacf692

File tree

4 files changed

+35
-9
lines changed

4 files changed

+35
-9
lines changed

.circleci/config.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -494,7 +494,6 @@ jobs:
494494
pip install -r dev-requirements.txt --quiet
495495
pip install -r python-requirements.txt --quiet
496496
pip install dash-package/dash-package.tar.gz[dev,testing]
497-
pip uninstall -y dash-html-components dash-core-components
498497
- run:
499498
name: Build
500499
command: |

dash/development/base_component.py

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ def __str__(self):
7878
REQUIRED = _REQUIRED()
7979

8080
def __init__(self, **kwargs):
81+
import dash # pylint: disable=import-outside-toplevel, cyclic-import
82+
8183
# pylint: disable=super-init-not-called
8284
for k, v in list(kwargs.items()):
8385
# pylint: disable=no-member
@@ -88,12 +90,33 @@ def __init__(self, **kwargs):
8890
# e.g. "The dash_core_components.Dropdown component (version 1.6.0)
8991
# with the ID "my-dropdown"
9092
try:
91-
error_string_prefix = "The `{}.{}` component (version {}){}".format(
92-
self._namespace,
93-
self._type,
94-
getattr(__import__(self._namespace), "__version__", "unknown"),
95-
' with the ID "{}"'.format(kwargs["id"]) if "id" in kwargs else "",
96-
)
93+
# Get fancy error strings that have the version numbers
94+
error_string_prefix = "The `{}.{}` component (version {}){}"
95+
# These components are part of dash now, so extract the dash version:
96+
dash_packages = {
97+
"dash_html_components": "html",
98+
"dash_core_components": "dcc",
99+
"dash_table": "dash_table",
100+
}
101+
if self._namespace in dash_packages:
102+
error_string_prefix = error_string_prefix.format(
103+
dash_packages[self._namespace],
104+
self._type,
105+
dash.__version__,
106+
' with the ID "{}"'.format(kwargs["id"])
107+
if "id" in kwargs
108+
else "",
109+
)
110+
else:
111+
# Otherwise import the package and extract the version number
112+
error_string_prefix = error_string_prefix.format(
113+
self._namespace,
114+
self._type,
115+
getattr(__import__(self._namespace), "__version__", "unknown"),
116+
' with the ID "{}"'.format(kwargs["id"])
117+
if "id" in kwargs
118+
else "",
119+
)
97120
except ImportError:
98121
# Our tests create mock components with libraries that
99122
# aren't importable

requires-install.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
Flask>=1.0.4
22
flask-compress
33
plotly>=5.0.0
4+
dash_html_components==2.0.0
5+
dash_core_components==2.0.0
6+
dash_table==5.0.0

tests/unit/development/test_base_component.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import plotly
44
import pytest
55

6+
from dash import __version__
67
from dash import html
78
from dash.development.base_component import Component
89

@@ -458,7 +459,7 @@ def test_debc027_component_error_message():
458459
with pytest.raises(TypeError) as e:
459460
html.Div(asdf=True)
460461
assert str(e.value) == (
461-
"The `Div` component "
462+
"The `html.Div` component (version {}) ".format(__version__)
462463
+ "received an unexpected "
463464
+ "keyword argument: `asdf`\n"
464465
+ "Allowed arguments: {}".format(", ".join(sorted(html.Div()._prop_names)))
@@ -467,7 +468,7 @@ def test_debc027_component_error_message():
467468
with pytest.raises(TypeError) as e:
468469
html.Div(asdf=True, id="my-component")
469470
assert str(e.value) == (
470-
"The `Div` component "
471+
"The `html.Div` component (version {}) ".format(__version__)
471472
+ 'with the ID "my-component" received an unexpected '
472473
+ "keyword argument: `asdf`\n"
473474
+ "Allowed arguments: {}".format(", ".join(sorted(html.Div()._prop_names)))

0 commit comments

Comments
 (0)