From 981c48d5189e6af1efebc06c73ede70d355a7eec Mon Sep 17 00:00:00 2001 From: Dane Hillard Date: Tue, 15 Apr 2025 10:03:11 -0400 Subject: [PATCH] Support dynamic default values Typer allows passing a `default_factory` to options and arguments, which is a callable that will be used at runtime to determine the default value. The Textual application created by Trogon crashes when this is used, because it tries to convert the str representation of the callable function for display when it should be converting the result of calling that function instead. This change ensures dynamic default values do not crash the application, and has the effect of setting the default value appropriately in the TUI. --- trogon/widgets/parameter_controls.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/trogon/widgets/parameter_controls.py b/trogon/widgets/parameter_controls.py index c9f559a..c34457c 100644 --- a/trogon/widgets/parameter_controls.py +++ b/trogon/widgets/parameter_controls.py @@ -157,7 +157,10 @@ def compose(self) -> ComposeResult: for default_value, control_widget in zip( default_value_tuple, widget_group ): - self._apply_default_value(control_widget, default_value) + if isinstance(default_value, Callable): + self._apply_default_value(control_widget, default_value()) + else: + self._apply_default_value(control_widget, default_value) yield control_widget # Keep track of the first control we render, for easy focus if first_focus_control is None: