Skip to content
Myr

:bug: Fix extracting help from docstrings in functions

Before adoption: this commit came in with the history the project adopted, so no landing records it.

handling defaults when including Typer groups and explicit None

authored by Sebastián Ramírez · committed by Sebastián Ramírez · parent a650881 · Browse files · 2 files+60 added−15 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+60 added−15 removed

  • other:+60 added−15 removed

2 files+60 added−15 removed

modified:typer/completion.py+3 added-2 removed
1import sys2from typing import Any34import click5import click_completion
8 unmodified lines
14 if not value or ctx.resilient_parsing:15 return value16 shell, path = click_completion.core.install()17 click.echo(f"{shell} completion installed in {path}")17 exit(0)18 sys.exit(0)192021def show_callback(ctx: click.Context, param: click.Parameter, value: Any) -> Any:22 if not value or ctx.resilient_parsing:23 return value24 click.echo(click_completion.core.get_code())24 exit(0)25 sys.exit(0)262728# Create a fake command function to extract the completion parameters29def _install_completion_placeholder_function(
18 unmodified lines
modified:typer/main.py+57 added-13 removed
257 unmodified lines
258 return get_command_name(typer_info.typer_instance.info.callback.__name__)259 return None260261262def solve_typer_info_help(typer_info: TyperInfo) -> str:263 # Priority 1a: Value was set in app.add_typer()264 if not isinstance(typer_info.help, DefaultPlaceholder):265 return inspect.cleandoc(typer_info.help or "")266 # Priority 1b: Value was set in app.add_typer(), in callback docstring267 if typer_info.callback:268 doc = inspect.getdoc(typer_info.callback)269 if doc:270 return doc271 try:272 # Priority 2a: Value was set in @subapp.callback()273 doc = typer_info.typer_instance.registered_callback.help274 if not isinstance(doc, DefaultPlaceholder):275 return inspect.cleandoc(doc or "")276 # Priority 2b: Value was set in @subapp.callback(), in callback docstring277 doc = inspect.getdoc(typer_info.typer_instance.registered_callback.callback)278 if doc:279 return doc280 except AttributeError:281 pass282 try:283 # Priority 3a: Value set in subapp = typer.Typer()284 instance_value = typer_info.typer_instance.info.help285 if not isinstance(instance_value, DefaultPlaceholder):286 return inspect.cleandoc(instance_value or "")287 doc = inspect.getdoc(typer_info.typer_instance.callback)288 if doc:289 return doc290 except AttributeError:291 pass292 # Value not set, use the default293 return typer_info.help.value294295296def solve_typer_info_defaults(typer_info: TyperInfo) -> TyperInfo:263 values = {}297 values: Dict[str, Any] = {}298 values["help"] = solve_typer_info_help(typer_info)299 name = None300 for name, value in typer_info.__dict__.items():301 # Priority 1: Value was set in app.add_typer()302 if not isinstance(value, DefaultPlaceholder):303 values[name] = value304 continue270 if typer_info.typer_instance:271 if typer_info.typer_instance.registered_callback:272 callback_value = getattr(273 typer_info.typer_instance.registered_callback, name274 )275 # Priority 2: Value was set in @subapp.callback()276 if not isinstance(callback_value, DefaultPlaceholder):277 values[name] = callback_value278 continue279 instance_value = getattr(typer_info.typer_instance.info, name)280 # Priority 3: Value set in subapp = typer.Typer()305 # Priority 2: Value was set in @subapp.callback()306 try:307 callback_value = getattr(308 typer_info.typer_instance.registered_callback, name # type: ignore309 )310 if not isinstance(callback_value, DefaultPlaceholder):311 values[name] = callback_value312 continue313 except AttributeError:314 pass315 # Priority 3: Value set in subapp = typer.Typer()316 try:317 instance_value = getattr(typer_info.typer_instance.info, name) # type: ignore318 if not isinstance(instance_value, DefaultPlaceholder):319 values[name] = instance_value320 continue321 except AttributeError:322 pass323 # Value not set, use the default324 values[name] = value.value325 if values["name"] is None:326 values["name"] = get_group_name(typer_info)
70 unmodified lines
397398def get_command_from_info(command_info: CommandInfo) -> click.Command:399 assert command_info.callback, "A command must have a callback function"400 name = command_info.name or get_command_name(command_info.callback.__name__)401 use_help = command_info.help402 if use_help is None:403 use_help = inspect.getdoc(command_info.callback)404 else:405 use_help = inspect.cleandoc(use_help)406 (407 params,408 convertors,409 context_param_name,
8 unmodified lines
418 convertors=convertors,419 context_param_name=context_param_name,420 ),421 params=params, # type: ignore378 help=command_info.help,422 help=use_help,423 epilog=command_info.epilog,424 short_help=command_info.short_help,425 options_metavar=command_info.options_metavar,426 add_help_option=command_info.add_help_option,
288 unmodified lines