Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
9 changes: 8 additions & 1 deletion q2cli/core/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,17 @@ def _special_option_flags(type):
metadata = 'file'
elif expr.name == 'MetadataColumn':
metadata = 'column'
elif expr.name == 'Bool':
elif _is_bool(type):
is_bool_flag = True

return multiple, is_bool_flag, metadata


def _is_bool(type_expr):
return all(t.name == 'Bool' for t in type_expr) \
and not type_expr.is_bottom()


def _get_type_repr(type):
import qiime2.sdk.util

Expand Down Expand Up @@ -157,6 +162,8 @@ def _get_metavar(type):
metavar = 'METADATA'
elif style.style is not None and style.style != 'simple':
metavar = 'VALUE'
elif _is_bool(type):
metavar = ''
elif qiime2.sdk.util.is_union(type):
metavar = 'VALUE'
else:
Expand Down
71 changes: 69 additions & 2 deletions q2cli/tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,19 @@

from click.testing import CliRunner
from qiime2.core.cache import get_cache
from qiime2.core.testing.type import IntSequence1, IntSequence2, SingleInt
from qiime2.core.testing.format import EchoFormat
from qiime2.core.testing.type import (
Bar, C1, Foo, IntSequence1, IntSequence2, SingleInt
)
from qiime2.core.testing.util import get_dummy_plugin
from qiime2.plugin import Bool, Choices, TypeMap
from qiime2.sdk import Artifact, Visualization, ResultCollection

from q2cli.builtin.info import info
from q2cli.builtin.tools import tools
from q2cli.commands import RootCommand
from q2cli.commands import PluginCommand, RootCommand
from q2cli.click.type import QIIME2Type
from q2cli.core.state import get_plugin_state


CONFIG_LEVEL_2 = """[parsl]
Expand Down Expand Up @@ -94,6 +99,30 @@ class = "_TEST_EXECUTOR_"
""" # noqa: E501


def _register_higher_union_bool_flag_action():
def higher_union_bool_flag_swaps_output_method(a: EchoFormat,
b: bool) -> EchoFormat:
return a

plugin = get_dummy_plugin()
action_id = 'higher_union_bool_flag_swaps_output_method'
if action_id not in plugin.actions:
P, R = TypeMap({
Bool % Choices(True): C1[Foo],
Bool % Choices(False): Foo,
})
plugin.methods.register_function(
function=higher_union_bool_flag_swaps_output_method,
inputs={'a': Bar},
parameters={'b': P},
outputs=[('x', R)],
name='Higher Union Bool Flag Swaps Output Method',
description=(
'Test if a higher-union bool parameter can change output'),
)
return plugin


class CliTests(unittest.TestCase):
def setUp(self):
get_dummy_plugin()
Expand Down Expand Up @@ -233,6 +262,44 @@ def test_action_parameter_types(self):
'10', '--output-dir', output_dir, '--verbose'])
self.assertEqual(result.exit_code, 0)

def test_higher_union_bool_typemap_parameter_is_flag(self):
plugin = _register_higher_union_bool_flag_action()
command = PluginCommand(get_plugin_state(plugin), 'dummy-plugin')

results = self.runner.invoke(
command, [
'higher-union-bool-flag-swaps-output-method', '--help'])
self.assertEqual(results.exit_code, 0)
self.assertIn('--p-b / --p-no-b', results.output)

input_path = Artifact.import_data(
'Bar', 'element', view_type=str).save(
os.path.join(self.tempdir, 'bar.qza'))
true_output_path = os.path.join(self.tempdir, 'true-output.qza')
false_output_path = os.path.join(self.tempdir, 'false-output.qza')

result = self.runner.invoke(
command, [
'higher-union-bool-flag-swaps-output-method',
'--i-a', input_path,
'--p-b',
'--o-x', true_output_path,
'--verbose'
])
self.assertEqual(result.exit_code, 0)
self.assertEqual(repr(Artifact.load(true_output_path).type), 'C1[Foo]')

result = self.runner.invoke(
command, [
'higher-union-bool-flag-swaps-output-method',
'--i-a', input_path,
'--p-no-b',
'--o-x', false_output_path,
'--verbose'
])
self.assertEqual(result.exit_code, 0)
self.assertEqual(repr(Artifact.load(false_output_path).type), 'Foo')

def test_execute_hidden_action(self):
int_path = os.path.join(self.tempdir, 'int.qza')
qiime_cli = RootCommand()
Expand Down
Loading