Skip to content

Commit 9a6461b

Browse files
committed
refactor: clean up return type handling
1 parent ad5fdee commit 9a6461b

2 files changed

Lines changed: 58 additions & 70 deletions

File tree

argunparse/argument_unparser.py

Lines changed: 41 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -45,78 +45,66 @@ def unparse_arg(self, arg: t.Any) -> str: # pylint: disable = no-self-use
4545
arg = '""'
4646
return arg
4747

48-
def unparse_args(self, arguments: t.Sequence[t.Any],
49-
*, to_list: bool = False) -> t.Union[str, t.List[str]]:
50-
"""Convert list to string of command-line args."""
48+
def unparse_args_to_list(self, arguments: t.Sequence[t.Any]) -> list[str]:
49+
"""Convert list of objects to a list of command-line args."""
5150
unparsed_list = []
5251
for arg in arguments:
5352
unparsed_list.append(self.unparse_arg(arg))
5453
_LOG.debug('%s: unparsed args to %s', self, unparsed_list)
55-
if to_list:
56-
return unparsed_list
57-
unparsed = ' '.join(unparsed_list)
54+
return unparsed_list
55+
56+
def unparse_args(self, arguments: t.Sequence[t.Any]) -> str:
57+
"""Convert list to string of command-line args."""
58+
unparsed = ' '.join(self.unparse_args_to_list(arguments))
5859
_LOG.debug('%s: converted unparsed args to string "%s"', self, unparsed)
5960
return unparsed
6061

61-
def unparse_option(self, key: str, value: t.Any,
62-
*, to_list: bool = False) -> t.Union[str, t.List[str]]:
63-
"""Convert a key-value pair into a string that can be used as a command-line option."""
62+
def unparse_option_to_list(self, key: str, value: t.Any) -> list[str]:
63+
"""Convert a key-value pair into a short list to be used as a command-line option."""
6464
if option_should_be_skipped(value):
65-
return [] if to_list else ''
65+
return []
6666
unparsed_key = f'{self._long_opt if len(key) > 1 else self._short_opt}{key}'
67-
if not treat_as_option_with_no_value(value):
68-
unparsed_value = self.unparse_arg(value)
69-
if to_list and (self._opt_value == ' ' or treat_as_option_with_no_value(value)):
70-
if treat_as_option_with_no_value(value):
71-
return [unparsed_key]
72-
return [unparsed_key, unparsed_value]
73-
if not treat_as_option_with_no_value(value):
74-
unparsed_option = f'{unparsed_key}{self._opt_value}{unparsed_value}'
75-
if to_list:
76-
return [unparsed_option]
7767
if treat_as_option_with_no_value(value):
78-
return unparsed_key
79-
return unparsed_option
68+
return [unparsed_key]
69+
unparsed_value = self.unparse_arg(value)
70+
if self._opt_value == ' ':
71+
return [unparsed_key, unparsed_value]
72+
return [f'{unparsed_key}{self._opt_value}{unparsed_value}']
8073

81-
def unparse_options(self, options: t.Mapping[str, t.Any],
82-
*, to_list: bool = False) -> t.Union[str, t.List[str]]:
83-
"""Convert dictionary to string of command-line args."""
74+
def unparse_option(self, key: str, value: t.Any) -> str:
75+
"""Convert a key-value pair into a string that can be used as a command-line option."""
76+
if option_should_be_skipped(value):
77+
return ''
78+
unparsed = ' '.join(self.unparse_option_to_list(key, value))
79+
return unparsed
80+
81+
def unparse_options_to_list(self, options: t.Mapping[str, t.Any]) -> list[str]:
82+
"""Convert dictionary to a list of command-line args."""
8483
unparsed_list: t.List[str] = []
8584
for key, value in options.items():
8685
if option_should_be_skipped(value):
8786
continue
88-
unparsed_option = self.unparse_option(key, value, to_list=to_list)
89-
if to_list:
90-
unparsed_list += unparsed_option
91-
else:
92-
assert isinstance(unparsed_option, str), type(unparsed_option)
93-
unparsed_list.append(unparsed_option)
87+
unparsed_list += self.unparse_option_to_list(key, value)
9488
_LOG.debug('%s: unparsed options to %s', self, unparsed_list)
95-
if to_list:
96-
return unparsed_list
97-
unparsed = ' '.join(unparsed_list)
89+
return unparsed_list
90+
91+
def unparse_options(self, options: t.Mapping[str, t.Any]) -> str:
92+
"""Convert dictionary to string of command-line args."""
93+
unparsed = ' '.join(self.unparse_options_to_list(options))
9894
_LOG.debug('%s: converted unparsed options to string "%s"', self, unparsed)
9995
return unparsed
10096

101-
def unparse_options_and_args(self, options: t.Mapping[str, t.Any], arguments: t.Sequence[t.Any],
102-
*, to_list: bool = False) -> t.Union[str, t.List[str]]:
97+
def unparse_options_and_args_to_list(
98+
self, options: t.Mapping[str, t.Any], arguments: t.Sequence[t.Any]) -> list[str]:
99+
"""Convert dictionary and list to a list of command-line args."""
100+
unparsed_options = [] if options is None else self.unparse_options_to_list(options)
101+
unparsed_args = [] if arguments is None else self.unparse_args_to_list(arguments)
102+
return unparsed_options + unparsed_args
103+
104+
def unparse_options_and_args(
105+
self, options: t.Mapping[str, t.Any], arguments: t.Sequence[t.Any]) -> str:
103106
"""Convert dictionary and list to string of command-line args."""
104-
if options is None:
105-
unparsed_options = [] if to_list else ''
106-
else:
107-
unparsed_options = self.unparse_options(options, to_list=to_list)
108-
if arguments is None:
109-
unparsed_args = [] if to_list else ''
110-
else:
111-
unparsed_args = self.unparse_args(arguments, to_list=to_list)
112-
if to_list:
113-
return unparsed_options + unparsed_args
114-
unparsed = []
115-
if unparsed_options:
116-
unparsed.append(unparsed_options)
117-
if unparsed_args:
118-
unparsed.append(unparsed_args)
119-
return ' '.join(unparsed)
107+
return ' '.join(self.unparse_options_and_args_to_list(options, arguments))
120108

121109
def unparse_to_list(self, *args, **kwargs) -> list:
122110
"""Unparse given args as command-line arguments and kwargs as command-line options.
@@ -125,7 +113,7 @@ def unparse_to_list(self, *args, **kwargs) -> list:
125113
126114
This process is a reverse of what built-in argparse module does with parse_args() method.
127115
"""
128-
return self.unparse_options_and_args(kwargs, args, to_list=True)
116+
return self.unparse_options_and_args_to_list(kwargs, args)
129117

130118
def unparse(self, *args, **kwargs) -> str:
131119
"""Unparse given args as command-line arguments and kwargs as command-line options.

test/test_argument_unparser.py

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -24,41 +24,41 @@ def test_option(self):
2424
for reference, option in OPTIONS.items():
2525
with self.subTest(option=option):
2626
key, value = list(*itertools.chain(option.items()))
27+
list_result = unparser.unparse_option_to_list(key, value)
28+
self.assertListEqual([reference], list_result)
2729
result = unparser.unparse_option(key, value)
2830
self.assertEqual(reference, result)
29-
list_result = unparser.unparse_option(key, value, to_list=True)
30-
self.assertListEqual([reference], list_result)
3131

3232
def test_option_skipped(self):
3333
unparser = ArgumentUnparser()
3434
for option in OPTIONS_SKIPPED.items():
3535
with self.subTest(option=option):
3636
key, value = option
37+
list_result = unparser.unparse_option_to_list(key, value)
38+
self.assertListEqual(list_result, [])
3739
result = unparser.unparse_option(key, value)
3840
self.assertEqual(result, '')
39-
list_result = unparser.unparse_option(key, value, to_list=True)
40-
self.assertListEqual(list_result, [])
4141

4242
def test_option_space(self):
4343
unparser = ArgumentUnparser(opt_value=' ')
4444
for reference, option in OPTIONS.items():
4545
with self.subTest(option=option):
4646
key, value = list(*itertools.chain(option.items()))
47+
list_result = unparser.unparse_option_to_list(key, value)
48+
self.assertListEqual(reference.split('='), list_result)
4749
result = unparser.unparse_option(key, value)
4850
self.assertEqual(reference.replace('=', ' '), result)
49-
list_result = unparser.unparse_option(key, value, to_list=True)
50-
self.assertListEqual(reference.split('='), list_result)
5151

5252
def test_options(self):
5353
_LOG.debug('testing %i option variants...',
5454
len(OPTIONS_VARIANTS) + len(OPTIONS_SKIPPED_VARIANTS))
5555
unparser = ArgumentUnparser()
5656
for reference, options in itertools.chain(OPTIONS_VARIANTS, OPTIONS_SKIPPED_VARIANTS):
5757
with self.subTest(options=options):
58+
list_result = unparser.unparse_options_to_list(options)
59+
self.assertListEqual(reference, list_result)
5860
result = unparser.unparse_options(options)
5961
self.assertEqual(' '.join(reference), result)
60-
list_result = unparser.unparse_options(options, to_list=True)
61-
self.assertListEqual(reference, list_result)
6262

6363
def test_options_space(self):
6464
_LOG.debug('testing %i option variants...',
@@ -68,7 +68,7 @@ def test_options_space(self):
6868
with self.subTest(options=options):
6969
result = unparser.unparse_options(options)
7070
self.assertEqual(' '.join(reference).replace('=', ' '), result)
71-
list_result = unparser.unparse_options(options, to_list=True)
71+
list_result = unparser.unparse_options_to_list(options)
7272
fixed_reference = list(itertools.chain.from_iterable(
7373
[_.split('=') if '=' in _ else [_] for _ in reference]))
7474
self.assertListEqual(fixed_reference, list_result)
@@ -84,30 +84,30 @@ def test_args(self):
8484
unparser = ArgumentUnparser()
8585
for reference, args in ARGUMENTS_VARIANTS:
8686
with self.subTest(args=args):
87+
list_result = unparser.unparse_args_to_list(args)
88+
self.assertListEqual(reference, list_result)
8789
result = unparser.unparse_args(args)
8890
self.assertEqual(' '.join(reference), result)
89-
list_result = unparser.unparse_args(args, to_list=True)
90-
self.assertListEqual(reference, list_result)
9191

9292
def test_options_and_args(self):
9393
unparser = ArgumentUnparser()
9494
for (reference_options, options), (reference_args, args) in OPTIONS_AND_ARGUMENTS_VARIANTS:
9595
with self.subTest(options=options, args=args):
96+
list_result = unparser.unparse_options_and_args_to_list(None, args)
97+
self.assertListEqual(reference_args, list_result)
9698
result = unparser.unparse_options_and_args(None, args)
9799
self.assertEqual(' '.join(reference_args), result)
98-
list_result = unparser.unparse_options_and_args(None, args, to_list=True)
99-
self.assertListEqual(reference_args, list_result)
100100

101+
list_result = unparser.unparse_options_and_args_to_list(options, None)
102+
self.assertListEqual(reference_options, list_result)
101103
result = unparser.unparse_options_and_args(options, None)
102104
self.assertEqual(' '.join(reference_options), result)
103-
list_result = unparser.unparse_options_and_args(options, None, to_list=True)
104-
self.assertListEqual(reference_options, list_result)
105105

106+
list_result = unparser.unparse_options_and_args_to_list(options, args)
107+
self.assertListEqual(reference_options + reference_args, list_result)
106108
result = unparser.unparse_options_and_args(options, args)
107109
self.assertEqual(
108110
' '.join(itertools.chain(reference_options, reference_args)), result)
109-
list_result = unparser.unparse_options_and_args(options, args, to_list=True)
110-
self.assertListEqual(reference_options + reference_args, list_result)
111111

112112
def test_unparse(self):
113113
unparser = ArgumentUnparser()

0 commit comments

Comments
 (0)