Skip to content

Commit

Permalink
šŸ› Fix default value of None for CLI Parameters when the type is `liā€¦
Browse files Browse the repository at this point in the history
ā€¦st | None` and the default value is `None` (#664)

Co-authored-by: svlandeg <svlandeg@github.com>
Co-authored-by: SebastiƔn Ramƭrez <tiangolo@gmail.com>
  • Loading branch information
3 people committed Mar 23, 2024
1 parent 3ad8f69 commit cf3290d
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 7 deletions.
3 changes: 2 additions & 1 deletion docs/tutorial/multiple-values/multiple-options.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@ Check it:
<div class="termy">

```console
// The default value is 'None'
$ python main.py

No provided users
No provided users (raw input = None)
Aborted!

// Now pass a user
Expand Down
2 changes: 1 addition & 1 deletion docs_src/multiple_values/multiple_options/tutorial001.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

def main(user: Optional[List[str]] = typer.Option(None)):
if not user:
print("No provided users")
print(f"No provided users (raw input = {user})")
raise typer.Abort()
for u in user:
print(f"Processing user: {u}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

def main(user: Annotated[Optional[List[str]], typer.Option()] = None):
if not user:
print("No provided users")
print(f"No provided users (raw input = {user})")
raise typer.Abort()
for u in user:
print(f"Processing user: {u}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ def test_main():
result = runner.invoke(app)
assert result.exit_code != 0
assert "No provided users" in result.output
assert "raw input = None" in result.output
assert "Aborted" in result.output


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ def test_main():
result = runner.invoke(app)
assert result.exit_code != 0
assert "No provided users" in result.output
assert "raw input = None" in result.output
assert "Aborted" in result.output


Expand Down
12 changes: 8 additions & 4 deletions typer/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -631,9 +631,11 @@ def convertor(value: Any) -> Any:


def generate_list_convertor(
convertor: Optional[Callable[[Any], Any]]
) -> Callable[[Sequence[Any]], List[Any]]:
def internal_convertor(value: Sequence[Any]) -> List[Any]:
convertor: Optional[Callable[[Any], Any]], default_value: Optional[Any]
) -> Callable[[Sequence[Any]], Optional[List[Any]]]:
def internal_convertor(value: Sequence[Any]) -> Optional[List[Any]]:
if default_value is None and len(value) == 0:
return None
return [convertor(v) if convertor else v for v in value]

return internal_convertor
Expand Down Expand Up @@ -852,7 +854,9 @@ def get_click_param(
)
convertor = determine_type_convertor(main_type)
if is_list:
convertor = generate_list_convertor(convertor)
convertor = generate_list_convertor(
convertor=convertor, default_value=default_value
)
if is_tuple:
convertor = generate_tuple_convertor(main_type.__args__)
if isinstance(parameter_info, OptionInfo):
Expand Down

0 comments on commit cf3290d

Please sign in to comment.