Skip to content

Provide useful message in Express when input was not imported #994

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 17, 2024
Merged
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
14 changes: 14 additions & 0 deletions shiny/express/_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,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 @@ -150,3 +151,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`?"
)