Skip to content

Commit

Permalink
Provide useful message in Express when input was not imported (#994)
Browse files Browse the repository at this point in the history
  • Loading branch information
wch authored Jan 17, 2024
1 parent c3bb9ff commit c362fff
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions shiny/express/_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ def set_result(x: object):
var_context: dict[str, object] = {
"__file__": file_path,
display_decorator_func_name: _display_decorator_function_def,
"input": InputNotImportedShim(),
}

# Execute each top-level node in the AST
Expand Down Expand Up @@ -144,3 +145,16 @@ def reset_top_level_recall_context_manager() -> None:

def get_top_level_recall_context_manager() -> RecallContextManager[Tag]:
return _top_level_recall_context_manager


class InputNotImportedShim:
# This is a dummy class that is used to provide a helpful error message when the
# user tries to access `input.x` but forgot to import `input`. If they do that, then
# it would get the builtin `input` function, and print an unhelpful error message:
# RuntimeError: 'builtin_function_or_method' object has no attribute 'x'
# This class provides an error message that is more helpful.
def __getattr__(self, name: str):
raise AttributeError(
"Tried to access `input`, but it was not imported. "
"Perhaps you need `from shiny.express import input`?"
)

0 comments on commit c362fff

Please sign in to comment.