Skip to content

Commit c362fff

Browse files
authored
Provide useful message in Express when input was not imported (#994)
1 parent c3bb9ff commit c362fff

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

shiny/express/_run.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ def set_result(x: object):
8484
var_context: dict[str, object] = {
8585
"__file__": file_path,
8686
display_decorator_func_name: _display_decorator_function_def,
87+
"input": InputNotImportedShim(),
8788
}
8889

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

145146
def get_top_level_recall_context_manager() -> RecallContextManager[Tag]:
146147
return _top_level_recall_context_manager
148+
149+
150+
class InputNotImportedShim:
151+
# This is a dummy class that is used to provide a helpful error message when the
152+
# user tries to access `input.x` but forgot to import `input`. If they do that, then
153+
# it would get the builtin `input` function, and print an unhelpful error message:
154+
# RuntimeError: 'builtin_function_or_method' object has no attribute 'x'
155+
# This class provides an error message that is more helpful.
156+
def __getattr__(self, name: str):
157+
raise AttributeError(
158+
"Tried to access `input`, but it was not imported. "
159+
"Perhaps you need `from shiny.express import input`?"
160+
)

0 commit comments

Comments
 (0)