Skip to content

Commit 4b89fb9

Browse files
committed
Now works for Optional[List[type]], Optional[List[type]], but only some of Optional[Tuple[type]]
1 parent 43cd071 commit 4b89fb9

File tree

2 files changed

+15
-12
lines changed

2 files changed

+15
-12
lines changed

tap/tap.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,6 @@ def _add_argument(self, *name_or_flags, **kwargs) -> None:
228228
# Handle the cases of List[bool], Set[bool], Tuple[bool]
229229
if var_type == bool:
230230
var_type = boolean_type
231-
232231
# If bool then set action, otherwise set type
233232
if var_type == bool:
234233
if explicit_bool:
@@ -401,6 +400,10 @@ def parse_args(self: TapType,
401400
if type(value) == list:
402401
var_type = get_origin(self._annotations[variable])
403402

403+
# Unpack nested boxed types such as Optional[List[int]]
404+
if var_type is Union:
405+
var_type = get_origin(get_args(self._annotations[variable])[0])
406+
404407
if var_type in (Set, set):
405408
value = set(value)
406409
elif var_type in (Tuple, tuple):

tests/test_integration.py

+11-11
Original file line numberDiff line numberDiff line change
@@ -172,14 +172,14 @@ class NestedOptionalTypesTap(Tap):
172172
set_bool: Optional[Set[bool]]
173173
set_int: Optional[Set[int]]
174174
set_str: Optional[Set[str]]
175-
# tuple_bool: Optional[Tuple[bool]]
176-
# tuple_int: Optional[Tuple[int]]
177-
# tuple_str: Optional[Tuple[str]]
175+
tuple_bool: Optional[Tuple[bool]]
176+
tuple_int: Optional[Tuple[int]]
177+
tuple_str: Optional[Tuple[str]]
178178
# tuple_pair: Optional[Tuple[bool, str, int]]
179179
# tuple_arbitrary_len_bool: Optional[Tuple[bool, ...]]
180180
# tuple_arbitrary_len_int: Optional[Tuple[int, ...]]
181181
# tuple_arbitrary_len_str: Optional[Tuple[str, ...]]
182-
182+
183183

184184
class NestedOptionalTypeTests(TestCase):
185185

@@ -205,9 +205,9 @@ def test_nested_optional_types(self):
205205
'--set_bool', *stringify(set_bool),
206206
'--set_int', *stringify(set_int),
207207
'--set_str', *stringify(set_str),
208-
# '--tuple_bool', *stringify(tuple_bool),
209-
# '--tuple_int', *stringify(tuple_int),
210-
# '--tuple_str', *stringify(tuple_str),
208+
'--tuple_bool', *stringify(tuple_bool),
209+
'--tuple_int', *stringify(tuple_int),
210+
'--tuple_str', *stringify(tuple_str),
211211
# '--tuple_pair', *stringify(tuple_pair),
212212
# '--tuple_arbitrary_len_bool', *stringify(tuple_arbitrary_len_bool),
213213
# '--tuple_arbitrary_len_int', *stringify(tuple_arbitrary_len_int),
@@ -225,10 +225,10 @@ def test_nested_optional_types(self):
225225
self.assertEqual(args.tuple_bool, tuple_bool)
226226
self.assertEqual(args.tuple_int, tuple_int)
227227
self.assertEqual(args.tuple_str, tuple_str)
228-
self.assertEqual(args.tuple_pair, tuple_pair)
229-
self.assertEqual(args.tuple_arbitrary_len_bool, tuple_arbitrary_len_bool)
230-
self.assertEqual(args.tuple_arbitrary_len_int, tuple_arbitrary_len_int)
231-
self.assertEqual(args.tuple_arbitrary_len_str, tuple_arbitrary_len_str)
228+
# self.assertEqual(args.tuple_pair, tuple_pair)
229+
# self.assertEqual(args.tuple_arbitrary_len_bool, tuple_arbitrary_len_bool)
230+
# self.assertEqual(args.tuple_arbitrary_len_int, tuple_arbitrary_len_int)
231+
# self.assertEqual(args.tuple_arbitrary_len_str, tuple_arbitrary_len_str)
232232

233233

234234
class ComplexTypeTap(Tap):

0 commit comments

Comments
 (0)