Skip to content

Commit

Permalink
Raise when express.[input,output,session] are used outside of Express…
Browse files Browse the repository at this point in the history
… app
  • Loading branch information
wch committed Jan 25, 2024
1 parent b6907f7 commit 4879b18
Showing 1 changed file with 31 additions and 3 deletions.
34 changes: 31 additions & 3 deletions shiny/express/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,39 @@
# returns the input for the current session. This will work in the vast majority of
# cases, but when it fails, it will be very confusing.
def __getattr__(name: str) -> object:
session = _get_current_session()

if name == "input":
return _get_current_session().input # pyright: ignore
if session is None:
return _ExpressOnlyPlaceholder("input")
else:
return session.input # pyright: ignore
elif name == "output":
return _get_current_session().output # pyright: ignore
if session is None:
return _ExpressOnlyPlaceholder("output")
else:
return session.output # pyright: ignore
elif name == "session":
return _get_current_session()
if session is None:
return _ExpressOnlyPlaceholder("session")
else:
return session

raise AttributeError(f"Module 'shiny.express' has no attribute '{name}'")


class _ExpressOnlyPlaceholder:
"""Placeholder class for objects that can only be used in a Shiny Express app."""

def __init__(self, name: str):
self.name = name

def __getattr__(self, name: str) -> None:
raise RuntimeError(
f"shiny.express.{self.name} can only be used inside of a Shiny Express app."
)

def __call__(self, *args: object, **kwargs: object) -> None:
raise RuntimeError(
f"shiny.express.{self.name} can only be used inside of a Shiny Express app."
)

0 comments on commit 4879b18

Please sign in to comment.