Skip to content

Commit edf6934

Browse files
authored
Merge pull request #54 from Cebtenzzre/fix-literal-append
Fix action=append for List[Literal[...]]
2 parents f30ffbe + 5e2c098 commit edf6934

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

tap/tap.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,8 @@ def _add_argument(self, *name_or_flags, **kwargs) -> None:
191191
and len(get_args(var_type)) > 0
192192
and is_literal_type(get_args(var_type)[0])):
193193
var_type, kwargs['choices'] = get_literals(get_args(var_type)[0], variable)
194-
kwargs['nargs'] = kwargs.get('nargs', '*')
194+
if kwargs.get('action') not in {'append', 'append_const'}:
195+
kwargs['nargs'] = kwargs.get('nargs', '*')
195196
# Handle Tuple type (with type args) by extracting types of Tuple elements and enforcing them
196197
elif get_origin(var_type) in (Tuple, tuple) and len(get_args(var_type)) > 0:
197198
loop = False

tests/test_actions.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import sys
22
from typing import List
3+
from typing_extensions import Literal
34
import unittest
45
from unittest import TestCase
56

@@ -99,6 +100,16 @@ def configure(self):
99100
args = AppendListIntTap().parse_args('--arg 3 --arg 4'.split())
100101
self.assertEqual(args.arg, [1, 2, 3, 4])
101102

103+
def test_actions_append_list_literal(self):
104+
class AppendListLiteralTap(Tap):
105+
arg: List[Literal['what', 'is', 'up', 'today']] = ['what', 'is']
106+
107+
def configure(self):
108+
self.add_argument('--arg', action='append')
109+
110+
args = AppendListLiteralTap().parse_args('--arg up --arg today'.split())
111+
self.assertEqual(args.arg, 'what is up today'.split())
112+
102113
def test_actions_append_untyped(self):
103114
class AppendListStrTap(Tap):
104115
arg = ['what', 'is']

0 commit comments

Comments
 (0)