From f082537387bdfd642eaf92ae804a7ca4caba7a68 Mon Sep 17 00:00:00 2001 From: Matthias Bernt Date: Fri, 28 Aug 2020 09:37:42 +0200 Subject: [PATCH 1/2] try to refine type by checking for subclasses --- acclimatise/model.py | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/acclimatise/model.py b/acclimatise/model.py index e5e088e..1b9c0a1 100644 --- a/acclimatise/model.py +++ b/acclimatise/model.py @@ -293,15 +293,21 @@ def _name_from_arg(self) -> typing.Iterable[str]: def get_type(self) -> cli_types.CliType: # Try the argument name, then the flag name, then the description in that order - arg_type = self.args.get_type() - if arg_type is not None: - return arg_type - - flag_type = infer_type(self.full_name()) - if flag_type is not None: - return flag_type - - return infer_type(self.description) or cli_types.CliString() + tpe = None + tpe_cand = [ + self.args.get_type(), + infer_type(self.full_name()), + infer_type(self.description), + cli_types.CliString() + ] + for c in tpe_cand: + if tpe is None: + if c is not None: + tpe = c + else: + if issubclass(c, tpe): + tpe = c + return tpe def full_name(self) -> str: """ From 747eebdb53d952e6c390b98a6610c2c881322262 Mon Sep 17 00:00:00 2001 From: Matthias Bernt Date: Fri, 28 Aug 2020 11:47:36 +0200 Subject: [PATCH 2/2] use isinstance instead of issubclass since in/out file/dir is represented by properties of the same class - also fixes linter errors --- acclimatise/cli.py | 8 ++++++-- acclimatise/converter/wdl.py | 7 ++++++- acclimatise/model.py | 4 ++-- acclimatise/usage_parser/elements.py | 6 +++++- test/executors/test_docker.py | 12 +++++++++--- test/name_generation/test_single_flag.py | 6 +++++- test/test_cli.py | 9 ++++++++- 7 files changed, 41 insertions(+), 11 deletions(-) diff --git a/acclimatise/cli.py b/acclimatise/cli.py index 16c19b3..11999a0 100644 --- a/acclimatise/cli.py +++ b/acclimatise/cli.py @@ -108,7 +108,9 @@ def explore( for format in formats: converter_cls = WrapperGenerator.choose_converter(format) converter = converter_cls( - generate_names=generate_names, ignore_positionals=not pos, case=case, + generate_names=generate_names, + ignore_positionals=not pos, + case=case, ) list(converter.generate_tree(command, out_dir)) @@ -133,7 +135,9 @@ def pipe(cmd, pos, generate_names, case, format): converter_cls = WrapperGenerator.choose_converter(format) converter = converter_cls( - generate_names=generate_names, ignore_positionals=not pos, case=case, + generate_names=generate_names, + ignore_positionals=not pos, + case=case, ) output = converter.save_to_string(command) print(output) diff --git a/acclimatise/converter/wdl.py b/acclimatise/converter/wdl.py index 5b35d66..8bf1e7c 100644 --- a/acclimatise/converter/wdl.py +++ b/acclimatise/converter/wdl.py @@ -36,7 +36,12 @@ def flag_to_command_input( if isinstance(named_flag.arg, model.Flag): args.update(dict(optional=named_flag.arg.optional)) if isinstance(named_flag.arg.args, model.EmptyFlagArg): - args.update(dict(true=named_flag.arg.longest_synonym, false="")) + args.update( + dict( + true=named_flag.arg.longest_synonym, + false="" + ) + ) else: args.update(dict(prefix=named_flag.arg.longest_synonym,)) elif isinstance(named_flag, model.Positional): diff --git a/acclimatise/model.py b/acclimatise/model.py index 1b9c0a1..93154c3 100644 --- a/acclimatise/model.py +++ b/acclimatise/model.py @@ -298,14 +298,14 @@ def get_type(self) -> cli_types.CliType: self.args.get_type(), infer_type(self.full_name()), infer_type(self.description), - cli_types.CliString() + cli_types.CliString(), ] for c in tpe_cand: if tpe is None: if c is not None: tpe = c else: - if issubclass(c, tpe): + if isinstance(type(c), type(tpe)): tpe = c return tpe diff --git a/acclimatise/usage_parser/elements.py b/acclimatise/usage_parser/elements.py index 8f5b048..bc83086 100644 --- a/acclimatise/usage_parser/elements.py +++ b/acclimatise/usage_parser/elements.py @@ -36,7 +36,11 @@ def action(s, loc, toks): mandatory_element = ( element_char.copy() - .setParseAction(lambda s, loc, toks: UsageElement(text=toks[0],)) + .setParseAction( + lambda s, loc, toks: UsageElement( + text=toks[0], + ) + ) .setName("MandatoryElement") ) """ diff --git a/test/executors/test_docker.py b/test/executors/test_docker.py index 31f4a26..1dcd8e2 100644 --- a/test/executors/test_docker.py +++ b/test/executors/test_docker.py @@ -7,7 +7,9 @@ def test_docker(bwamem_help): client = docker.from_env() container = client.containers.run( - "biocontainers/bwa:v0.7.17_cv1", entrypoint=["sleep", "999999999"], detach=True, + "biocontainers/bwa:v0.7.17_cv1", + entrypoint=["sleep", "999999999"], + detach=True, ) exec = DockerExecutor(container) @@ -23,7 +25,9 @@ def test_docker_kill(): """ client = docker.from_env(timeout=99999) container = client.containers.run( - "ubuntu:latest", entrypoint=["sleep", "999999999"], detach=True, + "ubuntu:latest", + entrypoint=["sleep", "999999999"], + detach=True, ) exec = DockerExecutor(container) @@ -54,7 +58,9 @@ def test_infinite_output(): """ client = docker.from_env(timeout=99999) container = client.containers.run( - "ubuntu:latest", entrypoint=["sleep", "999999999"], detach=True, + "ubuntu:latest", + entrypoint=["sleep", "999999999"], + detach=True, ) exec = DockerExecutor(container) diff --git a/test/name_generation/test_single_flag.py b/test/name_generation/test_single_flag.py index d0ba078..acf1337 100644 --- a/test/name_generation/test_single_flag.py +++ b/test/name_generation/test_single_flag.py @@ -54,7 +54,11 @@ def test_name_to_words(gen): """ Check that we can get an argument name even if the argument's flag is a symbol """ - arg = Flag(synonyms=["--genomepaths"], description="", args=EmptyFlagArg(),) + arg = Flag( + synonyms=["--genomepaths"], + description="", + args=EmptyFlagArg(), + ) name = gen.choose_variable_names([arg])[0].name assert "genome" in name diff --git a/test/test_cli.py b/test/test_cli.py index 37a1046..47881da 100644 --- a/test/test_cli.py +++ b/test/test_cli.py @@ -69,7 +69,14 @@ def test_explore_samtools_no_subcommands(runner, caplog): caplog.set_level(100000) with tempfile.TemporaryDirectory() as tempdir: result = runner.invoke( - main, ["explore", "samtools", "--no-subcommands", "--out-dir", tempdir,], + main, + [ + "explore", + "samtools", + "--no-subcommands", + "--out-dir", + tempdir, + ], ) cli_worked(result) # Since we aren't looking at subcommands, there should be one file for each format