Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ docs = [
]
dashboard = [
"gradio>=4.0",
# dashboard.py imports pandas directly, without the guard it puts around
# gradio. It only works today because gradio happens to depend on pandas;
# declare it rather than rely on another package's dependency list.
"pandas>=2.0",
]

[project.urls]
Expand Down
37 changes: 37 additions & 0 deletions tests/test_dashboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,3 +246,40 @@ def test_dashboard_websocket_url(self) -> None:

dashboard = Dashboard(host="localhost", port=8080)
assert dashboard.ws_url == "ws://localhost:8080/ws"


class TestDashboardExtra:
"""The `dashboard` extra must declare what dashboard.py imports.

`_create_gradio_app` guards its gradio import and then imports pandas
unguarded. That works only because gradio happens to depend on pandas, so
the extra has been relying on another package's dependency list. If gradio
ever drops it, the failure is a bare ModuleNotFoundError with an unhelpful
"pip install pyetwkit[dashboard]" already satisfied.
"""

def test_extra_declares_every_package_the_dashboard_imports(self) -> None:
import importlib.metadata
import re

requires = importlib.metadata.requires("pyetwkit") or []
# The marker is quoted differently depending on which build produced the
# metadata, so match either form rather than one of them.
marker = re.compile(r"""extra\s*==\s*['"]dashboard['"]""")
declared = {
re.split(r"[<>=!~\[ ;]", requirement)[0].strip().lower()
for requirement in requires
if marker.search(requirement)
}

assert "gradio" in declared, sorted(declared)
assert "pandas" in declared, (
"dashboard.py imports pandas unguarded, so the dashboard extra has "
f"to declare it. Declared: {sorted(declared)}"
)

def test_dashboard_module_imports_without_the_extra(self) -> None:
"""Only `_create_gradio_app` needs gradio; importing must not."""
from pyetwkit.dashboard import Dashboard

assert Dashboard is not None
Loading