Skip to content

Commit a0209aa

Browse files
Asthestarsfalllpre-commit-ci[bot]svlandeg
authored
🐛 Add support for an argument of type Optional[Tuple] and default value None (#757)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: svlandeg <[email protected]>
1 parent 5cee0af commit a0209aa

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

tests/test_type_conversion.py

+19
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,25 @@ def opt(user: Optional[str] = None):
2929
assert "User: Camila" in result.output
3030

3131

32+
def test_optional_tuple():
33+
app = typer.Typer()
34+
35+
@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")
41+
42+
result = runner.invoke(app)
43+
assert result.exit_code == 0
44+
assert "No number" in result.output
45+
46+
result = runner.invoke(app, ["--number", "4", "2"])
47+
assert result.exit_code == 0
48+
assert "Number: (4, 2)" in result.output
49+
50+
3251
def test_no_type():
3352
app = typer.Typer()
3453

typer/main.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -645,10 +645,14 @@ def internal_convertor(value: Sequence[Any]) -> Optional[List[Any]]:
645645

646646
def generate_tuple_convertor(
647647
types: Sequence[Any],
648-
) -> Callable[[Tuple[Any, ...]], Tuple[Any, ...]]:
648+
) -> Callable[[Optional[Tuple[Any, ...]]], Optional[Tuple[Any, ...]]]:
649649
convertors = [determine_type_convertor(type_) for type_ in types]
650650

651-
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 None
652656
return tuple(
653657
convertor(arg) if convertor else arg
654658
for (convertor, arg) in zip(convertors, param_args)

0 commit comments

Comments
 (0)