Skip to content
Myr

🐛 Fix list convertor with an empty list default factory (#1350)

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

authored by Sofie Van Landeghem · committed by GitHub · parent df2359a · Browse files · 2 files+19 added−2 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: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>

Diff stats

2 files+19 added−2 removed

  • tests:+17 added−0 removed
  • other:+2 added−2 removed

2 files+19 added−2 removed

modified:tests/test_others.py+17 added
12 unmodified lines
13from typer.core import _split_opt14from typer.main import solve_typer_info_defaults, solve_typer_info_help15from typer.models import ParameterInfo, TyperInfo16from typer.testing import CliRunner17from typing_extensions import Annotated1819from .utils import requires_completion_permission2021runner = CliRunner()
148 unmodified lines
170 result = runner.invoke(app, [])171 assert "Hello World" in result.stdout172173174def test_empty_list_default_generator():175 def empty_list() -> typing.List[str]:176 return []177178 app = typer.Typer()179180 @app.command()181 def main(182 names: Annotated[typing.List[str], typer.Option(default_factory=empty_list)],183 ):184 print(names)185186 result = runner.invoke(app)187 assert "[]" in result.output188189190def test_completion_argument():191 file_path = Path(__file__).parent / "assets/completion_argument.py"192 result = subprocess.run(193 [sys.executable, "-m", "coverage", "run", str(file_path), "E"],
130 unmodified lines
modified:typer/main.py+2 added-2 removed
627 unmodified lines
628def generate_list_convertor(629 convertor: Optional[Callable[[Any], Any]], default_value: Optional[Any]630) -> 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_value632 if (value is None) or (default_value is None and len(value) == 0):633 return None634 return [convertor(v) if convertor else v for v in value]635636 return internal_convertor637
499 unmodified lines