Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 Fix default value of None for CLI Parameters when the type is list | None and the default value is None #664

Merged
merged 5 commits into from
Mar 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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