Skip to content
Myr

🐛 Add support for an argument of type `Optional[Tuple]` and default value `None` (#757)

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

authored by Asthestarsfalll · committed by GitHub · parent 5cee0af · Browse files · 2 files+25 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>
  • Co-authored-by: svlandeg <svlandeg@github.com>

Diff stats

2 files+25 added−2 removed

  • tests:+19 added−0 removed
  • other:+6 added−2 removed

2 files+25 added−2 removed

modified:tests/test_type_conversion.py+19 added
27 unmodified lines
28 assert result.exit_code == 029 assert "User: Camila" in result.output303132def test_optional_tuple():33 app = typer.Typer()3435 @app.command()36 def opt(number: Optional[Tuple[int, int]] = None):37 if number:38 print(f"Number: {number}")39 else:40 print("No number")4142 result = runner.invoke(app)43 assert result.exit_code == 044 assert "No number" in result.output4546 result = runner.invoke(app, ["--number", "4", "2"])47 assert result.exit_code == 048 assert "Number: (4, 2)" in result.output495051def test_no_type():52 app = typer.Typer()5354 @app.command()
95 unmodified lines
modified:typer/main.py+6 added-2 removed
643 unmodified lines
644645646def generate_tuple_convertor(647 types: Sequence[Any],648) -> Callable[[Tuple[Any, ...]], Tuple[Any, ...]]:648) -> Callable[[Optional[Tuple[Any, ...]]], Optional[Tuple[Any, ...]]]:649 convertors = [determine_type_convertor(type_) for type_ in types]650651 def internal_convertor(param_args: Tuple[Any, ...]) -> Tuple[Any, ...]:651 def internal_convertor(652 param_args: Optional[Tuple[Any, ...]],653 ) -> Optional[Tuple[Any, ...]]:654 if param_args is None:655 return None656 return tuple(657 convertor(arg) if convertor else arg658 for (convertor, arg) in zip(convertors, param_args)659 )
408 unmodified lines