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
Changes from 1 commit
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
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