Skip to content

Commit eaaf86e

Browse files
authored
Use more consistent types for static_assets (#1171)
1 parent f0a05fa commit eaaf86e

File tree

2 files changed

+16
-8
lines changed

2 files changed

+16
-8
lines changed

shiny/_app.py

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ def __init__(
113113
Callable[[Inputs], None] | Callable[[Inputs, Outputs, Session], None] | None
114114
),
115115
*,
116-
static_assets: Optional["str" | "os.PathLike[str]" | dict[str, Path]] = None,
116+
static_assets: Optional[str | Path | dict[str, str | Path]] = None,
117117
debug: bool = False,
118118
) -> None:
119119
# Used to store callbacks to be called when the app is shutting down (according
@@ -142,20 +142,28 @@ def __init__(
142142

143143
if static_assets is None:
144144
static_assets = {}
145-
if isinstance(static_assets, (str, os.PathLike)):
146-
if not os.path.isabs(static_assets):
145+
146+
if isinstance(static_assets, dict):
147+
static_assets_map = {k: Path(v) for k, v in static_assets.items()}
148+
else:
149+
static_assets_map = {"/": Path(static_assets)}
150+
151+
for _, static_asset_path in static_assets_map.items():
152+
if not static_asset_path.is_absolute():
147153
raise ValueError(
148-
f"static_assets must be an absolute path: {static_assets}"
154+
f'static_assets must be an absolute path: "{static_asset_path}".'
155+
" Consider using one of the following instead:\n"
156+
f' os.path.join(__file__, "{static_asset_path}") OR'
157+
f' pathlib.Path(__file__).parent/"{static_asset_path}"'
149158
)
150-
static_assets = {"/": Path(static_assets)}
151159

152160
# Sort the static assets keys by descending length, to ensure that the most
153161
# specific paths are mounted first. Suppose there are mounts "/foo" and "/". If
154162
# "/" is first in the dict, then requests to "/foo/file.html" will never reach
155163
# the second mount. We need to put "/foo" first and "/" second so that it will
156164
# actually look in the "/foo" mount.
157-
static_assets = sort_keys_length(static_assets, descending=True)
158-
self._static_assets: dict[str, Path] = static_assets
165+
static_assets_map = sort_keys_length(static_assets_map, descending=True)
166+
self._static_assets: dict[str, Path] = static_assets_map
159167

160168
self._sessions: dict[str, Session] = {}
161169

shiny/express/_run.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ def express_server(input: Inputs, output: Outputs, session: Session):
8585
app = App(
8686
app_ui,
8787
express_server,
88-
**app_opts,
88+
**app_opts, # pyright: ignore[reportArgumentType]
8989
)
9090

9191
return app

0 commit comments

Comments
 (0)