File: typing_test.py

package info (click to toggle)
python-nubia 0.2.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 772 kB
  • sloc: python: 4,182; makefile: 9; sh: 1
file content (641 lines) | stat: -rw-r--r-- 23,094 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
#!/usr/bin/env python3

# Copyright (c) Facebook, Inc. and its affiliates.
# All rights reserved.
#
# This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree.
#

import argparse
import inspect
import typing
import unittest
from io import StringIO

from nubia.internal.typing import argument, command
from nubia.internal.typing.argparse import add_command, find_command
from nubia.internal.typing.builder import build_value


class ParseError(Exception):
    pass


class ContainedParser(argparse.ArgumentParser):
    """
    Parser that gives options that avoid using sys.stdout, sys.stderr and
    raising SystemExit
    """

    def help(self):
        return self._print_to_buffer(self.print_help)

    def usage(self):
        return self._print_to_buffer(self.print_usage)

    def _print_to_buffer(self, print_function):
        s = StringIO()
        print_function(s)
        return s.getvalue()

    def error(self, message):
        raise ParseError(message)


class SimpleValuesBuilderTest(unittest.TestCase):
    def test_build_string(self):
        value = build_value("some string", str, False)
        self.assertEqual(value, "some string")

        value = build_value('"some string"', str, True)
        self.assertEqual(value, "some string")

    def test_build_int(self):
        value = build_value("1", int, False)
        self.assertEqual(value, 1)

        value = build_value("1", int, True)
        self.assertEqual(value, 1)

    def test_build_custom_type(self):
        def parser(string):
            return string.split("#")

        value = build_value("special#string", parser, False)
        self.assertEqual(value, ["special", "string"])

        value = build_value('"special#string"', parser, True)
        self.assertEqual(value, ["special", "string"])

    def test_build_tuple(self):
        value = build_value("foo bar,1,0.5", typing.Tuple[str, int, float], False)
        self.assertEqual(value, ("foo bar", 1, 0.5))

        value = build_value('("foo bar",1,0.5)', typing.Tuple[str, int, float], True)
        self.assertEqual(value, ("foo bar", 1, 0.5))

    def test_build_tuple_partially_typed(self):
        value = build_value(
            "foo bar,1,0.5", typing.Tuple[str, typing.Any, float], False
        )
        self.assertEqual(value, ("foo bar", "1", 0.5))

        value = build_value(
            '("foo bar",1,0.5)', typing.Tuple[str, typing.Any, float], True
        )
        self.assertEqual(value, (str("foo bar"), 1, 0.5))

    def test_build_tuple_untyped(self):
        value = build_value("foo bar,1,0.5", typing.Tuple, False)
        self.assertEqual(value, ("foo bar", "1", "0.5"))

        value = build_value('("foo bar",1,0.5)', typing.Tuple, True)
        self.assertEqual(value, (str("foo bar"), 1, 0.5))

    def test_build_tuple_single_element(self):
        value = build_value("foo bar", typing.Tuple[str], False)
        self.assertEqual(value, ("foo bar",))

        value = build_value('("foo bar",)', typing.Tuple[str], True)
        self.assertEqual(value, (str("foo bar"),))

    def test_build_typed_dict(self):
        value = build_value("a:1;b:2", typing.Mapping[str, int], False)
        self.assertEqual(value, {"a": 1, "b": 2})

        value = build_value(
            '{"a": "1", "b": 2, "c": 3.2}', typing.Mapping[str, int], True
        )
        self.assertEqual(value, {"a": 1, "b": 2, "c": 3})

    def test_build_typed_dict_mixed(self):
        value = build_value("a=1;b=2", typing.Mapping[str, int], False)
        self.assertEqual(value, {"a": 1, "b": 2})

        value = build_value("a:1;b=2", typing.Mapping[str, int], False)
        self.assertEqual(value, {"a": 1, "b": 2})

    def test_build_typed_dict_with_list(self):
        value = build_value("a=1,2,3;b=2", typing.Mapping[str, str], False)
        self.assertEqual(value, {"a": "1,2,3", "b": "2"})

        value = build_value("a=1,2,3;b=2", typing.Mapping[str, typing.List[int]], False)
        self.assertEqual(value, {"a": [1, 2, 3], "b": [2]})

    def test_build_partially_typed_dict(self):
        value = build_value("a:1;b:2", typing.Mapping[typing.Any, int], False)
        self.assertEqual(value, {"a": 1, "b": 2})

        value = build_value(
            '{"a": "1", "b": 2, 0: 3}', typing.Mapping[typing.Any, int], True
        )
        self.assertEqual(value, {"a": 1, "b": 2, 0: 3})

    def test_build_untyped_dict(self):
        value = build_value("a:1;b:2", typing.Mapping, False)
        self.assertEqual(value, {"a": "1", "b": "2"})

        value = build_value('{"a": 1, "b": 2.5}', typing.Mapping, True)
        self.assertEqual(value, {"a": 1, "b": 2.5})

    def test_build_typed_list(self):
        value = build_value("1,2,3", typing.List[int], False)
        self.assertEqual(value, [1, 2, 3])

        value = build_value("hello,world,test", typing.List[str], False)
        self.assertEqual(value, ["hello", "world", "test"])

        value = build_value("hello", typing.List[str], False)
        self.assertEqual(value, ["hello"])

        value = build_value('["1",2,3.2]', typing.List[int], True)
        self.assertEqual(value, [1, 2, 3])

    def test_build_untyped_list(self):
        value = build_value("1,2,3", typing.List, False)
        self.assertEqual(value, ["1", "2", "3"])

        value = build_value('["1",2,3.5]', typing.List, True)
        self.assertEqual(value, ["1", 2, 3.5])

    def test_build_any_typed_list(self):
        value = build_value("1,2,3", typing.List[typing.Any], False)
        self.assertEqual(value, ["1", "2", "3"])

        value = build_value('["1",2,3.5]', typing.List[typing.Any], True)
        self.assertEqual(value, ["1", 2, 3.5])

    def test_build_whitespaces(self):
        value = build_value(" a : 1 ; b : 2 ", typing.Mapping[str, int], False)
        self.assertEqual(value, {"a": 1, "b": 2})

        value = build_value('{ "a" : 1 , "b" : 2 }', typing.Mapping[str, int], True)
        self.assertEqual(value, {"a": 1, "b": 2})

        value = build_value(" 1 , 2 , 3 ", typing.List[int], False)
        self.assertEqual(value, [1, 2, 3])

        value = build_value("[ 1 , 2 , 3 ]", typing.List[int], True)
        self.assertEqual(value, [1, 2, 3])

        value = build_value(" 1 , 2 , 3 ", typing.Tuple[int, int, int], False)
        self.assertEqual(value, (1, 2, 3))

        value = build_value("( 1 , 2 , 3 )", typing.Tuple[int, int, int], True)
        self.assertEqual(value, (1, 2, 3))

    def test_build_with_casting(self):
        value = build_value("a:1;b:2;c:3", typing.Mapping[str, float])
        self.assertEqual(value, {"a": 1.0, "b": 2.0, "c": 3.0})

        value = build_value("a:1;b:2;c:3", typing.Mapping[str, str])
        self.assertEqual(value, {"a": "1", "b": "2", "c": "3"})

        self.assertRaises(
            ValueError, build_value, "a:1;b:2;c:3", typing.Mapping[int, int]
        )

    def test_build_nested_structures(self):
        inpt = """{
            "a": 1,
            "b": {
                "c": [2, 3, 4, [5, 6]]
            }
        }"""
        expected = {"a": 1, "b": {"c": [2, 3, 4, [5, 6]]}}
        expected_type = typing.Any
        self.assertEqual(build_value(inpt, expected_type, True), expected)

        inpt = """{
            "a": [ [1, 2], [3, 4] ],
            "b": [ [10, 20, 30], [40] ]
        }"""
        expected = {"a": [[1, 2], [3, 4]], "b": [[10, 20, 30], [40]]}
        # dict of str => list of list of ints
        expected_type = typing.Mapping[str, typing.List[typing.List[int]]]
        self.assertEqual(build_value(inpt, expected_type, True), expected)

    def test_build_tuple_error(self):
        # too many arguments
        self.assertRaises(
            ValueError,
            build_value,
            "foo bar,1,0.5,extra!",
            typing.Tuple[str, int, float],
            False,
        )

        self.assertRaises(
            ValueError,
            build_value,
            '("foo bar", 1, 0.5, "extra!")',
            typing.Tuple[str, int, float],
            True,
        )

        # too few arguments
        self.assertRaises(
            ValueError, build_value, "foo bar", typing.Tuple[str, int, float], False
        )

        self.assertRaises(
            ValueError, build_value, '("foo bar",)', typing.Tuple[str, int, float], True
        )


class ArgparseExtensionTest(unittest.TestCase):
    def test_no_decorator_simple(self):
        def foo():
            return "bar"

        def foo2(arg1, arg2):
            return (arg1, arg2)

        self._test(foo, "foo".split(), "bar")
        self._test(
            foo,
            "foo --invalid arg".split(),
            ParseError("unrecognized arguments: --invalid arg"),
        )

        self._test(foo2, "foo2 --arg1=abc --arg2=123".split(), ("abc", "123"))
        self._test(foo2, "foo2 --arg1 abc --arg2 123".split(), ("abc", "123"))

    def test_no_decorator_defaults(self):
        def foo(arg1="bar"):
            return arg1

        def foo2(arg1=True):
            return arg1

        def foo3(arg1=False):
            return arg1

        self._test(foo, "foo".split(), "bar")
        self._test(foo, "foo --arg1 lol".split(), "lol")

        # boolean args are exposed as flags that works as on/off switches
        # if the argument default is True, the flag works as an "off" switch
        self._test(foo2, "foo2".split(), True)
        self._test(foo2, "foo2 --arg1".split(), False)
        # if the argument default is False, the flag works as an "on" switch
        self._test(foo3, "foo3".split(), False)
        self._test(foo3, "foo3 --arg1".split(), True)

    def test_argument_decorated_simple(self):
        @argument("arg1")
        @argument("arg2")
        def foo(arg1, arg2):
            return "{} {}".format(arg1, arg2)

        self._test(foo, "foo --arg1 Hello --arg2 World".split(), "Hello World")

    def test_argument_decorated_different_name(self):
        @argument("arg1", name="banana")
        @argument("arg2", name="apple")
        def foo(arg1, arg2):
            return "{} {}".format(arg1, arg2)

        # arg2 is not decorated
        @argument("arg1", name="banana")
        def foo2(arg1, arg2):
            return "{} {}".format(arg1, arg2)

        # arg2 is decorated but pretty much useless in this form
        @argument("arg1", name="banana")
        @argument("arg2")
        def foo3(arg1, arg2):
            return "{} {}".format(arg1, arg2)

        self._test(foo, "foo --banana Hello --apple World".split(), "Hello World")
        self._test(foo2, "foo2 --banana Hello --arg2 World".split(), "Hello World")
        self._test(foo3, "foo3 --banana Hello --arg2 World".split(), "Hello World")

        self._test(foo, "foo --arg1 Hello --apple World".split(), ParseError)

    def test_argument_decorated_aliases(self):
        @argument("arg", aliases=["banana", "apple", "b", "a"])
        def foo(arg):
            return arg

        self._test(foo, "foo --arg bar".split(), "bar")
        self._test(foo, "foo --banana bar".split(), "bar")
        self._test(foo, "foo --apple bar".split(), "bar")
        self._test(foo, "foo -b bar".split(), "bar")
        self._test(foo, "foo -a bar".split(), "bar")

    def test_argument_decorated_kwargs(self):
        @argument("arg", type=int, description="arg help")
        @argument("extra_arg", type=int, description="extra")
        def foo(arg, **kwargs):
            return (arg, kwargs)

        self._test(foo, "foo --arg 6".split(), (6, {"extra_arg": None}))
        self._test(foo, "foo --extra-arg 15".split(), ParseError)
        self._test(foo, "foo --arg 14 --another-extra-arg 15".split(), ParseError)
        self._test(foo, "foo --arg 3 --extra-arg 15".split(), (3, {"extra_arg": 15}))

    def test_argument_decorated_naming_conventions(self):
        @argument("arg_1", aliases=["_argument__1"])
        @argument("arg_2", name="_argument___2")
        def __foo__bar__(arg_1, arg_2):
            return "{} {}".format(arg_1, arg_2)

        self._test(__foo__bar__, "foo-bar --arg-1 x --argument-2 y".split(), "x y")
        self._test(__foo__bar__, "foo-bar --argument-1 x --argument-2 y".split(), "x y")

    def test_argument_dict_list_type_lifting(self):
        @argument("arg_1", type=typing.Mapping[str, int])
        @argument("arg_2", type=typing.List[int])
        def __foo__bar__(arg_1, arg_2):
            return (arg_1, arg_2)

        self._test(__foo__bar__, "foo-bar --arg-1 x --arg-2 y".split(), ParseError)

        self._test(__foo__bar__, "foo-bar --arg-1 1 --arg-2 2".split(), ParseError)
        self._test(
            __foo__bar__,
            "foo-bar --arg-1 allData=1 --arg-2 2".split(),
            ({"allData": 1}, [2]),
        )
        self._test(
            __foo__bar__,
            "foo-bar --arg-1 all=1;nothing-data:2 --arg-2 2".split(),
            ({"all": 1, "nothing-data": 2}, [2]),
        )
        self._test(
            __foo__bar__,
            "foo-bar --arg-1 all=1;nothing-data=2 --arg-2 2".split(),
            ({"all": 1, "nothing-data": 2}, [2]),
        )
        self._test(
            __foo__bar__,
            "foo-bar --arg-1 all=1;nothing-data=2 --arg-2 2 3".split(),
            ({"all": 1, "nothing-data": 2}, [2, 3]),
        )
        self._test(
            __foo__bar__,
            "foo-bar --arg-1 all=1;nothing-data=2 --arg-2 2 3".split(),
            ({"all": 1, "nothing-data": 2}, [2, 3]),
        )

    def test_argument_list_in_dict_type_lifting(self):
        @argument("arg_1", type=typing.Mapping[str, typing.List[int]])
        def __foo__bar__(arg_1):
            return arg_1

        self._test(__foo__bar__, "foo-bar --arg-1 x".split(), ParseError)

        self._test(__foo__bar__, "foo-bar --arg-1 allData=1".split(), {"allData": [1]})
        self._test(
            __foo__bar__,
            "foo-bar --arg-1 all=1;nothing-data:2".split(),
            {"all": [1], "nothing-data": [2]},
        )
        self._test(
            __foo__bar__,
            "foo-bar --arg-1 all=1,2,3;nothing-data=2".split(),
            {"all": [1, 2, 3], "nothing-data": [2]},
        )
        self._test(
            __foo__bar__,
            "foo-bar --arg-1 all=1;nothing-data=2,2,3".split(),
            {"all": [1], "nothing-data": [2, 2, 3]},
        )

    def test_argument_decorated_unknown_arg(self):
        with self.assertRaises(NameError):

            @argument("arg1", description="arg1 description")
            @argument("bar", description="this arg doesnt exist!")
            def foo(arg1, arg2):
                pass

    def test_kwargs(self):
        try:

            @argument("arg1", description="this exists!")
            def foo(arg1, **kwargs):
                pass

        except Exception as e:
            self.fail("Should not have thrown: {}".format(e))

    def test_kwargs_with_arguments(self):
        try:

            @argument("arg1", description="this exists!")
            @argument("arg2", description="this is in kwargs!")
            def foo(arg1, **kwargs):
                pass

        except Exception as e:
            self.fail("Should not have thrown: {}".format(e))

    def test_command_decorator_presence(self):
        def foo():
            return "bar"

        self._test(foo, ["foo"], "bar")
        self._test(command(foo), ["foo"], "bar")
        self._test(command()(foo), ["foo"], "bar")

    def test_command_exclusive_args_simple(self):
        @command(exclusive_arguments=["arg1", "arg2"])
        def foo(arg1="", arg2="", arg3=""):
            return ",".join(str(arg) for arg in (arg1, arg2, arg3))

        self._test(foo, "foo --arg1=bar".split(), "bar,,")
        self._test(foo, "foo --arg2=bar".split(), ",bar,")
        self._test(foo, "foo --arg3=bar".split(), ",,bar")
        self._test(foo, "foo --arg1=bar --arg3=bar".split(), "bar,,bar")
        self._test(foo, "foo --arg2=bar --arg3=bar".split(), ",bar,bar")

        self._test(foo, "foo --arg1=bar --arg2=bar".split(), ParseError)
        self._test(foo, "foo --arg1=bar --arg2=bar --arg3=bar".split(), ParseError)

    def test_command_exclusive_args_array(self):
        @command(exclusive_arguments=[["arg1", "arg2"], ["arg3", "arg4"]])
        def foo(arg1="", arg2="", arg3="", arg4=""):
            return ",".join(str(arg) for arg in (arg1, arg2, arg3, arg4))

        self._test(foo, "foo --arg1=bar".split(), "bar,,,")
        self._test(foo, "foo --arg2=bar".split(), ",bar,,")
        self._test(foo, "foo --arg3=bar".split(), ",,bar,")
        self._test(foo, "foo --arg4=bar".split(), ",,,bar")
        self._test(foo, "foo --arg1=bar --arg3=bar".split(), "bar,,bar,")
        self._test(foo, "foo --arg1=bar --arg4=bar".split(), "bar,,,bar")
        self._test(foo, "foo --arg2=bar --arg3=bar".split(), ",bar,bar,")
        self._test(foo, "foo --arg2=bar --arg4=bar".split(), ",bar,,bar")

        self._test(foo, "foo --arg1=bar --arg2=bar".split(), ParseError)
        self._test(foo, "foo --arg3=bar --arg4=bar".split(), ParseError)
        self._test(
            foo, "foo --arg1=bar --arg2=bar --arg3=bar --arg4=bar".split(), ParseError
        )

    def test_command_repeated_exclusive_args(self):
        with self.assertRaises(ValueError):
            # arg1 is present in two exclusive groups
            @command(exclusive_arguments=[["arg1", "arg2"], ["arg1", "arg3"]])
            def foo(arg1="", arg2="", arg3=""):
                pass

    def test_command_unknown_exclusive_args(self):
        with self.assertRaises(NameError):
            # arg bar doesnt exist
            @command(exclusive_arguments=[["arg1", "bar"]])
            def foo(arg1="", arg2="", arg3=""):
                pass

    def test_duplicate_argument_decorator(self):
        with self.assertRaises(ValueError):
            # two refs to the same arg
            @command
            @argument("arg", name="arg1")
            @argument("arg", name="arg2")
            def foo(arg=1):
                pass

    def test_positional_arg(self):
        @argument("arg", positional=True)
        def foo(arg):
            return arg

        self._test(foo, "foo lalala", "lalala")

    def test_positional_arg_with_default(self):
        @argument("arg1", positional=True)
        @argument("arg2")
        def foo(arg1, arg2="default_arg1"):
            return "{},{}".format(arg1, arg2)

        self._test(foo, "foo lalala", "lalala,default_arg1")
        self._test(foo, "foo lalala --arg2 bububu", "lalala,bububu")
        self._test(foo, "foo --arg2 bububu lalala", "lalala,bububu")

    def test_only_single_value_allowed_for_positional(self):
        @argument("arg1", positional=True)
        def foo(arg1):
            pass

        self._test(foo, "foo lalala bububu", ParseError)

    def test_missing_positional(self):
        @argument("arg", positional=True)
        def foo(arg):
            pass

        self._test(foo, "foo", ParseError)

    def test_multiple_positionals(self):
        @argument("arg1", positional=True)
        @argument("arg2", positional=True)
        @argument("arg3")
        def foo(arg1, arg2, arg3="default"):
            return ",".join([arg1, arg2, arg3])

        self._test(foo, "foo arg1v arg2v", "arg1v,arg2v,default")
        self._test(foo, "foo arg1v arg2v --arg3 arg3v", "arg1v,arg2v,arg3v")
        self._test(foo, "foo arg1v --arg3 arg3v arg2v", "arg1v,arg2v,arg3v")
        self._test(foo, "foo --arg3 arg3v arg1v arg2v", "arg1v,arg2v,arg3v")

    def test_multiple_positionals_not_relates_to_decorator(self):
        # just all permutations of three decorators

        @argument("arg1", positional=True)
        @argument("arg2", positional=True)
        @argument("arg3", positional=True)
        def foo(arg1, arg2, arg3):
            return ",".join([arg1, arg2, arg3])

        self._test(foo, "foo arg1v arg2v arg3v", "arg1v,arg2v,arg3v")

        @argument("arg1", positional=True)
        @argument("arg3", positional=True)
        @argument("arg2", positional=True)
        def foo(arg1, arg2, arg3):
            return ",".join([arg1, arg2, arg3])

        self._test(foo, "foo arg1v arg2v arg3v", "arg1v,arg2v,arg3v")

        @argument("arg2", positional=True)
        @argument("arg1", positional=True)
        @argument("arg3", positional=True)
        def foo(arg1, arg2, arg3):
            return ",".join([arg1, arg2, arg3])

        self._test(foo, "foo arg1v arg2v arg3v", "arg1v,arg2v,arg3v")

        @argument("arg2", positional=True)
        @argument("arg3", positional=True)
        @argument("arg1", positional=True)
        def foo(arg1, arg2, arg3):
            return ",".join([arg1, arg2, arg3])

        self._test(foo, "foo arg1v arg2v arg3v", "arg1v,arg2v,arg3v")

        @argument("arg3", positional=True)
        @argument("arg1", positional=True)
        @argument("arg2", positional=True)
        def foo(arg1, arg2, arg3):
            return ",".join([arg1, arg2, arg3])

        self._test(foo, "foo arg1v arg2v arg3v", "arg1v,arg2v,arg3v")

        @argument("arg3", positional=True)
        @argument("arg2", positional=True)
        @argument("arg1", positional=True)
        def foo(arg1, arg2, arg3):
            return ",".join([arg1, arg2, arg3])

        self._test(foo, "foo arg1v arg2v arg3v", "arg1v,arg2v,arg3v")

    def test_positional_with_default(self):
        msg = (
            "We explicitly do not support positional "
            "with default because it is confusing"
        )
        with self.assertRaises(ValueError, msg=msg):

            @command
            @argument("arg", positional=True)
            def foo(arg="default"):
                return arg

            # validation happens on building parser time so let's build one
            parser = ContainedParser()
            add_command(parser, foo)

    def test_positional_with_aliases(self):
        msg = "Aliases for positional not yet supported"
        with self.assertRaises(ValueError, msg=msg):

            @command
            @argument("arg", positional=True, aliases=["a"])
            def foo(arg="default"):
                return arg

            # validation happens on building parser time so let's build one
            parser = ContainedParser()
            add_command(parser, foo)

    def _test(self, command_function, arguments, expected_result):
        if isinstance(arguments, str):
            arguments = arguments.split()

        parser = ContainedParser()
        add_command(parser, command_function)
        try:
            parsed = parser.parse_args(args=arguments)
        except Exception as e:
            if inspect.isclass(expected_result):
                self.assertIsInstance(e, expected_result)
            elif isinstance(expected_result, ParseError):
                self.assertEqual(str(e), str(expected_result))
            else:
                raise
        else:
            command_function = find_command(parser, parsed, True)
            self.assertEqual(command_function(), expected_result)