Skip to content
Myr

🐛 Ensure that `Optional[list]` values work correctly with callbacks (#1018)

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

authored by Dima Goncharenko · committed by GitHub · parent 13619fb · Browse files · 2 files+30 added−4 removed
This commit

State

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

Trailers

  • Co-authored-by: svlandeg <svlandeg@github.com>
  • Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Co-authored-by: Sofie Van Landeghem <svlandeg@users.noreply.github.com>

Diff stats

2 files+30 added−4 removed

  • tests:+26 added−0 removed
  • other:+4 added−4 removed

2 files+30 added−4 removed

modified:tests/test_others.py+26 added
142 unmodified lines
143 assert "param name is: name" in result.stdout144 assert "value is: Camila" in result.stdout145146147def test_callback_4_list_none():148 app = typer.Typer()149150 def names_callback(ctx, param, values: typing.Optional[typing.List[str]]):151 if values is None:152 return values153 return [value.upper() for value in values]154155 @app.command()156 def main(157 names: typing.Optional[typing.List[str]] = typer.Option(158 None, "--name", callback=names_callback159 ),160 ):161 if names is None:162 print("Hello World")163 else:164 print(f"Hello {', '.join(names)}")165166 result = runner.invoke(app, ["--name", "Sideshow", "--name", "Bob"])167 assert "Hello SIDESHOW, BOB" in result.stdout168169 result = runner.invoke(app, [])170 assert "Hello World" in result.stdout171172173def test_completion_argument():174 file_path = Path(__file__).parent / "assets/completion_argument.py"175 result = subprocess.run(176 [sys.executable, "-m", "coverage", "run", str(file_path), "E"],
130 unmodified lines
modified:typer/main.py+4 added-4 removed
625 unmodified lines
626627628def generate_list_convertor(629 convertor: Optional[Callable[[Any], Any]], default_value: Optional[Any]630) -> Callable[[Sequence[Any]], Optional[List[Any]]]:631 def internal_convertor(value: Sequence[Any]) -> Optional[List[Any]]:632 if default_value is None and len(value) == 0:633 return None630) -> Callable[[Optional[Sequence[Any]]], Optional[List[Any]]]:631 def internal_convertor(value: Optional[Sequence[Any]]) -> Optional[List[Any]]:632 if value is None or len(value) == 0:633 return default_value634 return [convertor(v) if convertor else v for v in value]635636 return internal_convertor637
499 unmodified lines