Skip to content

Support dynamic default values #114

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

Conversation

daneah
Copy link
Collaborator

@daneah daneah commented Apr 15, 2025

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. Typer sets the option's default on the Click command object to the supplied factory 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.

I don't think I fully understand why this crash occurs, since it appears that Trogon properly handles a default Click application when an option's default is e.g. a lambda. Perhaps Typer is missing something in its translation of default_factory to Click's default, and I'm open to filing an issue/opening a PR on their end if it appears that's the case, but I'd need help in better understanding it!

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.
@daneah daneah requested a review from darrenburns April 15, 2025 14:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant