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

GH-5768: Better pyflyte boolean parsing #2764

Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions flytekit/clis/sdk_in_container/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -449,10 +449,14 @@ def to_click_option(
# If a query has been specified, the input is never strictly required at this layer
required = False if default_val and isinstance(default_val, ArtifactQuery) else required

if literal_converter.is_bool():
click_cli_paramater_name = f"--{input_name}/--no_{input_name}"
Tom-Newton marked this conversation as resolved.
Show resolved Hide resolved
Tom-Newton marked this conversation as resolved.
Show resolved Hide resolved
else:
click_cli_paramater_name = f"--{input_name}"
Tom-Newton marked this conversation as resolved.
Show resolved Hide resolved

return click.Option(
param_decls=[f"--{input_name}"],
param_decls=[click_cli_paramater_name],
type=literal_converter.click_type,
is_flag=literal_converter.is_bool(),
default=default_val,
show_default=True,
required=required,
Expand Down
37 changes: 37 additions & 0 deletions tests/flytekit/unit/cli/pyflyte/test_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,37 @@ def test_union_type1(input):
assert result.exit_code == 0


@pytest.mark.parametrize(
"extra_cli_args, task_name, expected_output",
[
(("--a",), "test_boolean", True),
(("--no_a",), "test_boolean", False),

(tuple(), "test_boolean_default_true", True),
(("--a",), "test_boolean_default_true", True),
(("--no_a",), "test_boolean_default_true", False),

(tuple(), "test_boolean_default_false", False),
(("--a",), "test_boolean_default_false", True),
(("--no_a",), "test_boolean_default_false", False),
],
)
def test_boolean_type(extra_cli_args, task_name, expected_output):
runner = CliRunner()
result = runner.invoke(
pyflyte.main,
[
"run",
os.path.join(DIR_NAME, "workflow.py"),
task_name,
*extra_cli_args,
],
catch_exceptions=False,
)
assert result.exit_code == 0
assert str(expected_output) in result.stdout


def test_all_types_with_json_input():
runner = CliRunner()
result = runner.invoke(
Expand Down Expand Up @@ -391,6 +422,9 @@ def test_get_entities_in_file(workflow_file):
"task_with_env_vars",
"task_with_list",
"task_with_optional",
"test_boolean",
"test_boolean_default_false",
"test_boolean_default_true",
"test_union1",
"test_union2",
]
Expand All @@ -405,6 +439,9 @@ def test_get_entities_in_file(workflow_file):
"task_with_env_vars",
"task_with_list",
"task_with_optional",
"test_boolean",
"test_boolean_default_false",
"test_boolean_default_true",
"test_union1",
"test_union2",
]
Expand Down
12 changes: 12 additions & 0 deletions tests/flytekit/unit/cli/pyflyte/workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,18 @@ def test_union1(a: typing.Union[int, FlyteFile, typing.Dict[str, float], datetim
def test_union2(a: typing.Union[float, typing.List[int], MyDataclass]):
print(a)

@task
def test_boolean(a: bool):
print(a)

@task
def test_boolean_default_true(a: bool = True):
print(a)

@task
def test_boolean_default_false(a: bool = False):
print(a)


@workflow
def my_wf(
Expand Down
Loading