From c362fffecfb37298ac364e76eaf3116776b75feb Mon Sep 17 00:00:00 2001 From: Winston Chang Date: Wed, 17 Jan 2024 15:17:57 -0600 Subject: [PATCH] Provide useful message in Express when `input` was not imported (#994) --- shiny/express/_run.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/shiny/express/_run.py b/shiny/express/_run.py index f3a38e28d..d65190b1e 100644 --- a/shiny/express/_run.py +++ b/shiny/express/_run.py @@ -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 @@ -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`?" + )